




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Solutions to problem set 5 of a numerical computing course, which covers interpolating polynomials, divided differences, newton basis functions, and matlab functions for polynomial fitting and evaluation. It also includes plots of interpolating polynomials and the runge function for different interpolation methods.
Typology: Assignments
1 / 8
This page cannot be seen from the preview
Don't miss anything!





D. Schwendeman
Due: Thursday, 10/27/
f (t) = 5 exp(−t^2 ) cos(7t), for t ∈ [− 1 , 1]
Use your matlab functions to find the weights for f˜ (t) fitted to the data (ti, f (ti)), i = 1 , 2 ,... , 11, where ti are equally spaced nodes on [− 1 , 1]. (Hint: consider the matlab function t=linspace(-1,1,11) and the matlab examples on the course website.) Plot f (x) and f˜ (x) on the same graph for many values of x on [− 1 , 1] (e.g. use x=linspace(-1,1,400).) Comment on the behavior of the interpolating polynomial. (c) Repeat part (b) for the Chebychev nodes ti = − cos((2i − 1)π/22), i = 1, 2 ,... , 11. (Hint: consider the matlab examples on the course website.)
ti − 3 − 1 0 2 yi − 7 6 4 − 12
Part (a) Following section 7.3.2 of the book as a guide, we see that we have four data points, so n = 4; our interpolating polynomial is p 3 (t) = y 1 1 (t) + y 2 2 (t) + y 3 3 (t) + y 4 4 (t). The equations for j (t) are given asj (t) =
∏nk=1,k 6 =j (t−tk ) ∏nk=1,k 6 =j (tj −tk ) , 1 ≤ j ≤ n. Then we have 1 (t) = (^) (t( 1 t−−tt 22 )()(tt 1 −−tt^33 )()(tt− 1 −t^4 t) 4 ) = (^) (−(t+1)2)(−t(3)(t−−2)5) ; similarly, 2 (t) = (^) (2)((t+3)−1)(t(t−−2)3) , 3 (t) = (t+3)((3)(1)(t+1)(−2)t− 2), and 4 (t) = (t+3)( (5)(3)(2)t+1) t. Hence the interpolant is p 3 (t) = − 7 1 (t) + 6 2 (t) + 43 (t) − 12 4 (t) = 4 − 133 t − 136 t^2 + 16 t^3 , with the `j computed above.
Part (b) For Newton interpolation, the basis functions are given by πj (t) = ∏j k−=1^1 (t − tk), 1 ≤ j ≤ n; the coefficients for these basis functions are given by the divided differences formula, whose recursion relation is given on page 320. The divided differences approach yields the following coefficients (f [t 4 ] = −12 is not in the table).
f [t 1 ] = y 1 = − 7 f [t 2 ] = 6 f [t 3 ] = 4 f [t 1 , t 2 ] = f^ [t t^22 ]−−ft 1 [ t^1 ]= (^) −6+71+3 = 13/ 2 f [t 2 , t 3 ] = (^4) 0+1−^6 = − 2 f [t 3 , t 4 ] = − 212 −− 0 4 = − 8 f [t 1 , t 2 , t 3 ] = f^ [t^2 ,t^3 t 3 ]−−ft 1 [ t^1 ,t^2 ]= −^2 0+3−^13 /^2 = − 17 / 6 f [t 2 , t 3 , t 4 ] = −2+18+2 = − 2 f [t 1 , t 2 , t 3 , t 4 ] = f^ [t^2 ,t^3 ,t t^44 ]−−ft 1 [t 1 ,t^2 ,t^3 ]= −2+172+3 /^6 = 1/ 6
The basis functions are π 1 (t) = 1, π 2 (t) = t − t 1 = t + 3, π 3 (t) = (t − t 1 )(t − t 2 ) = (t + 3)(t + 1), and π 4 = (t − t 1 )(t − t 2 )(t − t 3 ) = (t + 3)(t + 1)t. Hence our interpolant is p(t) = f [t 1 ] + f [t 1 , t 2 ]π 2 (t) + f [t 1 , t 2 , t 3 ]π 3 (t) + f [t 1 , t 2 , t 3 , t 4 ]π 4 (t) = −7 + 132 (t + 3) − 176 (t + 1)(t + 3) + 16 t(t + 1)(t + 3) = 4 − 133 t − 136 t^2 + 16 t^3.
Part (a) Your code for myPolyfit.m might look like this.
1 function a=myPolyfit(t,y)
8 if length(a) 6 =n 9 disp('Error : lengths of t and a must agree') 10 return 11 end 12 13 % nested iteration 14 y=a(n); 15 for k=n-1:-1: 16 y=a(k)+(x-t(k)).*y; 17 end
Part (b) We could use the following code...
1 t = linspace(-1,1,11); f = (^5) * exp(-t.ˆ2) .* cos(7 * t); 2 x = linspace(-1,1,400); a = myPolyfit(t,f); y = myPolyval(t,a,x); 3 fx = (^5) * exp(-x.ˆ2) .* cos(7 * x); plot(x,fx,'k',x,y,'r',t,f,'o');
...to produce the following graph.
−5−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1
−
−
−
−
0
1
2
3
4
5
The original function is in black; the interpolant is in red; interpolation nodes are circled. Note that the interpolant overshoots the original function near the endpoints.
Part (c) Replace the expression for t in the code above with j = 1:11; t = -cos((2j-1)pi/22); to produce the following figure (same legend as above).
−5−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1
−
−
−
−
0
1
2
3
4
5
Note how the choice of nodes prevented the overshoot from occurring in this case.
The general error formula is f (t) − fˆ (t) = f^ (n n)!( θ)(t − t 1 )...(t − tn) for some θ in the interval of interpolation, where the ti are the nodes of the interpolation problem. To bound the LHS, we can bound the RHS. There are several ways to do this; here, I’ll maximize separately f^ (n n)!(θ) and (t − t 1 )...(t − tn). Doing this, we get
∣ f^ (3) n(!θ)
∣ = ∣∣− 1 /(θ + 1)^4 ∣∣^ ≤ 1 (the value at θ = 0); | ∣(t − 1)(t − 3)t| ≤ 2 .113 (the approximate absolute value at t ≈ 2 .215). Hence |f (t) − fˆ (t)| = ∣∣ f (3) n!( θ)(t − 1)(t − 3)t^ ∣∣∣ ≤ max^ ∣∣∣ f (3) n!(θ)^ ∣∣∣ · max |(t − 1)(t − 3)t| ≤ 2 .113.
We could also have taken the approach in the book for a more coarse estimate.
Here’s some code using the built-in functions for interpolation.
1 % Runge function f(t) evaluated at lots of points (for nice plotting) 2 t = linspace(-1,1,10ˆ3); 3 f = 1 ./ (1 + (^25) * t.ˆ2); 4
−60−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1
−
−
−
−
−
0
10
Polynomial interpolation is in black; cubic spline interpolation is in red; the Runge function is in green; the interpolation points are circled.
Using the equations from class, our code might look like this...
1 % enter the data 2 t = .1:.1:.9; y = [1.143,1.168,1.226,1.274,1.289,1.366,1.367,1.471,1.453]; 3 4 % solve the system of equations provided in class 5 a = [length(t),sum(t);sum(t),sum(t.ˆ2)] \ [sum(y);sum(t.y)]; 6 c=[a(2),a(1)]; 7 8 % plot 9 T = linspace(t(1),t( end ),500); plot(t,y,'o',T,a(1)+a(2)T,'r');
...and produce a figure that looks like the following.