﻿ 17074225顾涛chapter3-4+4-2 - 计算数学达人 - 专，学者，数值代数，微分方程数值解

### 17074225顾涛chapter3-4+4-2

3.4 Interpolation

1.Decide whether the equations form a cubic spline.

(b)

S1(x) =2x^3 + x^2 + 4x + 5on [0,1]

S2(x) =(x 1)3 + 7(x 1)2 + 12(x 1) + 12on [1,2]

satises all cubic spline properties for the data points (0, 1),(1, 2)

7.Solve equations (3.24) to nd the natural cubic spline through

the three points

(a) (0,0), (1,1), (2,4) (b) (1,1), (1,1), (2,4).

solution:

a)c1=0;c2=2/3;c3=0

b1=1/2;b2=2;

d1=1/2;d2=-1/2;

S1(x)=(1/2)x+(1/2)x^3

S2(x)=1+2(x-1)+(3/2)*(x-1)^2-(1/2)*(x-1)^3

b)c1=0;c2=2/3;c3=0

b1=-1;b2=2;

d1=1/4;d2=-1/2;

S1(x)=1-(x+1)+(1/4)*(x+1)^3

S2(x)=1+2(x-1)+(3/2)*(x-1)^2-(1/2)*(x-1)^3

computer problems:

1. Find the equations and plot the natural cubic spline that interpolates the data points

(a) (0, 3),(1, 5),(2, 4),(3, 1)

(b) (1, 3),(0, 5),(3, 1),(4, 1),(5, 1).

%Program 3.5 Calculation of spline coefficients

%Calculates coefficients of cubic spline

%Input: x,y vectors of data points

%plus two optional extra data v1, vn

%Output: matrix of coefficients b1,c1,d1;b2,c2,d2;...

function coeff=splinecoeff(x,y)

n=length(x);v1=0;vn=0;

A=zeros(n,n);% matrix A is nxn

r=zeros(n,1);

for i=1:n-1% define the deltas

dx(i)= x(i+1)-x(i); dy(i)=y(i+1)-y(i);

end

for i=2:n-1% load the A matrix

A(i,i-1:i+1)=[dx(i-1) 2*(dx(i-1)+dx(i)) dx(i)];

r(i)=3*(dy(i)/dx(i)-dy(i-1)/dx(i-1)); % right-hand side

end

% Set endpoint conditions

% Use only one of following 5 pairs:

A(1,1) = 1;% natural spline conditions

A(n,n) = 1;

%A(n,n)=2;r(n)=vn;

%A(1,1:2)=[2*dx(1) dx(1)];r(1)=3*(dy(1)/dx(1)-v1);%clamped

%A(n,n-1:n)=[dx(n-1) 2*dx(n-1)];r(n)=3*(vn-dy(n-1)/dx(n-1));

%A(1,1:2)=[1 -1];% parabol-term conditions, for n>=3

%A(n,n-1:n)=[1 -1];

%A(1,1:3)=[dx(2) -(dx(1)+dx(2)) dx(1)]; % not-a-knot, for n>=4

%A(n,n-2:n)=[dx(n-1) -(dx(n-2)+dx(n-1)) dx(n-2)];

coeff=zeros(n,3);

coeff(:,2)=Ar;% solve for c coefficients

for i=1:n-1% solve for b and d

coeff(i,3)=(coeff(i+1,2)-coeff(i,2))/(3*dx(i));

coeff(i,1)=dy(i)/dx(i)-dx(i)*(2*coeff(i,2)+coeff(i+1,2))/3;

end

coeff=coeff(1:n-1,1:3);

function [x1,y1]=splineplot(x,y,k)

n=length(x);

coeff=splinecoeff(x,y);

x1=[]; y1=[];

for i=1:n-1

xs=linspace(x(i),x(i+1),k+1);

dx=xs-x(i);

ys=coeff(i,3)*dx; % evaluate using nested multiplication

ys=(ys+coeff(i,2)).*dx;

ys=(ys+coeff(i,1)).*dx+y(i);

x1=[x1; xs(1:k)']; y1=[y1;ys(1:k)'];

end

x1=[x1; x(end)];y1=[y1;y(end)];

plot(x,y,'o',x1,y1)

4.2A SURVEY OF MODELS

1.Fit data to the periodic model

y = F3(t) = c1 + c2 cos 2πt + c3 sin 2πt.

Find the 2-norm error and the RMSE.

a)t:0 1/4 1/2 3/4 y:1 3 2 0

b)t:0 1/4 1/2 3/4 y:1 3 2 1

c)t:0 1/2   1   3/2 y:3 1 3 2

solution:

a)A=[1 1 0;1 0 1;1 -1 0;1 0 -1];b=[1;3;2;0]

C=[3/2;-1/2;3/2]

y=3/2-1/2 cos 2πt + 3/2sin 2πt.

r=[0;0;0;0]

||r||2=0 ;RMSE=0;

b)A=[1 1 0;1 0 1;1 -1 0;1 0 -1];b=[1;3;2;1]

C=[7/4;-1/2;3/2]

y=7/4-1/2 cos 2πt + 3/2sin 2πt.

r=[-1/4;1/4;1/4;1/4]

||r||2=1/2 ;RMSE=1/4;

c):A=[1 1 0;1 -1 0 ;1  1 0;1 -1 0];b=[3;1;3;2]

C=[9/4;3/4;3]

y=9/4-3/4 cos 2πt + 3sin 2πt.

r=[0;1/2;0;1/2]

||r||2=sqrt（1/2） ;RMSE=sqrt(1/8);

3.Fit data to the exponential model by using linearization.

Find the 2-norm of the difference between the data points yi

and the best model c1*e^c2ti .

a)t:-2 0 1 2 y:1 2 2 5

b)t:0 1 1 2 y:1 1 2 4

(a) y = 1.932e0.3615t, ||e||2 = 1.2825,

(b) y = 2t 1/4, ||e||2 = 0.9982

computer programs

1.Fit the monthly data for Japan 2003 oil consumption,

shown in the following table, with the periodic model (4.9),

and calculate the RMSE:

3..Consider the world population data of Computer Problem 3.1.1.

Find the best exponential t of the data points by using

linearization. Estimate the 1980 population, and nd the estimation

error.