﻿ 17074192 作业七 Interpolation&&Least Squares - 计算数学达人 - 专，学者，数值代数，微分方程数值解

作业七

EXERCISE3.4

1. Decide whether the equations form a cubic spline.

S(x)=2*x^3+x^2+4*x+5,when 0<=x<=1 and S(x)=(x-1)^3+7*(x-1)^2+12*(x-1)+12, when 1<=x<=2;

Solution:

Denote S(x)=S1(x) when 0<=x<=1 and S(x)=S2(x) when 1<=x<=2.

We can get that when x=1, S1(1)=S2(1)=12S1’(1)=S2’(1)=12 and S1”(1)=S2”(1)=14.

Consequently, these equations satisfy the properties, so they form a cubic spline.

2.Solve equations (3.24) to find the natural cubic spline through the three points (a)(0,0), (1,1), (2,4);  (b)(-1,1),(1,1),(2,4).

Solution:

Let w(i) denote y(i+1)-y(i) and v(i) denote x(i+1)-x(i) for i=1,2,…,n-1.b(i), c(i) and d(i) are the unknowns, and we already have known that d(i)=(c(i+1)-c(i))/(3v(i)), b(i)=w(i)/v(i)-v(i)*(2*c(i)+c(i+1))/3.

(a) From the conditions we can have that w(1)=w(2)=1 and v(1)=1v(2)=3;

Then we can solve the unknown c by equations A*c=[0;6;0], where the coefficient matrix A is [1,0,0;1,4,0;0,0,1]. Then by the calculation we get the c is [0, 3/2, 0] ,then b=[1/2,2] and d=[1/2,-1/2].

Consequently, the natural cubic spline through the three points is S1(x)=x/2+x^2/2 when 0<=x<=1 and S2(x)=1+2*(x-1)-(x-1)^2/2, when 1<=x<=2;

(b) From the condition we can have w(1)=0,w(2)=3 and v(1)=2,v(2)=1;

The by the formula we can solve that c=[0,3/2,0] ,d=[1/4,1/2]and b=[-1/2,2]; that is the natural cubic splines is S1(x)=1-x/2+x^2/4 when -1<=x<=1 and S2(x)=1+2*(x-1)-(x-1)^2/2, when 1<=x<=2;

COMPUTER3.4

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

Solution:

The code of the solution is:

function coeff=splinecoeff(x,y)

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

A=zeros(n,n);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

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

end

A(1,1)=1;A(n,n)=1;

coeff=zeros(n,3);

coeff(:,2)=Ar;

for i=1:n-1

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 [xl,yl]=splinepolt(x,y,k)

n=length(x);

coeff=splinecoeff(x,y);

xl=[];yl=[];

for i=1:n-1

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

dx=xs-x(i);

ys=coeff(i,3)*dx;

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

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

xl=[xl;xs(1:k)'];yl=[yl;ys(1:k)'];

end

xl=[xl;x(end)];yl=[yl;y(end)];

plot(x,y,'o',xl,yl);

%解决问题的代码：

xa=[0 1 2 3];xb=[-1 0 3 4 5];

ya=[3 5 4 1];yb=[3 5 1 1 1];

% splinepolt(xa,ya,100);

splinepolt(xb,yb,100)

Chapter Four

EXERCISE4.2

1.Fit data to the periodic model y=F3(t)=c1+c2cos(2*pi*t)+c3*sin(2*pi*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) From the data we can get that: [1 1 0;1 0 1;1 -1 0;1 0 -1][c1;c2;c3]=[1;3;2;0]. Then we denote the equation systems as A*c=b. By the method of Normal equations, we can get the least squares of the system is c=[3/2; -1/2; 3/2];

The 2-norm error is 0; the RMSE is 0.

(b) From the data and the model we can get the equation systems:

[1 1 0; 1 0 1;1 -1 0;1 0 -1]*[c1;c2;c3]=[1;3;2;1]. Then by the method of normal equations we can get the least squares of the system is c = [7/4;-1/2; 1];

The 2-norm error is 1/2;

The RMSE is 1/4;

(c)From the data and the model we can get the equation systems:

[1 1 0;1 -1 0;1 1 0;1 -1 0]*[c1;c2;c3]=[3;1;3;2]; Then by the method of normal equations we can get the least squares of the system is c=[9/4;3/4;coe],where coe is a real number ;

The 2-norm error is sqrt(2)/2;

The RMSE is sqrt(2)/4.

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^(c2*ti).

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

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

Solution:

(a) Denote k=ln(c1), then from the data and the model we can get the following system:

[1 1 1 1;-2 0 1 2]’*[k;c2]=[0;ln2;ln2;ln5].

The 2-norm error is 0.4052;

The RMSE is 0.2026

(b) Denote k=ln(c1), then from the data and the model we can get the following system:

[1 1 1 1;0 1 1 2]’*[k;c2]=[0;0;ln2;ln4];

The 2-norm error is 0.6003;

The RMSE is 0.3001;

COMPUTER4.2

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:

month oil use (106 bbl/day)

mouth:      Jan    Feb   Mar   Apr   May    Jun    Jul    Aug   Sep    Oct   Nov   Dec

oil use(10^6): 6.224  6.665  6.241  5.302  5.073  5.127  4.994  5.012  5.108  5.377  5.510  6.372

Solution:

The model 4.9 is y=c1+c2*cos(2*pi*t)+c3*sin(2*pi*t)+c4*cos(4*pi*t);

The code is:

t=linspace(1,12,12);n=12;

y=10^6.*[6.224 6.665 6.241 5.302 5.073 5.127 4.994 5.012 5.108 5.377 5.510 6.372]';

A=zeros(n,4);

for i=1:n

A(i,1)=1;

A(i,2)=cos(2*pi*t(i));

A(i,3)=sin(2*pi*t(i));

A(i,4)=cos(4*pi*t(i));

end

c=(A'*A)(A'*y);

>>c=[1.0e+20 *; NaN; NaN; 0;-7.9096]

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.

year       1960         1970         1990         2000

population  3039585530   3707475887   5281653820   6079603571

Solution: