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 themaximum 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 x1P 2(x) if 1 x2 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)=12x. P2 is quadratic and has value1 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). Thenc 0 = P2(1) =1 and 1+c1 = P2(2) = 0, so c1 = 1. Since P0 2(x)=c1+c2(2x3),we have P0 2(1) = 1c2 =2 so c2 = 3. Thus P(x)=⇢ 12x if 0x1 48x +3x2 if 1x2 Following is a plot of the function.
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 leftsubinterval 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(x1) + d2(x1)(x2), we find d0 = r(1) = 2, 2 + d1 = r(2) = 0, so d1 = 2, and since r0(1) = 2d2 = 2,d2 =4. Thus, in the right subinterval r(x)=4x2 + 10x4. In summary, r(x)=⇢ 1+x2 0x14x2 + 10x41x2 .
106
Problem Set 4: Due Nov 9, 2017. Fall 2017
• (8.8.15)
2
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⇥61array 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