Description
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 ⇥ 106 while the
maximum relative error is 1.2337 ⇥ 106.
% Use table lookup to compute sin(x)
x = [0:pi/1000:pi]; % Points in table of sin(x)
y = 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
P(x) = ⇢ P1(x) if 0 x 1
P2(x) if 1 x 2
that is defined by the conditions:
– P1(x) is linear.
– P2(x) is quadratic.
– P(x) and P0
(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 P0
2(1) = 2. Write P2(x) in the form c0+c1(x1)+c2(x1)(x2). Then
c0 = P2(1) = 1 and 1 +c1 = P2(2) = 0, so c1 = 1. Since P0
2(x) = c1 +c2(2×3),
we have P0
2(1) = 1 c2 = 2 so c2 = 3. Thus
P(x) = ⇢ 1 2x if 0 x 1
4 8x + 3×2 if 1 x 2
Following is a plot of the function.
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 −1.5
−1
−0.5
0
0.5
1
x
P(x)
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 r0
(0) = 0. [Hint: Start from the left
subinterval.]
In the left subinterval r must satisfy r(0) = 1, r(1) = 2, and r0
(0) = 0. Writing r(x)
in this subinterval in the form c0 +c1x+c2x(x1), we find c0 = r(0) = 1, 1 +c1 =
r(1) = 2, so c1 = 1, and since r0
(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 r0
(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 r0
(1) = 2 d2 = 2,
d2 = 4. Thus, in the right subinterval r(x) = 4×2 + 10x 4. In summary,
r(x) = ⇢ 1 + x2 0 x 1
4×2 + 10x 4 1 x 2 .
106
Problem Set 4: Due Nov 9, 2017.
Fall 2017
• (8.8.15)
2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 −0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
x
e−2x sin(10 pi x)
Not−a−knot cubic spline fit
15. A sampling of altitudes of a hillside is given below:
y
” height(x, y)
6 0001000
5 0002000
4 0024200
3 1248422
2 0024200
1 0002000
0 0001000
0123456 ! x
In this exercise, we will fit a 2D cubic spline (bicubic spline) through this data and plot the
result.
(a) Using the spline command, fit a not-a-knot cubic spline to the data in each row,
evaluating the spline at the points x = 0, 0.1, 0.2,…, 5.9, 6.0. This will produce a 7 ⇥ 61
array of values.
(b) Fit a not-a-knot cubic spline to the data in each column of this array, evaluating each of
these splines at y = 0.0, 0.1, 0.2,…, 5.9, 6.0. This will produce a 61 ⇥ 61 array of values.
Call this matrix M.
The following MATLAB code produces the plots in (c), (d), and (e):
% Bicubic spline fit
height = [ 0 0 0 1 0 0 0;
0 0 0 2 0 0 0;
0 0 2 4 2 0 0;
109
• (9.9.6)
3
• (10.10.2)
4
• (10.10.7)
5