Numerical Computing Problem Set 5 Solutions - Prof. Donald W. Schwendeman, Assignments of Mathematics

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

2011/2012

Uploaded on 02/17/2012

koofers-user-jsl
koofers-user-jsl 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
D. Schwendeman
Numerical Computing Problem Set 5
Due:
Thursday, 10/27/11
1. Consider the data consisting of the four points (3,7), (1,6), (0,4) and (2,12).
Construct an interpolating polynomial of degree three of less that fits the data. Determine
the polynomial (by hand) in two ways: (a) using Lagrange basis functions and (b) using
Newton basis functions. For the Newton basis, determine the weights using a divided
difference table.
2. (a) Write a matlab function, myPolyfit, that computes the weights ai,i= 1,2, . . . , n,
of the Newton interpolating polynomial ˜
f(t) of degree n1 (or less). The input to the
function is the data (ti, yi), i= 1,2, . . . , n and the weights should be computed based
on divided differences following the algorithm discussed in class. Write a second matlab
function, myPolyval, that takes the weights {ai}, the nodes {ti}, and a point x(or vector
of points) and returns the value of ˜
f(x) using the nested iteration discussed in class.
(b) Consider the function
f(t) = 5 exp(t2) 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 tiare 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 xon [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((2i1)π/22), i= 1,2,...,11.
(Hint: consider the matlab examples on the course website.)
3. A polynomial ˜
f(t) of degree two or less is fitted to the function f(t) = (1+ t)1at nodes
t1= 0, t2= 1, and t3= 3. Use the error formula to find an upper bound for the maximum
absolute difference between f(t) and ˜
f(t) for values of ton the interval [0,3].
4. Computer problem 7.4 on page 337.
Note: the interpolating polynomial may be computed using your matlab functions obtained
in problem 2 above or using matlab’s built-in functions polyfit and polyval. The cubic
spline may be computed using matlab’s spline routine.
5. An experiment generates the data
ti0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
yi1.143 1.168 1.226 1.274 1.289 1.366 1.367 1.471 1.453
Find the coefficients a0and a1of the best-fit line of the form y=a0+a1tto the data. Plot
the data and the best-fit line on the same graph.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Numerical Computing Problem Set 5 Solutions - Prof. Donald W. Schwendeman and more Assignments Mathematics in PDF only on Docsity!

D. Schwendeman

Numerical Computing Problem Set 5

Due: Thursday, 10/27/

  1. Consider the data consisting of the four points (− 3 , −7), (− 1 , 6), (0, 4) and (2, −12). Construct an interpolating polynomial of degree three of less that fits the data. Determine the polynomial (by hand) in two ways: (a) using Lagrange basis functions and (b) using Newton basis functions. For the Newton basis, determine the weights using a divided difference table.
  2. (a) Write a matlab function, myPolyfit, that computes the weights ai, i = 1, 2 ,... , n, of the Newton interpolating polynomial f˜ (t) of degree n − 1 (or less). The input to the function is the data (ti, yi), i = 1, 2 ,... , n and the weights should be computed based on divided differences following the algorithm discussed in class. Write a second matlab function, myPolyval, that takes the weights {ai}, the nodes {ti}, and a point x (or vector of points) and returns the value of f˜ (x) using the nested iteration discussed in class. (b) Consider the function

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.)

  1. A polynomial f˜ (t) of degree two or less is fitted to the function f (t) = (1 + t)−^1 at nodes t 1 = 0, t 2 = 1, and t 3 = 3. Use the error formula to find an upper bound for the maximum absolute difference between f (t) and f˜ (t) for values of t on the interval [0, 3].
  2. Computer problem 7.4 on page 337. Note: the interpolating polynomial may be computed using your matlab functions obtained in problem 2 above or using matlab’s built-in functions polyfit and polyval. The cubic spline may be computed using matlab’s spline routine.
  3. An experiment generates the data ti 0. 1 0. 2 0. 3 0. 4 0. 5 0. 6 0. 7 0. 8 0. 9 yi 1. 143 1. 168 1. 226 1. 274 1. 289 1. 366 1. 367 1. 471 1. 453 Find the coefficients a 0 and a 1 of the best-fit line of the form y = a 0 + a 1 t to the data. Plot the data and the best-fit line on the same graph.

Problem 1

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.

Problem 2

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.

Problem 3

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.

Problem 4: Computer Problem 7.4, page 337

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.

Problem 5

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.

  • 1.10.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.