Docsity

Numerical Computing Homework 5: Interpolation and Polynomial Fitting - Prof. Ashwani K. Ka, Assignments of Computer Science

Toggle sidebar
bg1
Numerical Computing S’09 MATH/CSCI 4800-01
Homework-5
Due in class on Monday March 23, 2009
Illegible reports will be returned without grading. Show all work. All Matlab scripts, Matlab output and
plots should be printed out and stapled to the report.
1. (Pencil-and-paper)
(a) Define natural, clamped and not-a-knot cubic splines.
(b) Check whether the following function is a natural cubic spline. If it is not, then indicate the
reason for the breakdown.
S(x) =
28 + 25x+ 9x2+x3x∈[−3,−1]
26 + 19x+ 3x2−x3x∈[−1,0]
26 + 19x+ 3x2−2x3x∈[0,3]
−163 + 208x−60x2+ 5x3x∈[3,4]
(a) A cubic spline is a piecewise cubic interpolant to discrete data (xi,yi), i = 1,2, . . . n. It consists
of a different cubic polynomial on each of the n−1 subintervals and may be defined as follows.
si(x) = aix3+bix2+c1x+di, xi≤x≤xi+1, i = 1,2,...n−1.
The 4n−4 coefficients appearing above are found by the following conditions.
i. The ninterpolation conditions,
si(xi) = yi, i = 1,2,...n−1, sn−1(xn) = yn.(1)
ii. The 3(n−2) continuity conditions at the interior nodes,
si−1(xi) = si(xi), s0
i−1(xi) = s0
i(xi), s00
i−1(xi) = s00
i(xi), i = 2,3,...n−1.(2)
iii. Two additional conditions depending upon the type of spline.
The natural spline satisfies zero curvature conditions at the ends, i.e.,
s00
1(x1) = s00
n−1(xn)=0.(3)
The clamped spline satisfies given slopes at the ends, i.e.,
s0
1(x1) = α, s0
n−1(xn) = β.
The not-a-knot spline satisfies
s000
1(x2) = s000
2(x2), s000
n−2(xn−1) = s000
n−1(xn−1).
(b) First, note that we have the cubics
s1(x) = 28 + 25x+ 9x2+x3, x ∈[−3,−1]
s2(x) = 26 + 19x+ 3x2−x3, x ∈[−1,0]
s3(x) = 26 + 19x+ 3x2−2x3, x ∈[0,3]
s4(x) = −163 + 208x−60x2+ 5x3, x ∈[3,4].
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Numerical Computing Homework 5: Interpolation and Polynomial Fitting - Prof. Ashwani K. Ka and more Assignments Computer Science in PDF only on Docsity!

Numerical Computing S’09 MATH/CSCI 4800- Homework-

Due in class on Monday March 23, 2009

Illegible reports will be returned without grading. Show all work. All Matlab scripts, Matlab output and plots should be printed out and stapled to the report.

  1. (Pencil-and-paper)

(a) Define natural, clamped and not-a-knot cubic splines. (b) Check whether the following function is a natural cubic spline. If it is not, then indicate the reason for the breakdown.

S(x) =

28 + 25x + 9x^2 + x^3 x ∈ [− 3 , −1] 26 + 19x + 3x^2 − x^3 x ∈ [− 1 , 0] 26 + 19x + 3x^2 − 2 x^3 x ∈ [0, 3] −163 + 208x − 60 x^2 + 5x^3 x ∈ [3, 4]

(a) A cubic spline is a piecewise cubic interpolant to discrete data (xi, yi), i = 1, 2 ,... n. It consists of a different cubic polynomial on each of the n − 1 subintervals and may be defined as follows.

si(x) = aix^3 + bix^2 + c 1 x + di, xi ≤ x ≤ xi+1, i = 1, 2 ,... n − 1.

The 4n − 4 coefficients appearing above are found by the following conditions. i. The n interpolation conditions,

si(xi) = yi, i = 1, 2 ,... n − 1 , sn− 1 (xn) = yn. (1)

ii. The 3(n − 2) continuity conditions at the interior nodes,

si− 1 (xi) = si(xi), s′ i− 1 (xi) = s′ i(xi), s′′ i− 1 (xi) = s′′ i (xi), i = 2, 3 ,... n − 1. (2)

iii. Two additional conditions depending upon the type of spline. The natural spline satisfies zero curvature conditions at the ends, i.e.,

s′′ 1 (x 1 ) = s′′ n− 1 (xn) = 0. (3)

The clamped spline satisfies given slopes at the ends, i.e.,

s′ 1 (x 1 ) = α, s′ n− 1 (xn) = β.

The not-a-knot spline satisfies

s′′′ 1 (x 2 ) = s′′′ 2 (x 2 ), s′′′ n− 2 (xn− 1 ) = s′′′ n− 1 (xn− 1 ).

(b) First, note that we have the cubics

s 1 (x) = 28 + 25x + 9x^2 + x^3 , x ∈ [− 3 , −1] s 2 (x) = 26 + 19x + 3x^2 − x^3 , x ∈ [− 1 , 0] s 3 (x) = 26 + 19x + 3x^2 − 2 x^3 , x ∈ [0, 3] s 4 (x) = −163 + 208x − 60 x^2 + 5x^3 , x ∈ [3, 4].

We shall also need the first two derivatives:

s′ 1 (x) = 25 + 18x + 3x^2 , x ∈ [− 3 , −1] s′ 2 (x) = 19 + 6x − 3 x^2 , x ∈ [− 1 , 0] s′ 3 (x) = 19 + 6x − 6 x^2 , x ∈ [0, 3] s′ 4 (x) = 208 − 120 x + 15x^2 , x ∈ [3, 4],

and

s′′ 1 (x) = 18 + 6x, x ∈ [− 3 , −1] s′′ 2 (x) = 6 − 6 x, x ∈ [− 1 , 0] s′′ 3 (x) = 6 − 12 x, x ∈ [0, 3] s′′ 4 (x) = −120 + 30x, x ∈ [3, 4].

Now consider the conditions (1)-(3) above which must be satisfied if S is a cubic spline. Inter- polation conditions (1) are irrelevant because we are not given any yi. Consider the continuity conditions (2) at the interior nodes; the following equations confirm that these conditions are satisfied. s 1 (−1) = 11 = s 2 (−1), s 2 (0) = 26 = s 3 (0), s 3 (3) = 56 = s 4 (3), s′ 1 (−1) = 10 = s′ 2 (−1), s′ 2 (0) = 19 = s′ 3 (0), s′ 3 (3) = −17 = s′ 4 (3), s′′ 1 (−1) = 12 = s′′ 2 (−1), s′′ 2 (0) = 6 = s′′ 3 (0), s′′ 3 (3) = −30 = s′′ 4 (3). Now consider the natural spline conditions (3):

s′′ 1 (−3) = 0, s′′ 4 (4) = 0.

As these conditions hold, S(x) is a natural cubic spline.

  1. (Pencil-and-paper and Matlab) In class we examined the Chebyshev interpolation procedure of con- structing polynomial interpolants to a function; see section 3.3 of the Text as well as class notes. Assume that the Chebyshev procedure is to be used to construct a 5-th degree interpolant p(x) to f (x) = ln x on the interval [2, 6].

(a) Write down the (x, y) points that will serve as the interpolation nodes for p(x). (b) Write a Matlab script to compute p(x) and plot a graph showing both p and f on the interval [2, 6]. (You may use the Matlab commands polyfit and polyval.) Comment on the eyeball- accuracy of your result. (c) Find, analytically, the upper bound on the interpolation error | ln x − p(x)| that is valid for all x in [2, 6]. (d) Based upon the above estimate, how many digits will be correct when p(4) is used to approximate ln 4? Check your result against the exact answer and comment.

(a) The x -coordinates of the Chebyshev nodes for n = 6 and for the interval [a, b] are

xi = a + b 2

b − a 2

cos (2i − 1)π 12

, i = 1, 2 ,... 6.

When specialized to a = 2 and b = 6 we get

xi = 4 + 2 cos (2i − 1)π 12

, i = 1, 2 ,... 6.

The corresponding y -coordinates are yi = ln xi.

(c) See attached Matlab Log. It appears that the data is best represented by two straight line segments, which makes the piecewise linear interpolant the best choice. Both the polynomial interpolant and the spline interpolant lend some un-necessary curvature to the trend, making them both unsatisfactory.

  1. (Pencil-and-paper and Matlab)

(a) Solve Text Problem 2a, Exercises 4.1, page 204 by hand, using the procedure outlined in Example 4.4 on page 200. (b) (Extra Credit) Solve the above problem by hand by using the QR decomposition. This will require adding an extra column to the 4 × 3 matrix, employing Gram-Schmidt orthogonalization to generate Q and R, and then using the procedure outlined on page 222 to solve the least- squares problem. (c) Solve the problem with Matlab by using the QR decomposition. You may find the code segment given in Example 4.15 on page 223 helpful.

(a) The least-squares problem is Ax = b, i.e.,    

x 1 x 2 x 3

Multiplying both sides of the above equation by AT^ leads to the following normal equations  

x 1 x 2 x 3

whose solution is

x =

x 1 x 2 x 3

The residual vector is r = b − Ax, i.e.,

r =

The root mean squared error is RMSE =

rTr = 1/

(b) We follow the procedure outlined in class; see also section 4.3 and Examples 4.12-4.14 in the Text (pages 218-222). First, we need to add a fourth column to A, linearly independent of the other three columns of A, so that a QR-factorization can be performed. Thus the enhanced version of A is taken to be

A^ ˆ =

We denote by vi the columns of A.ˆ The QR-factorization will be obtained via the Gram-Schmidt process. We begin by setting

y 1 = v 1 =

Then r 11 = ||y 1 || =

12 + 0^2 + 1^2 + 1^2 =

3 , and the first unit vector is

q 1 =

y 1 ||y 1 ||

To find the second unit vector we set

y 2 = v 2 − r 12 q 1

where

r 12 = (qT 1 v 2 ) =

[1 0 1 1]

 =^

Therefore,

y 2 =

With r 22 = ||y 2 || =

3 , the second unit vector is

q 2 =

y 2 ||y 2 ||

For the third unit vector we define

y 3 = v 3 − r 13 q 1 − r 23 q 2

where

r 13 = (qT 1 v 3 ) =

[1 0 1 1]

 =^

and

r 23 = (qT 2 v 3 ) =

[0 1 1 − 1]

 =^

Therefore,

y 3 =

 −^

 −^

With r 33 = ||y 3 || = 2 /

3 , the third unit vector is

q 3 = y 3 ||y 3 ||

The original (inconsistent) problem Ax = b can now be written as QRx = b, or Rx = Q−^1 b, or Rx = QTb since Q−^1 = QT. On substituting the entries, Rx = QTb becomes    

x 1 x 2 x 3

 = √^1

We now ignore the last row in R and QTb and solve the resulting upper triangular system to get the least-squares solution

x =

x 1 x 2 x 3

This solution agress with that obtained in part (a) above. (c) See Matlab Log.

  1. (Extra Credit) Text Problem 5, Computer Problems 4.1, page 205. You may use any method to obtain the least-squares fit, including appropriate Matlab commands. See Matlab Log. The best-fit line is found to be S = c 1 + c 2 P, where c 1 = 9510 and c 2 = − 8314. The profit is given by F = (P − 0 .23)(c 1 + c 2 P ). It is maximized at F ′(P ) = 0, yielding P = 0. 69.

HW 5, Problem 2

(b) The Matlab script is given below.

% Script File P2.m

% Interpolates log(x) with Chebyshev polynomial of degree 5

% on the interval [2,6]

% Demonstrates the use of polyfit, polyval, spline and ppval

% Provide data

clc

a=2;b=6;n=6;

% Chebyshev points

for i=1:n

x(i)=(b+a)/2+((b-a)/2)cos(((2i-1)pi)/(2n));

end

% Reverse the points so that they are in ascending order

for j=1:n

x1(j)=x(n-j+1);

end

y=log(x1);

% Compute polynomial interpolant

p=polyfit(x1,y,n-1);

% Plot data and interpolant

xx=linspace(2,6);

yy=polyval(p,xx);

zz=log(xx);

figure

plot(x1,y,'ro',xx,yy,'g','LineWidth',2);

hold on

plot(xx,zz,'b','LineWidth',2);

legend('Data','Interpolant','Function');

xlabel('x'); ylabel('y');

exact=log(4);

approx=polyval(p,4);

relerror=(exact-approx)/exact;

fprintf('exact= %9.6e, approx= %9.6e, relerror= %9.4e

\n',exact,approx,error);

Numerical Output

exact= 1.386294e+

approx= 1.386184e+

relerror= 7.9925e-

HW 5, Problem 3

The Matlab scripts are given below.

% Script HW5prob3.m

clc

% Supply data

x=[0:10];

y=[32 4 5.6 7.1 8.4 10.1 11.5 13.1 14.4 16.1 17.5];

n=length(x); m=length(y);

if (m~=n),error('x and y are incompatible');end

% Call newtdd for coefficients of polynomial interpolant

c=newtdd(x,y,n);

% Evaluate Newton interpolant for plotting by using nested

product

t=linspace(0,10,200);

p=c(n);

for k=n-1:-1:

p=(t-x(k)).*p+c(k);

end

% Plot data, linear interpolant (lines joining data points) and

the Newton

% interpolant

plot(x,y,'ro','MarkerSize',10,'MarkerFaceColor','r')

hold on

plot(x,y,'b--','LineWidth',2)

hold on

plot(t,p,'k','LineWidth',2)

hold on

xlabel('x'); ylabel('y');

% Generate and plot the spline interpolant using Matlab's spline

command

ss=spline(x,y,t);

plot(t,ss,'g','LineWidth',2);

legend('data','linear','Newton','spline')

% Matlab function newtdd

% Computes coefficients of the Newton interpolant as

% divided differences

function c=newtdd(x,y,n)

% Input:

% n-vectors x and y representing the data

% Output:

% n-vector c of divided differences

% Fill in the y column of the Newton triangle

v(1:n,1)=y(1:n);

for j=2:n % for column j

for i=1:n+1-j % fill in column from top to bottom

v(i,j)=(v(i+1,j-1)-v(i,j-1))/(x(i+j-1)-x(i));

end

end

c(1:n)=v(1,1:n); % select top row for output coefficients

HW 5, Problem 4(c)

Matlab Script

% Script File HW5P4.m

% Uses QR factorization to solve a least-squares problem

clc

% Provide matrices A and b

A=[1 1 0;0 1 1;1 2 1;1 0 1];

b=[2;2;3;4];

% QR decomposition of A

[Q,R]=qr(A); % command qr

disp('A');

disp (A);

disp('R');

disp (R);

% Vector bhat

bhat=Q'*b;

% Solve the 3x3 block of R*x=bhat

x=R(1:3,1:3)\bhat(1:3);

disp ('x');

disp(x);

Numerical Output

A

R

x

HW 5, Problem 5

Matlab Log (Matlab command polyfit used to determine best fit.)

% Script File HW5P5.m

% Uses polyfit to fit polynomial to data

% Provide data as columns

clc

P=[0.59 0.80 0.95 0.45 0.79 0.99 0.90 0.65 0.79 0. ...

0.99 0.85]';

S=[3980 2200 1850 6100 2100 1700 2000 4200 2440 3300 ...

2860 1920 2160]';

% Check for data dimension consistency

N=length(P); M=length(S);

if N ~=M, error('x and y do not have the same dimensions'); end

% Fit to a polynomial of degree p

p=1;

% plot data and fit

xx=linspace(min(P),max(P));

figure

% Use of polyfit

pp=polyfit(P,S,p);

yyy=polyval(pp,xx);

plot(P,S,'ro','MarkerSize',10,'MarkerFaceColor','b');

hold on

plot(xx,yyy,'LineWidth',2);

legend('Data', 'Fit');

c1=pp(2);c2=pp(1);

disp('Fit is S=c1+c2*P')

fprintf('c1= %10.4e, c2= %10.4e \n',c1,c2);

Popt=(0.23c2-c1)/(2c2);

fprintf('Optimal Price= %4.2f \n',Popt);

Numerical Output

Fit is S=c1+c2*P

c1= 9.5101e+03, c2= -8.3144e+

Optimal Price= 0.