Numerical Methods Contents - SAM
Numerical Methods Contents - SAM
Numerical Methods Contents - SAM
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Algebraic order of convergence in h = min { 1 + regularity of f, 4 }.<br />
39 p = p o l y f i t ( log ( e r r ( : , 1 ) ) , log ( e r r ( : , 3 ) ) ,1) ; exp_rate_L2=p ( 1 )<br />
Theory [22]: f ∈ C 4 ([t 0 , t n ]) ➙ ‖f − s‖ L ∞ ([t 0 ,t n ]) ≤ 5<br />
384 h4 ∥ ∥ ∥f<br />
(4) ∥ ∥ ∥L ∞ ([t 0 ,t n ]) ✸<br />
Code 9.4.5: Spline approximation error<br />
1 % 22.06.2009 splineapprox.m<br />
2 % Plot convergence of approximation error of cubic spline interpolation<br />
3 % with respect to the meshwidth<br />
4 % print the algebraic order of convergence in sup and L 2 norms<br />
5 %<br />
6 % inputs: f function to be interpolated<br />
7 % df derivative of f, to be computed in a and b<br />
8 % a, b left and right extremes of the interval<br />
9 % N maximum number of subdomains<br />
10<br />
11 function splineapprox ( f , df , a , b ,N)<br />
12 x = a :0.00025:b ; f v = feval ( f , x ) ;<br />
Ôº¿¿ º<br />
13 dfa = feval ( df , a ) ; dfb = feval ( df , b ) ;<br />
14 e r r = [ ] ;<br />
15 for j =2:N<br />
16 t = a : ( b−a ) / j : b ; % spline nodes<br />
17 y = [ dfa , feval ( f , t ) , dfb ] ;<br />
18 % compute complete spline imposing exact first derivative at the extremes:<br />
19 v = spline ( t , y , x ) ;<br />
20 d = abs ( fv−v ) ;<br />
21 h = x ( 2 : end )−x ( 1 : end−1) ;<br />
22 %compute L 2 norm of the error using trapezoidal rule<br />
23 l 2 = sqrt (0.5∗ dot ( h , ( d ( 1 : end−1).^2+d ( 2 : end ) . ^ 2 ) ) ) ;<br />
24 % columns of err = meshwidth, L ∞ error, L 2 error:<br />
25 e r r = [ e r r ; ( b−a ) / j ,max( d ) , l 2 ] ;<br />
26 end<br />
27<br />
28 figure ( ’Name ’ , ’ Spline i n t e r p o l a t i o n ’ ) ;<br />
29 plot ( t , y ( 2 : end−1) , ’m∗ ’ , x , fv , ’ b−’ , x , v , ’ r−’ ) ;<br />
30 xlabel ( ’ t ’ ) ; ylabel ( ’ s ( t ) ’ ) ;<br />
31 legend ( ’ Data p o i n t s ’ , ’ f ’ , ’ Cubic s p l i n e i n t e r p o l a n t ’ , ’ l o c a t i o n ’ , ’ best ’ ) ;<br />
32<br />
33 figure ( ’Name ’ , ’ Spline approximation e r r o r ’ ) ;<br />
34 loglog ( e r r ( : , 1 ) , e r r ( : , 2 ) , ’ r .− ’ , e r r ( : , 1 ) , e r r ( : , 3 ) , ’ b.− ’ ) ;<br />
35 grid on ; xlabel ( ’ Meshwidth h ’ ) ; ylabel ( ’ | | s−f | | ’ ) ;<br />
Ôº¿ º<br />
36 legend ( ’ sup−norm ’ , ’ L^2−norm ’ , ’ Location ’ , ’ NorthWest ’ ) ;<br />
37 %compute algebraic orders of convergence using polynomial fit<br />
38 p = p o l y f i t ( log ( e r r ( : , 1 ) ) , log ( e r r ( : , 2 ) ) ,1) ; e xp_rate_Linf=p ( 1 )<br />
Try:<br />
1 splineapprox ( @atan , @( x ) 1 . / ( 1 + x . ^ 2 ) , −5 ,5 ,100) ;<br />
2 splineapprox (@( x ) 1 . / ( 1 + exp(−2∗x ) ) , . . .<br />
3 @( x ) 2∗exp(−2∗x ) . / ( 1 + exp(−2∗x ) ) . ^ 2 , −1 ,1 ,100) ;<br />
9.4.2 Shape Preserving Spline Interpolation<br />
Given: nodes (t i ,y i ) ∈ R 2 , i = 0,...,n, t 0 < t 1 < · · · < t n .<br />
Find:<br />
grid M ⊂ [t 0 , t n ] &<br />
an interpolating quadratic spline function s ∈ S 2,M , s(t i ) = y i , i = 0,...,n that preserves<br />
the “shape” of the data.<br />
Notice that M ≠ {t j } n j=0 : s interpolates the data in the points t i but is piecewise polynomial on M!<br />
We do four steps:<br />
➀ “Shape-faithful” choice of slopes c i , i = 0,...,n [26, 31] → Section 9.3<br />
We fix the slopes c i in the nodes using the harmonic mean of data slopes ∆ j , the final interpolant will<br />
be tangents to these segments in the points (t i ,y i ). If (t i , y i ) is a local maximum or minimum of the<br />
data, c j is set to zero.<br />
⎧<br />
⎨ 2<br />
Limiter c i := ∆ −1<br />
i +∆ −1 if sign(∆ i ) = sign(∆ i+1 ) ,<br />
i+1<br />
⎩0 otherwise,<br />
where ∆ j = y j−y j−1<br />
t j −t j−1<br />
.<br />
c 0 := 2∆ 1 − c 1 , c n := 2∆ n − c n−1 ,<br />
c i = 2<br />
∆ −1<br />
i +∆ −1<br />
i+1<br />
i = 1, ...,n − 1 .<br />
= harmonic mean of the slopes, see (9.3.4).<br />
Ôº¿ º<br />
Ôº¿ º