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.
However, it would be foolish not to use the better value y k+1 = ˜Ψ hk y k , since it is available for<br />
free. This is what is done in every implementation of adaptive methods, also in Code 11.5.2, and this<br />
choice can be justified by control theoretic arguments [12, Sect. 5.2].<br />
Example 11.5.4 (Simple adaptive stepsize control).<br />
IVP for ODE<br />
ẏ = cos(αy) 2 , α > 0, solution y(t) = arctan(α(t −c))/α for y(0) ∈] −π/2,π/2[<br />
Simple adaptive timestepping based on explicit Euler (11.2.1) and explicit trapezoidal rule (11.4.3)<br />
Code 11.5.5: MATLAB function for Ex. 11.5.4<br />
1 function o d e i n t a d a p t d r i v e r ( T , a , r e l t o l , a b s t o l )<br />
2 % Simple adaptive timestepping strategy of Code 11.5.2<br />
3 % based on explicit Euler (11.2.1) and explicit trapezoidal<br />
4 % rule (11.4.3)<br />
5<br />
Ôº½ ½½º<br />
10 i f ( nargin < 1) , T = 2 ; end<br />
6 % Default arguments<br />
7 i f ( nargin < 4) , a b s t o l = 1E−4; end<br />
8 i f ( nargin < 3) , r e l t o l = 1E−2; end<br />
9 i f ( nargin < 2) , a = 20; end<br />
%f ’ , r e l t o l , abstol , a ) ) ;<br />
33 xlabel ( ’ { \ b f t } ’ , ’ f o n t s i z e ’ ,14) ;<br />
34 ylabel ( ’ { \ b f y } ’ , ’ f o n t s i z e ’ ,14) ;<br />
35 legend ( ’ y ( t ) ’ , ’ y_k ’ , ’ r e j e c t i o n ’ , ’ l o c a t i o n ’ , ’ northwest ’ ) ;<br />
36 p r i n t −depsc2 ’ . . / PICTURES/ o d e i n tadaptsol . eps ’ ;<br />
37<br />
38 f p r i n t f ( ’%d timesteps , %d r e j e c t e d<br />
timesteps \ n ’ , length ( t ) −1,length ( r e j ) ) ;<br />
39<br />
40 % Plotting estimated and true errors<br />
41 figure ( ’name ’ , ’ ( estimated ) e r r o r ’ ) ;<br />
42 plot ( t , abs ( s o l ( t ) − y ) , ’ r + ’ , t , ee , ’m∗ ’ ) ;<br />
43 xlabel ( ’ { \ b f t } ’ , ’ f o n t s i z e ’ ,14) ;<br />
44 ylabel ( ’ { \ b f e r r o r } ’ , ’ f o n t s i z e ’ ,14) ;<br />
45 legend ( ’ t r u e e r r o r | y ( t_k )−y_k | ’ , ’ estimated e r r o r<br />
EST_k ’ , ’ l o c a t i o n ’ , ’ northwest ’ ) ;<br />
46 t i t l e ( s p r i n t f ( ’ Adaptive timestepping , r t o l = %f , a t o l = %f , a =<br />
%f ’ , r e l t o l , abstol , a ) ) ;<br />
47 p r i n t −depsc2 ’ . . / PICTURES/ o d e i n t a d a p t e r r . eps ’ ;<br />
Ôº¿ ½½º<br />
11<br />
12 % autonomous ODE ẏ = cos(ay) and its general solution<br />
13 f = @( y ) ( cos ( a∗y ) . ^ 2 ) ; s o l = @( t ) ( atan ( a∗( t −1) ) / a ) ;<br />
14 % Initial state y 0<br />
15 y0 = s o l ( 0 ) ;<br />
16<br />
17 % Discrete evolution operators, see Def. 11.2.1<br />
18 Psilow = @( h , y ) ( y + h∗ f ( y ) ) ; % Explicit Euler (11.2.1)<br />
19 % Explicit trapzoidal rule (11.4.3)<br />
20 Psihigh = @( h , y ) ( y + 0.5∗h∗( f ( y ) + f ( y+h∗ f ( y ) ) ) ) ;<br />
21<br />
22 % Heuristic choice of initial timestep and h min<br />
23 h0 = T /(100∗(norm( f ( y0 ) ) +0.1) ) ; hmin = h0 /10000;<br />
24 % Main adaptive timestepping loop, see Code 11.5.2<br />
25 [ t , y , r e j , ee ] =<br />
odeintadapt_ext ( Psilow , Psihigh , T , y0 , h0 , r e l t o l , abstol , hmin ) ;<br />
26<br />
y<br />
0.08<br />
0.06<br />
0.04<br />
0.02<br />
0<br />
−0.02<br />
−0.04<br />
−0.06<br />
y(t)<br />
y k<br />
rejection<br />
Adaptive timestepping, rtol = 0.010000, atol = 0.000100, a = 20.000000<br />
−0.08<br />
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2<br />
t<br />
Statistics:<br />
Observations:<br />
Fig. 148<br />
error<br />
0.025<br />
0.02<br />
0.015<br />
0.01<br />
0.005<br />
Adaptive timestepping, rtol = 0.010000, atol = 0.000100, a = 20.000000<br />
true error |y(t k<br />
)−y k<br />
|<br />
estimated error EST k<br />
Fig. 149<br />
0<br />
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2<br />
t<br />
66 timesteps, 131 rejected timesteps<br />
27 % Plotting the exact the approximate solutions and rejected timesteps<br />
28 figure ( ’name ’ , ’ s o l u t i o n s ’ ) ;<br />
29 tp = 0 :T/1000:T ; plot ( tp , s o l ( tp ) , ’ g−’ , ’ l i n e w i d t h ’ ,2) ; hold on ;<br />
Ôº¾ ½½º<br />
30 plot ( t , y , ’ r . ’ ) ;<br />
31 plot ( r e j , 0 , ’m+ ’ ) ;<br />
32 t i t l e ( s p r i n t f ( ’ Adaptive timestepping , r t o l = %f , a t o l = %f , a =<br />
☞ Adaptive timestepping well resolves local features of solution y(t) at t = 1<br />
☞ Estimated error (an estimate for the one-step error) and true error are not related!<br />
Ôº ½½º