For each value r(j), find the two consecutive x entries, x(i) and x(i+1

For each value r(j), find the two consecutive x entries, x(i) and x(i+1) that satisfy
x(i) ≤ r(j) ≤ x(i + 1). Having identified the subinterval that contains r(j), use linear
interpolation to estimate sin(r(j)). Compare your results with those returned by Matlab
when you type sin(r). Find the maximum absolute error and the maximum relative
error in your results.
The following MATLAB code implements table lookup as described and finds that
the maximum absolute error in the estimate of sin(r) is 1.1795 × 10−6 while the
maximum relative error is 1.2337 × 10−6 .
% Use table lookup to compute sin(x)
x = [0:pi/1000:pi];
y = sin(x);
% Points in table of sin(x)
r = pi*rand(1,100); % Points at which to estimate sine
sinr = zeros(1,100);
for k=1:100,
i = 1;
% Find interval [x(i),x(i+1)] in table
% that contains r(k)
while r(k) > x(i+1) & i < length(x)-1
i = i+1;
end;
%
Interpolate linearly
sinr(k) = y(i) + (r(k)-x(i))/(x(i+1)-x(i)) * (y(i+1)-y(i));
end;
% Compare with "true" solution returned by MATLAB
sinr_true = sin(r);
abserr = max(abs(sinr_true - sinr))
relerr = max(abs( (sinr_true - sinr)./sinr_true ))
9. Determine the piecewise polynomial function
�
P1 (x) if 0 ≤ x ≤ 1
P (x) =
P2 (x) if 1 ≤ x ≤ 2
that is defined by the conditions:
– P1 (x) is linear.
– P2 (x) is quadratic.
– P (x) and P � (x) are continuous at x = 1.
105
– P (0) = 1, P (1) = −1, and P (2) = 0.
Plot this function.
Since P1 is linear and has value 1 at 0 and value −1 at 1, P1 (x) = 1 − 2x. P2 is
quadratic and has value −1 at 1 and value 0 at 2, and its derivative matches that of
P1 at 1 so P2� (1) = −2. Write P2 (x) in the form c0 +c1 (x−1)+c2 (x−1)(x−2). Then
c0 = P2 (1) = −1 and −1 + c1 = P2 (2) = 0, so c1 = 1. Since P2� (x) = c1 + c2 (2x − 3),
we have P2� (1) = 1 − c2 = −2 so c2 = 3. Thus
�
1 − 2x
if 0 ≤ x ≤ 1
P (x) =
2
4 − 8x + 3x if 1 ≤ x ≤ 2
Following is a plot of the function.
1
0.5
P(x)
0
−0.5
−1
−1.5
0
0.2
0.4
0.6
0.8
1
x
1.2
1.4
1.6
1.8
2
10. Let f be a given function satisfying f (0) = 1, f (1) = 2, and f (2) = 0. A quadratic spline
interpolant r(x) is defined as a piecewise quadratic that interpolates f at the nodes (x0 = 0,
x1 = 1, and x2 = 2) and whose first derivative is continuous throughout the interval. Find
the quadratic spline interpolant of f that also satisfies r� (0) = 0. [Hint: Start from the left
subinterval.]
In the left subinterval r must satisfy r(0) = 1, r(1) = 2, and r� (0) = 0. Writing r(x)
in this subinterval in the form c0 + c1 x + c2 x(x − 1), we find c0 = r(0) = 1, 1 + c1 =
r(1) = 2, so c1 = 1, and since r� (0) = 1 + c2 (−1) = 0, c2 = 1. Thus, in the left
subinterval r(x) = 1+x2 . In the next subinterval, r must satisfy r(1) = 2, r(2) = 0,
and r� (1) = 2 (since this is the derivative of the quadratic in the left subinterval).
Writing r(x) in this subinterval in the form d0 + d1 (x − 1) + d2 (x − 1)(x − 2), we
find d0 = r(1) = 2, 2 + d1 = r(2) = 0, so d1 = −2, and since r� (1) = −2 − d2 = 2,
d2 = −4. Thus, in the right subinterval r(x) = −4x2 + 10x − 4. In summary,
�
0≤x≤1
1 + x2
r(x) =
.
2
−4x + 10x − 4 1 ≤ x ≤ 2
106
11. Let f (x) = x2 (x − 1)2 (x − 2)2 (x − 3)2 . What is the piecewise cubic Hermite interpolant of f
on the grid x0 = 0, x1 = 1, x2 = 2, x3 = 3? Let g(x) = ax3 + bx2 + cx + d for some parameters
a, b, c, and d. Write down the piecewise cubic Hermite interpolant of g on the same grid.
[Note: You should not need to do any arithmetic for either of these problems.]
Since f and f � are 0 at all of the nodes, the piecewise cubic Hermite interpolant
of f is identically 0. Since g is cubic, its piecewise cubic Hermite interpolant is g
itself.
12. Show that the following function is a natural cubic spline through the points (0, 1), (1, 1),
(2, 0), and (3, 10):

0≤x<1
 1 + x − x3
1 − 2(x − 1) − 3(x − 1)2 + 4(x − 1)3 1 ≤ x < 2
s(x) =

2≤x≤3
4(x − 2) + 9(x − 2)2 − 3(x − 2)3
Clearly, s is piecewise cubic, and evaluating s(x) at x = 0, 1, 2, 3, we see that it
goes through the given points. Differentiating each piece of s, we find

0≤x<1
 1 − 3x2
�
−2 − 6(x − 1) + 12(x − 1)2 1 ≤ x < 2 ,
s (x) =

2≤x≤3
4 + 18(x − 2) − 9(x − 2)2
and it can be seen that this function is continuous, with s� (1) = −2, s� (2) = 4.
Differentiating again,

0≤x<1
 −6x
��
−6 + 24(x − 1) 1 ≤ x < 2 .
s (x) =

18 − 18(x − 2) 2 ≤ x ≤ 3
From this it is clear that s�� is continuous, with s�� (1) = −6, s�� (2) = 18. Also,
s�� (0) = 0 and s�� (3) = 0. Therefore s is the natural cubic spline interpolant for the
given data.
13. Determine all the values of a, b, c, d, e, and f for
spline:

 ax2 + b(x − 1)3
cx2 + d
s(x) =
 2
ex + f (x − 2)3
Clearly s is piecewise cubic. In order that it be
4c + d = 4e. Differentiating, we find

 2ax + 3b(x − 1)2
2cx
s� (x) =

2ex + 3f (x − 2)2
107
which the following function is a cubic
x ∈ (−∞, 1]
x ∈ [1, 2]
x ∈ [2, ∞)
continuous, we need a = c + d and
x ∈ (−∞, 1]
x ∈ [1, 2]
.
x ∈ [2, ∞)
In order for this to be continuous, we need a = c = e. Combining this with
the previous requirements, we find that d = 0. Making these substitutions and
differentiating again, we have

 2a + 6b(x − 1) x ∈ (−∞, 1]
��
2a
x ∈ [1, 2]
.
s (x) =

2a + 6f (x − 2) x ∈ [2, ∞)
This is always continuous, with value 2a at x = 1 and x = 2. Thus, any function s
of the following form is a cubic spline:

 ax2 + b(x − 1)3 x ∈ (−∞, 1]
x ∈ [1, 2]
ax2
,
s(x) =

2
3
ax + f (x − 2) x ∈ [2, ∞)
where a, b, and f are arbitrary.
14. Use MATLAB’s spline function to find the not-a-knot cubic spline interpolant of f (x) =
e−2x sin(10πx), using n = 20 subintervals of equal size between x0 = 0 and x21 = 1. [To learn
how to use the spline routine, type help spline in MATLAB.] Plot the function along with
its cubic spline interpolant, marking the knots on the curves.
The following MATLAB code does the cubic spline fit and produces the plot below:
%
Not-a-knot cubic spline fit
x = [0:.05:1];
y = exp(-2*x) .* sin(10*pi*x);
% Function values to be fit
xx = [0:.005:1];
% Points at which to plot function
yy = exp(-2*xx) .*sin(10*pi*xx);
% and spline fit
ys = spline(x,y,xx);
plot(x,y,’o’, xx,yy,’-’, xx,ys,’--r’)
xlabel(’x’)
ylabel(’e^{-2x} sin(10 pi x)’)
title(’Not-a-knot cubic spline fit’)
108