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.

Example 11.5.6 (Gain through adaptivity). → Ex. 11.5.4<br />

Simple adaptive timestepping from previous experiment Ex. 11.5.4.<br />

New: initial state y(0) = 0!<br />

Now we study the dependence of the maximal point error on the computational effort, which is proportional<br />

to the number of timesteps.<br />

Code 11.5.7: MATLAB function for Ex. 11.5.6<br />

1 function adaptgain ( T , a , r e l t o l , a b s t o l )<br />

2 % Experimental study of gasin through simple adaptive timestepping<br />

3 % strategy of Code 11.5.2 based on explicit Euler<br />

4 % (11.2.1) and explicit trapezoidal<br />

5 % rule (11.4.3)<br />

Ôº ½½º<br />

9 i f ( nargin < 3) , r e l t o l = 1E−1; end<br />

6<br />

7 % Default arguments<br />

8 i f ( nargin < 4) , a b s t o l = 1E−3; end<br />

✸<br />

33 end<br />

34<br />

35 % Run adaptive timestepping with various tolerances, which is the only way<br />

36 % to make it use a different total number of timesteps.<br />

37 % Plot the solution sequences for different values of the relative tolerance.<br />

38 figure ( ’name ’ , ’ adaptive timestepping ’ ) ;<br />

39 axis ( [ 0 2 0 0 . 0 5 ] ) ; hold on ; c o l = colormap ;<br />

40 errad = [ ] ; l = 1 ;<br />

41 for r t o l = r e l t o l ∗2.^(2: −1: −4)<br />

42 % Crude choice of initial timestep and h min<br />

43 h0 = T / 1 0 ; hmin = h0 /10000;<br />

44 % Main adaptive timestepping loop, see Code 11.5.2<br />

45 [ t , y , r e j , ee ] =<br />

odeintadapt_ext ( Psilow , Psihigh , T , y0 , h0 , r t o l ,0.01∗ r t o l , hmin ) ;<br />

46 errad = [ errad ; length ( t ) −1, max( abs ( s o l ( t ) − y ) ) , r t o l ,<br />

length ( r e j ) ] ;<br />

47 f p r i n t f ( ’ r t o l = %d : %d timesteps , %d r e j e c t e d timesteps \ n ’ ,<br />

r t o l , length ( t ) −1,length ( r e j ) ) ;<br />

48 plot ( t , y , ’ . ’ , ’ c o l o r ’ , c o l (10∗( l −1) + 1 , : ) ) ;<br />

49 leg { l } = s p r i n t f ( ’ r t o l = %f ’ , r t o l ) ; l = l +1;<br />

Ôº ½½º<br />

50 end<br />

51 xlabel ( ’ { \ b f t } ’ , ’ f o n t s i z e ’ ,14) ;<br />

52 ylabel ( ’ { \ b f y } ’ , ’ f o n t s i z e ’ ,14) ;<br />

10 i f ( nargin < 2) , a = 40; end<br />

11 i f ( nargin < 1) , T = 2 ; end<br />

12<br />

13 % autonomous ODE ẏ = cos(ay) and its general solution<br />

14 f = @( y ) ( cos ( a∗y ) . ^ 2 ) ; s o l = @( t ) ( atan ( a∗( t ) ) / a ) ;<br />

15 % Initial state y 0<br />

16 y0 = s o l ( 0 ) ;<br />

17<br />

18 % Discrete evolution operators, see Def. 11.2.1<br />

19 Psilow = @( h , y ) ( y + h∗ f ( y ) ) ; % Explicit Euler (11.2.1)<br />

20 % Explicit trapzoidal rule (11.4.3)<br />

21 Psihigh = @( h , y ) ( y + 0.5∗h∗( f ( y ) + f ( y+h∗ f ( y ) ) ) ) ;<br />

22<br />

23 % Lop over uniform timesteps of varying length and integrate ODE by explicit trapezoidal<br />

24 % rule (11.4.3)<br />

25 e r r u f = [ ] ;<br />

26 for N=10:10:200<br />

27 h = T /N; t = 0 ; y = y0 ; e r r = 0 ;<br />

28 for k =1:N<br />

29 y = Psihigh ( h , y ) ; t = t +h ;<br />

30 e r r = max( err , abs ( s o l ( t ) −<br />

Ôº ½½º<br />

y ) ) ;<br />

31 end<br />

32 e r r u f = [ e r r u f ;N, e r r ] ;<br />

53 legend ( leg , ’ l o c a t i o n ’ , ’ southeast ’ ) ;<br />

54 t i t l e ( s p r i n t f ( ’ Solving d_t y = a cos ( y ) ^2 w i th a = %f by simple<br />

adaptive timestepping ’ , a ) ) ;<br />

55 p r i n t −depsc2 ’ . . / PICTURES/ adaptgainsol . eps ’ ;<br />

56<br />

57 % Plotting the errors vs. the number of timesteps<br />

58 figure ( ’name ’ , ’ gain by a d a p t i v i t y ’ ) ;<br />

59 loglog ( e r r u f ( : , 1 ) , e r r u f ( : , 2 ) , ’ r + ’ , errad ( : , 1 ) , errad ( : , 2 ) , ’m∗ ’ ) ;<br />

60 xlabel ( ’ { \ b f no . N o f timesteps } ’ , ’ f o n t s i z e ’ ,14) ;<br />

61 ylabel ( ’ { \ b f max_k | y ( t_k )−y_k | } ’ , ’ f o n t s i z e ’ ,14) ;<br />

62 t i t l e ( s p r i n t f ( ’ E r r o r vs . no . o f timesteps f o r d_t y = a cos ( y ) ^2 w i th a<br />

= %f ’ , a ) ) ;<br />

63 legend ( ’ uniform timestep ’ , ’ adaptive timestep ’ , ’ l o c a t i o n ’ , ’ n o r theast ’ ) ;<br />

64 p r i n t −depsc2 ’ . . / PICTURES/ adaptgain . eps ’ ;<br />

Ôº ½½º

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

Saved successfully!

Ooh no, something went wrong!