21.06.2014 Views

Numerical Methods Contents - SAM

Numerical Methods Contents - SAM

Numerical Methods Contents - SAM

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Interpolation of the function:<br />

f(x) = sin(5x) e x ,<br />

in the interval I = [−1, 1]<br />

on 11 equispaced points<br />

t j = −1 + 0.2 j, j = 0,...,10.<br />

Use of weighted averages of slopes as<br />

in (9.3.3).<br />

s(t)<br />

−0.5<br />

−1.5<br />

−2.5<br />

See Code 9.3.2.<br />

−3<br />

−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1<br />

1.5<br />

1<br />

0.5<br />

0<br />

−1<br />

−2<br />

Data points<br />

f(x)<br />

Piecew. cubic interpolation polynomial<br />

t<br />

Fig. 106 ✸<br />

26 legend ( ’ Data p o i n t s ’ , ’ f ( x ) ’ , ’ Piecew . cubic i n t e r p o l a t i o n polynomial ’ ) ;<br />

27 hold o f f ;<br />

28<br />

29 %———————————————————————–<br />

30 % slopes for interpolation: version (1) middle points of slopes<br />

31 function c=slopes1 ( t , y )<br />

32 n = length ( t ) ;<br />

33 h = d i f f ( t ) ; % increments in t<br />

34 d e l t a = d i f f ( y ) . / h ; % slopes of piecewise linear<br />

interpolant<br />

35<br />

36 c = [ d e l t a ( 1 ) , . . .<br />

37 ( ( h ( 2 : end ) .∗ d e l t a ( 1 : end−1)+h ( 1 : end−1) .∗ d e l t a ( 2 : end ) ) . . .<br />

38 . / ( t ( 3 : end ) − t ( 1 : end−2) ) ) , . . .<br />

39 d e l t a ( end ) ] ;<br />

Code 9.3.3: Piecewise cubic Hermite interpolation<br />

1 % 25.11.2009 hermintp1.m<br />

2 % compute and plot the Hermite interpolant of the function f in the nodes t<br />

3 % choice of the slopes using weighted averages of the local slopes<br />

Ôº¼ º¿<br />

6 n = length ( t ) ;<br />

4<br />

5 function hermintp1 ( f , t )<br />

7 h = d i f f ( t ) ; % increments in t<br />

8 y = feval ( f , t ) ;<br />

9 c=slopes1 ( t , y ) ;<br />

10<br />

Try: hermintp1( @(x)sin(5*x).*exp(x), [-1:0.2:1]);<br />

9.3.2 Interpolation error estimates<br />

Example 9.3.4 (Convergence of Hermite interpolation with exact slopes).<br />

Ôº¼ º¿<br />

11 figure ( ’Name ’ , ’ Hermite I n t e r p o l a t i o n ’ ) ;<br />

12 plot ( t , y , ’ ko ’ ) ; hold on ; % plot data points<br />

13 f p l o t ( f , [ t ( 1 ) , t ( n ) ] ) ;<br />

14 for j =1:n−1 % compute and plot the Hermite<br />

interpolant with slopes c<br />

15 vx = linspace ( t ( j ) , t ( j +1) , 100) ;<br />

16 plot ( vx , hermloceval ( vx , t ( j ) , t ( j +1) , y ( j ) , y ( j +1) , c ( j ) , c ( j +1) ) , ’ r−’ ,<br />

’ LineWidth ’ ,2) ;<br />

17 end<br />

18 for j =2:n−1 % plot segments indicating the slopes<br />

c-i<br />

19 plot ( [ t ( j ) −0.3∗h ( j −1) , t ( j ) +0.3∗h ( j ) ] , . . .<br />

20 [ y ( j ) −0.3∗h ( j −1)∗c ( j ) , y ( j ) +0.3∗h ( j ) ∗c ( j ) ] , ’ k−’ , ’ LineWidth ’ ,2) ;<br />

21 end<br />

22 plot ( [ t ( 1 ) , t ( 1 ) +0.3∗h ( 1 ) ] , [ y ( 1 ) , y ( 1 ) +0.3∗h ( 1 ) ∗c ( 1 ) ] , ’ k−’ ,<br />

’ LineWidth ’ ,2) ;<br />

23 plot ( [ t ( end ) −0.3∗h ( end ) , t ( end ) ] , [ y ( end ) −0.3∗h ( end ) ∗c ( end ) , y ( end ) ] , ’ k−’ ,<br />

Ôº¼ º¿<br />

’ LineWidth ’ ,2) ;<br />

24 xlabel ( ’ t ’ ) ;<br />

25 ylabel ( ’ s ( t ) ’ ) ;<br />

Piecewise cubic Hermite interpolation of<br />

f(x) = arctan(x) .<br />

domain: I = (−5, 5)<br />

mesh T = {−5 + hj} n j=0 ⊂ I, h = 10<br />

n ,<br />

exact slopes c i = f ′ (t i ), i = 0,...,n<br />

algebraic convergence O(h −4 )<br />

||s−f||<br />

sup−norm<br />

L 2 −norm<br />

10 2<br />

10 1<br />

10 0<br />

10 −1<br />

10 −2<br />

10 −3<br />

10 −4<br />

10 −5<br />

10 −6<br />

10 −1 10 0<br />

Meshwidth h<br />

10 1<br />

Formulas for approximate computation of errors:<br />

d = vector of absolute values of errors |f(x j ) − s(x j )| on a fine uniform mesh {x j },<br />

h = meshsize x j+1 − x j ,<br />

L ∞ -error = max(d);<br />

L 2 -error = sqrt(h*(sum(d(2:end-1).∧2)+(d(1)∧2+d(end)∧2)/2) );<br />

(trapezoidal rule).<br />

Ôº¼ º¿

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!