Math 578: Assignment 2 13. Determine whether the natural cubic spline that interpolates the table is or is not the x 0 1 2 3 y 1 1 0 10 function 1 + x x 3 x [0, 1] f(x) = 1 2(x 1) 3(x 1) 2 + 4(x 1) 3 x [1, 2] 4(x 2) + 9(x 2) 2 3(x 2) 3 x [2, 3] Solution: Yes. Check conditions as follows. Define 1 + x x 3 := S 0 (x) x [0, 1] f(x) = 1 2(x 1) 3(x 1) 2 + 4(x 1) 3 := S 1 (x) x [1, 2] 4(x 2) + 9(x 2) 2 3(x 2) 3 := S 2 (x) x [2, 3] and Hence, Therefore, Besides, 1 3x 2 x [0, 1] f (x) = 2 6(x 1) + 12(x 1) 2 x [1, 2] 4 + 18(x 2) 9(x 2) 2 x [2, 3] 6x x [0, 1] f (x) = 6 + 24(x 1) x [1, 2] 18 18(x 2) x [2, 3] S 0 (1) = 1 = S 1 (1) S 1 (2) = 0 = S 2 (2) S 0(1) = 2 = S 1(1) S 1 (2) = 4 = S 2(2) S 0 (1) = 6 = S 1 (1) S 1 (2) = 18 = S 2 (2). f (0) = f (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 -1 0 1 y 5 7 9 Solution: Suppose the natural cubic spline function has the form { S0 (x) x [ 1, 0] f(x) = S 1 (x) x [0, 1] Therefore, Assuming that and from the equation S 0 ( 1) = S 1 (1) = 0 = z 0 = z 2. S 0 (0) = z 1, h i 1 z i 1 + 2(h i 1 + h i )z i + h i z i+1 = b(b i b i 1 ) where b i = y i+1 y i h i and i = 1, we can get z 1 = 0. Hence, S 0 (x) = Ax + B, S 1 (x) = Cx + D. Substituting S 0 ( 1) = 5, S 0 (0) = 7, S 1 (0) = 7, S 1 (1) = 9 into the above equations, we have S 0 = S 1 = 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 appix. (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) = d 1 + d 2 (x 1885) + d 2 (x 1885)(x 1917) + + d 22 (x 1885) (x 2007), where the coefficients are [d 1, d 2,, d 22 ] = [2, 0.0313, 0.0156, 0.0012, 2.8944 10 5 2.2085 10 25 ]. (Details are in the algorithm.) The graph is 18 x 1013 Newton interpolation polynomial 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 (3) Figure 2: Interpolation using natural cubic spline. 90 Find when will it cost 50 cents 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 of the first month of 2008. Since currently the cost is 44 cents, this prediction is not good. 4
54 Find the time for the cost of 50 cents using cubic spline 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 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 Proof. For h small enough, we have f (x) 1 [f(x) 2f(x + h) + f(x + 2h)]. h2 f(x + h) = f(x) + f (x)h + f (x) 2 h 2 + f (ξ 1 ) h 3 6 f(x + 2h) = f(x) + 2f (x)h + f (x) (2h) 2 + f (ξ 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 Hence, f(x) 2f(x + h) + f(x + 2h) h 2 f (x) = 1 h 2 [f (x)h 2 2f (x)h 2 f (ξ 1 ) 3 = f (ξ 1 ) 3 h + 4f (ξ 2 ) h = O(h). 3 f (ξ 1 ) h + 4f (ξ 2 ) h 3 3 is the error term for the approximation. h 3 + 4f (ξ 2 ) h 3 ] f (x) 3 12. Show how to use Richardson extrapolation if. Solution: Denote L = ϕ(h) + a 1 h + a 3 h 3 + a 5 h 5 + L = ϕ(h) + a 1 h + a 3 h 3 + a 5 h 5 + := N 1 (h) + O(h). (0.1) Using h/2 instead of h in the above equation, we get L = ϕ(h/2) + a 1 h/2 + a 3 (h/2) 3 + a 5 (h/2) 5 + (0.2) Multiplying (0.2) by 2 and subtracting (0.1), we obtain L = 2ϕ(h/2) ϕ(h) + O(h 3 ) := N 2 (h) + O(h 3 ). (0.3) Hence N 2 (h) = N 1 (h/2) N 1 (h) = N 1 (h/2) + N 1(h/2) N 1 (h) 2 1 6
where N 1 (h) = ϕ(h). Apply Richardson extrapolation on (0.3), we get: Hence, L = 23 N 2 (h/2) N 2 (h) 2 3 1 N 3 (h) = N 2 (h/2) + N 2(h/2) N 2 (h). 2 3 1 So on and so on, we conclude: where N 1 (h) = ϕ(h). + O(h 5 ) := N 3 (h) + O(h 5 ). (0.4) N j (h) = N j 1 (h/2) + N j 1(h/2) N j 1 (h), (j 2) 2 2j 3 1 7
Appixes % Determine 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 =[1885 1917 1919 1932 1958 1963 1968 1971 1974 1978 1981+1/6... 1981+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 ) ; % c o e f f i c i e n t s d=y ; f o r j =2: n ; f o r i =n : 1: j ; d ( i ) = ( d ( i ) d ( i 1 ) ) / ( x ( i ) x ( i j + 1 ) ) ; ; ; %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 ) ones ( s i z e ( t ) ) ; f o r i =n 1: 1:1 p =( t x ( i ) ones ( s i z e ( t ) ) ). p+d ( i ) ; p l o t ( x, y, ro ) hold on p l o t ( 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 =[1885 1917 1919 1932 1958 1963 1968 1971 1974 1978 1981+1/6... 1981+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 ) ; 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 ) ; 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 ) ) ; z=a\c ; z = [ 0 ; z ; 0 ] ; p l o t ( x, y, ro ) hold on f o r i = 1 : ( n 1) t =x ( i ) : x ( i + 1 ) ; s= z e r o s ( 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 ) ; p l o t ( t, s ) hold on % % Determine 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 =[1885 1917 1919 1932 1958 1963 1968 1971 1974 1978 1981+1/6... 1981+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 ) ; % c o e f f i c i e n t s d=y ; f o r j =2: n ; f o r i =n : 1: j ; d ( i ) = ( d ( i ) d ( i 1 ) ) / ( x ( i ) x ( i j + 1 ) ) ; ; ; %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 = 2 0 0 7 : 0. 0 1 : 2 0 0 8. 2 5 %1885:2008; p=d ( n ) ones ( s i z e ( t ) ) ; f o r i =n 1: 1:1 9
p =( t x ( i ) ones ( s i z e ( t ) ) ). p+d ( i ) ; % p l o t ( x, y, ro ) % h old on p l o t ( t, p, r ) grid on ; t i t l e ( Find 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 =[1885 1917 1919 1932 1958 1963 1968 1971 1974 1978 1981+1/6... 1981+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 ) ; 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 ) ; 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 ) ) ; z=a\c ; z = [ 0 ; z ; 0 ] ; % p l o t ( x, y, ro ) % h old on i =n 1; t = 2 0 0 7 : 0. 1 : 2 0 1 1 ; s= z e r o s ( 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 p l o t ( t, s ) 10
grid on t i t l e ( Find t h e time f o r t h e c o s t of 50 c e n t s u s i n g c u b i c s p l i n e ) 11