21.06.2014 Views

Numerical Methods Contents - SAM

Numerical Methods Contents - SAM

Numerical Methods Contents - SAM

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Code 9.3.5: Hermite approximation and orders of convergence with exact slopes<br />

1 % 16.11.2009 hermiteapprox1.m<br />

2 % Plot convergence of approximation error of cubic hermite interpolation<br />

3 % with respect to the meshwidth<br />

4 % print the algebraic order of convergence in sup and L 2 norms<br />

5 % Slopes: exact slopes of f<br />

6 %<br />

7 % inputs: f function to be interpolated<br />

8 % df derivative of f<br />

9 % a, b left and right extremes of the interval<br />

10 % N maximum number of subdomains<br />

11<br />

12 function hermiteapprox1 ( f , df , a , b ,N)<br />

13 e r r = [ ] ;<br />

14 for j =2:N<br />

15 xx=a ; % xx is the mesh on which the error is computed, built in the k-loop<br />

16 v a l = f ( a ) ;<br />

17<br />

Ôº¼ º¿<br />

22 for k =1: j −1<br />

18 t = a : ( b−a ) / j : b ; % nodes<br />

19 y = f ( t ) ;<br />

20 c= d f ( t ) ; % compute exact slopes<br />

21<br />

e xp_rate_Linf= p I ( 1 )<br />

46 pL2 = p o l y f i t ( log ( e r r ( c e i l (N/ 2 ) :N−2,1) ) , log ( e r r ( c e i l (N/ 2 ) :N−2,3) ) ,1) ;<br />

exp_rate_L2=pL2 ( 1 )<br />

47 hold on ;<br />

48 plot ( [ e r r ( 1 , 1 ) , e r r (N−1,1) ] , [ e r r ( 1 , 1 ) , e r r (N−1,1) ] . ^ p I ( 1 ) ∗exp ( p I ( 2 ) ) , ’m ’ )<br />

49 plot ( [ e r r ( 1 , 1 ) , e r r (N−1,1) ] ,<br />

[ e r r ( 1 , 1 ) , e r r (N−1,1) ] . ^ pL2 ( 1 ) ∗exp ( pL2 ( 2 ) ) , ’ k ’ )<br />

Try: hermiteapprox1(@atan, @(x) 1./(1+x.∧2), -5,5,100);<br />

Example 9.3.6 (Convergence of Hermite interpolation with averaged slopes).<br />

✸<br />

Ôº½½ º¿<br />

23 vx = linspace ( t ( k ) , t ( k +1) , 100) ;<br />

24 l o c v a l =hermloceval ( vx , t ( k ) , t ( k +1) , y ( k ) , y ( k +1) , c ( k ) , c ( k +1) ) ;<br />

25 xx =[ xx , vx ( 2 : 1 0 0 ) ] ;<br />

26 v a l =[ val , l o c v a l ( 2 : 1 0 0 ) ] ;<br />

27 end<br />

28 d = abs ( feval ( f , xx )− v a l ) ;<br />

29 h = ( b−a ) / j ;<br />

30 % compute L 2 norm of the error using trapezoidal rule<br />

31 l 2 = sqrt ( h∗(sum( d ( 2 : end−1) . ^ 2 ) +(d ( 1 ) ^2+d ( end ) ^2) / 2 ) ) ;<br />

32 % columns of err = meshwidth, sup-norm error, L 2 error:<br />

33 e r r = [ e r r ; h ,max( d ) , l 2 ] ;<br />

34 end<br />

35<br />

36 figure ( ’Name ’ , ’ Hermite approximation ’ ) ;<br />

37 loglog ( e r r ( : , 1 ) , e r r ( : , 2 ) , ’ r .− ’ , e r r ( : , 1 ) , e r r ( : , 3 ) , ’ b.− ’ ) ;<br />

38 grid on ;<br />

39 xlabel ( ’ Meshwidth h ’ ) ;<br />

40 ylabel ( ’ | | s−f | | ’ ) ;<br />

41 legend ( ’ sup−norm ’ , ’ L^2−norm ’ ) ;<br />

Ôº½¼ º¿<br />

45 p I = p o l y f i t ( log ( e r r ( c e i l (N/ 2 ) :N−2,1) ) , log ( e r r ( c e i l (N/ 2 ) :N−2,2) ) ,1) ;<br />

42<br />

43 % compute algebraic orders of convergence,<br />

44 % using polynomial fit on half of the plot<br />

Piecewise cubic Hermite interpolation of<br />

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

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

equidistant mesh T in I, see Ex. 9.3.4,<br />

averaged local slopes, see (9.3.3)<br />

algebraic convergence in meshwidth<br />

See Code 9.3.6.<br />

Lower order of convergence due to the choice of the slopes (9.3.3):<br />

from the plot L ∞ -norm ∼ L 2 -norm ∼ O(h 3 )<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 −1 10 0<br />

Meshwidth h<br />

10 1<br />

✸<br />

Ôº½¾ º¿<br />

Code 9.3.7: Hermite approximation and orders of convergence<br />

1 % 16.11.2009 hermiteapprox.m

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

Saved successfully!

Ooh no, something went wrong!