HW2 Solution

Math 578: Assignment 2
13. Determine whether the natural cubic spline that interpolates the table is or is not the
x
y
function
0 1
1 1
2
0
3
10

3
x ∈ [0, 1]

1 + x − x
2
3
f (x) = 1 − 2(x − 1) − 3(x − 1) + 4(x − 1) x ∈ [1, 2]

 4(x − 2) + 9(x − 2)2 − 3(x − 2)3
x ∈ [2, 3]
Solution: Yes. Check conditions as follows.
Define

3
:= S0 (x) x ∈ [0, 1]

1 + x − x
2
3
f (x) = 1 − 2(x − 1) − 3(x − 1) + 4(x − 1) := S1 (x) x ∈ [1, 2]


4(x − 2) + 9(x − 2)2 − 3(x − 2)3
:= S2 (x) x ∈ [2, 3]
Hence,
and

2
x ∈ [0, 1]

 1 − 3x
− 2 − 6(x − 1) + 12(x − 1)2 x ∈ [1, 2]
f 0 (x) =

 4 + 18(x − 2) − 9(x − 2)2
x ∈ [2, 3]

x ∈ [0, 1]

 − 6x
00
− 6 + 24(x − 1) x ∈ [1, 2]
f (x) =

 18 − 18(x − 2)
x ∈ [2, 3]
Therefore,
S0 (1) = 1 = S1 (1) S1 (2) = 0 = S2 (2)
S00 (1) = −2 = S10 (1) S1 (2) = 4 = S20 (2)
S000 (1) = −6 = S100 (1) S100 (2) = 18 = S200 (2).
Besides,
f 00 (0) = f 00 (3) = 0.
Hence, function f is the natural cubic spline of the given table.
1
19. Find a natural cubic spline function whose knots are −1, 0 and 1 and that takes these
values:
x
y
-1
5
0
7
1
9
Solution:
Suppose the natural cubic spline function has the form
(
S0 (x) x ∈ [−1, 0]
f (x) =
S1 (x)
x ∈ [0, 1]
Therefore,
S000 (−1) = S100 (1) = 0 = z0 = z2 .
Assuming that
S0 (0) = z1 ,
and from the equation
hi−1 zi−1 + 2(hi−1 + hi )zi + hi zi+1 = b(bi − bi−1 )
where bi =
yi+1 −yi
hi
and i = 1, we can get
z1 = 0.
Hence,
S0 (x) = Ax + B, S1 (x) = Cx + D.
Substituting
S0 (−1) = 5, S0 (0) = 7, S1 (0) = 7, S1 (1) = 9
into the above equations, we have
S0 = S1 = 2x + 7.
Therefore,
f (x) = 2x + 7
is the natural cubic spline whose knots and values are given in the table.
2
37. The first U.S. postage stamp was issued in 1885, with the cost to mail a letter set at 2
cents. In 1917, the cost was raised to 3 cents but then was returned to 2 cents in 1919. In 1932,
it was upped to 3 cents again, where it remained for 26 years. Then a series of increases took
place as follows: 1958 = 4 cents, 1963 = 5 cents, 1968 = 6 cents, 1971 = 8 cents, 1974 = 10
cents, 1978 = 15 cents, 1981 = 18 cents in March and 20 cents in October, 1985 = 22 cents,
1988 = 25 cents, 1991 = 29 cents, 1995 = 32 cents, 1999 = 33 cents, 2001 = 34 cents,
2002 = 37 cents, 2006 = 39 cents, 2007 = 41 cents, 2008 = 42 cents.
(1) Determine the Newton interpolation polynomial for these data.
(2) Determine the natural cubic spline for these data.
(3) Using both results, to answer the questions: when will it cost 50 cents to mail a letter?
Currently, the cost is 44 cents. What would each of these two types of interpolation predict?
Solution: All codes are in the appendix.
(1)Suppose
x = [1885 1917 1919 1932 1958 · · · 2008],
y = [2 3 2 3 4 · · · 42].
Based on divided difference algorithm, Newton interpolation polynomial is
p(x) = d1 + d2 (x − 1885) + d2 (x − 1885)(x − 1917) + · · · + d22 (x − 1885) · · · (x − 2007),
where the coefficients are
[d1 , d2 , · · · , d22 ] = [2, 0.0313, −0.0156, 0.0012, −2.8944 · 10−5 · · · 2.2085 · 10−25 ].
(Details are in the algorithm.) The graph is
13
18
Newton interpolation polynomial
x 10
16
14
12
10
8
6
4
2
0
−2
1880
1900
1920
1940
1960
1980
2000
2020
Figure 1: Newton interpolation polynomial.
3
(2) The second derivatives at each point are:
z = [0, −0.0508, 0.1351, −0.0378, 0.0369, −0.0672, 0.2317, −0.1906, 0.5307, −0.8394, 2.5557,
−2.7809, 0.9077, −0.0001, −0.2404, −0.0335, −0.3758, 3.0715, −2.6773, 2.1753, −2.0438, 0].
And the exact form of the cubic spline is calculated in the algorithm by formula (5) in our notes.
Here is the graph.
45
40
35
30
25
20
15
10
5
0
1880
1900
1920
1940
1960
1980
2000
2020
Figure 2: Interpolation using natural cubic spline.
(3)
Find when will it cost 50 cents
90
80
70
60
50
40
30
20
2007
2007.5
2008
2008.5
Figure 3: Find the time for cost of 50 cents using Newton interpolation polynomial.
From the figure, it can be seen that it will cost 50 cents in 2008.07, that is at the end of the
first month of 2008. Since currently the cost is 44 cents, this prediction is not good.
4
Find the time for the cost of 50 cents using cubic spline
54
52
50
48
46
44
42
40
2007
2007.5
2008
2008.5
2009
2009.5
2010
2010.5
2011
Figure 4: Find the time for cost of 50 cents using natural cubic spline.
From Fig. 4, it can be seen that it will cost 50 cents in 2010.65, that is at the end of July in
2010. However, since currently the cost is 44 cents while the cost is about 53 cents in the figure,
this prediction is also not very good.
5
16. Using Taylor series expansions, derive the error term for the formula
f 00 (x) ≈
1
[f (x) − 2f (x + h) + f (x + 2h)].
h2
Proof. For h small enough, we have
f (x + h) = f (x) + f 0 (x)h +
f (x + 2h) = f (x) + 2f 0 (x)h +
f 00 (x) 2 f 000 (ξ1 ) 3
h +
h
2
6
f 00 (x)
f 000 (ξ2 )
(2h)2 +
(2h)3
2
6
where ξ1 ∈ (x, x + h) and ξ2 ∈ (x, x + 2h).
Hence, substituting the above two equations in to the following formula, we have
f (x) − 2f (x + h) + f (x + 2h)
− f 00 (x)
2
h
f 000 (ξ1 ) 3 4f 000 (ξ2 ) 3
1 00
= 2 [f (x)h2 − 2f 00 (x)h2 −
h +
h ] − f 00 (x)
h
3
3
f 000 (ξ1 )
4f 000 (ξ2 )
=−
h+
h = O(h).
3
3
Hence,
f 000 (ξ1 )
4f 000 (ξ2 )
h+
h
3
3
is the error term for the approximation.
−
12. Show how to use Richardson extrapolation if
L = ϕ(h) + a1 h + a3 h3 + a5 h5 + · · ·
.
Solution: Denote
L = ϕ(h) + a1 h + a3 h3 + a5 h5 + · · · := N1 (h) + O(h).
(0.1)
Using h/2 instead of h in the above equation, we get
L = ϕ(h/2) + a1 h/2 + a3 (h/2)3 + a5 (h/2)5 + · · ·
(0.2)
Multiplying (0.2) by 2 and subtracting (0.1), we obtain
L = 2ϕ(h/2) − ϕ(h) + O(h3 ) := N2 (h) + O(h3 ).
Hence
N2 (h) = N1 (h/2) − N1 (h) = N1 (h/2) +
6
N1 (h/2) − N1 (h)
2−1
(0.3)
where N1 (h) = ϕ(h).
Apply Richardson extrapolation on (0.3), we get:
L=
23 N2 (h/2) − N2 (h)
+ O(h5 ) := N3 (h) + O(h5 ).
23 − 1
Hence,
N3 (h) = N2 (h/2) +
N2 (h/2) − N2 (h)
.
23 − 1
So on and so on, we conclude:
Nj (h) = Nj−1 (h/2) +
Nj−1 (h/2) − Nj−1 (h)
, (j ≥ 2)
22j−3 − 1
where N1 (h) = ϕ(h).
7
(0.4)
Appendixes
% D e t e r m i n e Newton i n t e r p o l a t i o n p o l y n o m i a l u s i n g d i v i d e d d i f f e r e n c e .
x =[ 18 85 1917 1919 1932 1958 1963 1968 1971 1974 1978 1 9 8 1 + 1 / 6 . . .
1 9 8 1 + 3 / 4 1985 1988 1991 1995 1999 2001 2002 2006 2007 2 0 0 8 ] ;
y =[ 2
3
2
3
4
5
6
8
10
15
18 20
...
22
25
29
32
33
34
37
39
41 4 2 ] ;
n= l e n g t h ( x ) ;
% coefficients
d=y ;
for j =2: n ;
f o r i =n : −1: j ;
d ( i ) = ( d ( i )−d ( i − 1 ) ) / ( x ( i )−x ( i −j + 1 ) ) ;
end ;
end ;
%Newton i n t e r p o l a t i o n p o l y n o m i a l
t =1885:2008;
p=d ( n ) ∗ o n e s ( s i z e ( t ) ) ;
f o r i =n −1: −1:1
p = ( t −x ( i ) ∗ o n e s ( s i z e ( t ) ) ) . ∗ p+d ( i ) ;
end
plot ( x , y , ’ ro ’ )
h o l d on
plot ( t , p)
t i t l e ( ’ Newton i n t e r p o l a t i o n p o l y n o m i a l ’ )
%−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
%N a t u r a l c u b i c s p l i n e
x =[ 18 85 1917 1919 1932 1958 1963 1968 1971 1974 1978 1 9 8 1 + 1 / 6 . . .
1 9 8 1 + 3 / 4 1985 1988 1991 1995 1999 2001 2002 2006 2007 2 0 0 8 ] ;
y =[ 2
3
2
3
4
5
6
8
10
15
18
20 . . .
22
25
29
32
33
34
37
39
41 4 2 ] ;
n= l e n g t h ( x ) ;
h= z e r o s ( n − 1 ) ;
b= z e r o s ( n − 1 ) ;
f o r i = 1 : ( n −1)
h ( i ) = x ( i +1)−x ( i ) ;
b ( i ) = ( y ( i +1)−y ( i ) ) / h ( i ) ;
end
A= z e r o s ( n −2 ,n − 2 ) ;
f o r i = 1 : n−3
8
A( i , i ) = 2 ∗ ( h ( i ) + h ( i + 1 ) ) ;
A( i , i +1)= h ( i + 1 ) ;
A( i +1 , i ) = h ( i + 1 ) ;
end
A( n −2 ,n −2)=2∗( h ( n −2)+h ( n − 1 ) ) ;
C= z e r o s ( n − 2 , 1 ) ;
f o r i = 1 : n−2
C ( i ) = 6 ∗ ( b ( i +1)−b ( i ) ) ;
end
z=A\C ;
z =[0; z ; 0];
plot ( x , y , ’ ro ’ )
h o l d on
f o r i = 1 : ( n −1)
t =x ( i ) : x ( i + 1 ) ;
s=zeros ( s i z e ( t ) ) ;
s =z ( i + 1 ) / ( 6 ∗ h ( i ) ) . ∗ ( t −x ( i ) ) . ˆ 3 + z ( i ) / ( 6 ∗ h ( i ) ) . ∗ ( x ( i +1)− t ) . ˆ 3 . . .
+ ( y ( i + 1 ) / h ( i )−h ( i ) ∗ z ( i + 1 ) / 6 ) . ∗ ( t −x ( i ) ) + ( y ( i ) / h ( i )−h ( i ) ∗ z ( i ) / 6 . . .
. ∗ ( x ( i +1)− t ) ;
plot ( t , s )
h o l d on
end
%−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
% D e t e r m i n e Newton i n t e r p o l a t i o n p o l y n o m i a l u s i n g d i v i d e d d i f f e r e n c e .
x =[ 18 85 1917 1919 1932 1958 1963 1968 1971 1974 1978 1 9 8 1 + 1 / 6 . . .
1 9 8 1 + 3 / 4 1985 1988 1991 1995 1999 2001 2002 2006 2007 2 0 0 8 ] ;
y =[ 2
3
2
3
4
5
6
8
10
15
18 20
...
22
25
29
32
33
34
37
39
41 4 2 ] ;
n= l e n g t h ( x ) ;
% coefficients
d=y ;
for j =2: n ;
f o r i =n : −1: j ;
d ( i ) = ( d ( i )−d ( i − 1 ) ) / ( x ( i )−x ( i −j + 1 ) ) ;
end ;
end ;
%Newton i n t e r p o l a t i o n p o l y n o m i a l
t= 2007:0.01:2008.25
%1 8 8 5 : 2 0 0 8 ;
p=d ( n ) ∗ o n e s ( s i z e ( t ) ) ;
f o r i =n −1: −1:1
9
p = ( t −x ( i ) ∗ o n e s ( s i z e ( t ) ) ) . ∗ p+d ( i ) ;
end
% p l o t ( x , y , ’ ro ’ )
% h o l d on
plot ( t , p , ’ r ’ )
g r i d on ;
t i t l e ( ’ F i n d when w i l l i t c o s t 50 c e n t s ’ )
%−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
%N a t u r a l c u b i c s p l i n e
x =[ 18 85 1917 1919 1932 1958 1963 1968 1971 1974 1978 1 9 8 1 + 1 / 6 . . .
1 9 8 1 + 3 / 4 1985 1988 1991 1995 1999 2001 2002 2006 2007 2 0 0 8 ] ;
y =[ 2
3
2
3
4
5
6
8
10
15
18
20 . . .
22
25
29
32
33
34
37
39
41 4 2 ] ;
n= l e n g t h ( x ) ;
h= z e r o s ( n − 1 ) ;
b= z e r o s ( n − 1 ) ;
f o r i = 1 : ( n −1)
h ( i ) = x ( i +1)−x ( i ) ;
b ( i ) = ( y ( i +1)−y ( i ) ) / h ( i ) ;
end
A= z e r o s ( n −2 ,n − 2 ) ;
f o r i = 1 : n−3
A( i , i ) = 2 ∗ ( h ( i ) + h ( i + 1 ) ) ;
A( i , i +1)= h ( i + 1 ) ;
A( i +1 , i ) = h ( i + 1 ) ;
end
A( n −2 ,n −2)=2∗( h ( n −2)+h ( n − 1 ) ) ;
C= z e r o s ( n − 2 , 1 ) ;
f o r i = 1 : n−2
C ( i ) = 6 ∗ ( b ( i +1)−b ( i ) ) ;
end
z=A\C ;
z =[0; z ; 0];
% p l o t ( x , y , ’ ro ’ )
% h o l d on
i =n −1;
t =2007:0.1:2011;
s=zeros ( s i z e ( t ) ) ;
s =z ( i + 1 ) / ( 6 ∗ h ( i ) ) . ∗ ( t −x ( i ) ) . ˆ 3 + z ( i ) / ( 6 ∗ h ( i ) ) . ∗ ( x ( i +1)− t ) . ˆ 3 . . .
+( y ( i + 1 ) / h ( i )−h ( i ) ∗ z ( i + 1 ) / 6 ) . ∗ ( t −x ( i ) ) + ( y ( i ) / h ( i )−h ( i ) ∗ z ( i ) / 6 ) . ∗ ( x ( i +1)− t
plot ( t , s )
10
g r i d on
t i t l e ( ’ F i n d t h e t i m e f o r t h e c o s t o f 50 c e n t s u s i n g c u b i c s p l i n e ’ )
11