KTH, DN2221, Computer Lab 2

22

description

Applied Numerical Methods, KTH course DN2221, Solution to Computer Lab 2 ( Numerical Solution of Initial Value Problems ), this report achieved full points, this is the solution to the corresponding computer lab 2 in Lennart Edsberg: Introduction to Computation and Modelling for Differential Equations

Transcript of KTH, DN2221, Computer Lab 2

Page 1: KTH, DN2221, Computer Lab 2

Applied Numerical Methods

DN2221 - HS 2012

Computer Lab 2

Numerical Solution of Initial Value Problems

Sebastian Arnoldt

Teacher: Lennart Edsberg

12-2012

Page 2: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 1

Contents

1 Accuracy of Runge-Kutta Method 2

2 Stability Investigation of a Runge-Kutta Method 3

3 Parameter Studies of Solutions of an ODE System 7

A MATLAB code 9

A.1 Accuracy of a Runge-Kutta Method . . . . . . . . . . . . . . . 9

A.2 Constant Stepsize Experiment . . . . . . . . . . . . . . . . . . 12

A.3 Adaptive Stepsize Using MATLAB functions . . . . . . . . . . 15

A.4 Particle Flow Past a Cylinder . . . . . . . . . . . . . . . . . . 17

A.5 Motion of a Particle . . . . . . . . . . . . . . . . . . . . . . . 19

B Bibliography 21

Page 3: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 2

1 Accuracy of Runge-Kutta Method

In the �rst part of this lab we determine the accuracy of the given Runge-

Kutta Method (RKM) in the lab description [1, p.21] in a numerical exper-

iment, using the Van der Pol's di�erential equation (VdPDE) [1, p.219] as

system under investigation. First, we implement the given RKM to solve the

VdPDE for constant stepsizes N = 10, 20, 40, 80, 160, 320 on the t-interval

[0,1]. We then de�ne the local truncation error at t = 1 as eN = yN − y(1),where y(1) ≈ yN=320.

Fig. 1 below shows a double logarithmic plot of the |eN | as a function

of the (decreasing) stepsize h. Making a linear �t to the logarithms of the

calculated data in MATLAB, we �nd that the order of the accuracy is ap-

proximately O(h3). This consistent with our expectations, since the applied

RKM uses three coe�cients k1, k2 and k3. The MATLAB code for this part

of the lab is included in the appendix (A.1).

Figure 1: Accuracy of the given RKM for VdPDE

Page 4: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 3

2 Stability Investigation of a Runge-Kutta Method

In the second part of this lab, we investigate the stability of the given RKM

for Robertson's Problem given in [1, p.219]. First we use the RKM given

in the �rst part of the lab to solve Robertson's Problem with constant step-

sizes corresponding to N = 125, 250, 500, 1000, 2000. Tab. 1 below shows the

results of the calculations, where nan stands for not any number. In analogy

to the discussion in [1, p.49], we conclude from tab. 1, that the smallest

number of steps in order to achieve numerical stability is N = 1000. Fig. 2

below shows the solution trajectory for Robertson's Problem for N = 2000.

Table 1: Results Constant Step-Size Experiment

N t x1 x2 x3

125 1.0000 � � �250 1.0000 � � �500 1.0000 � � �1000 1.0000 0.96646 3.0746× 10−5 3.0746× 10−5

2000 1.0000 0.96646 3.0746× 10−5 3.0746× 10−5

Page 5: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 4

Figure 2: Solution trajectory of Robertson's Problem for N = 2000

Page 6: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 5

Next, we perform an adaptive step-size experiment on Robertson's problem,

using MATLAB functions. First, we use the non-sti� IVP solver od23 on the

t-interval [0,1] at di�erent relative tolerances RelTol = 10−3, 10−4, 10−5, 10−6

and record the number of steps taken. Next, we run the sti� IVP solver

ode23s on the t-interval [0,1000] for the same tolerances and record the num-

ber of steps again. The recorded step-sizes for both experiments are summa-

rized in tab. 2 below. Here we see, that the number of steps used in the �rst

experiment is an order of magnitude higher than the number of steps used in

the second experiment. Moreover, the number of steps is practically constant

for di�erent relative tolerances in the �rst experiment. On the contrary, the

number of steps increases as the relative tolerance becomes smaller in the

second experiment.

Table 2: Results Constant Step-Size Experiment

RelTol 10−3 10−4 10−5 10−6

ode23 866 867 868 868ode23s 30 37 48 61

Fig. A.3 below shows two graphs of the step-size h as a function of t at

RelTol = 10−6. The upper plot is generated using ode23 in the �rst ex-

periment, wheres the lower plot is generated using ode23s in the second

experiment. The two plots visualize the observation of constant step-sizes

(�rst experiment) and varying step-sizes (second experiment) made above.

The MATLAB code for this part of the lab is included in the appendix (A.2

and A.3)

Page 7: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 6

Figure 3: Comparison of step-sizes used by ode23 and ode23s to solve Robert-son's problem.

Page 8: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 7

3 Parameter Studies of Solutions of an ODE

System

In the last part of the lab, we perform a parameter study of the two problems

given in the lab description [1, pp.220-221]. In the �rst problem we calculate

the �ow curves of four di�erent particles around a cylinder placed in an

incompressible liquid on the t-interval [0,10]. In the second problem, we

calculate the trajectories of a particle that is thrown up for di�erent elevation

angles and air resistance coe�cients. The MATLAB code for this part of

the lab is included in the appendix (A.4 and A.5). Fig. 4 below shows the

calculated �ow curves for the �rst problem, and �g. 5 shows the calculated

trajectories for the motion of the particle.

Figure 4: Parametric plot of �ow curves of four di�erent particles on t-interval[0,10]

Page 9: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 8

Figure 5: Trajectories of particle for k = 0.02 (left) and k = 0.065 (right) forthree di�erent values of elevation angle α = 30, 45 and 60 degrees.

Page 10: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 9

A MATLAB code

A.1 Accuracy of a Runge-Kutta Method

% f i r s t r ewr i t e 2nd order DE in to system o f f i r s t order

DEs

% d/dt y_2 = −y_1 − ep s i l o n (y_1^2 −1)*y_2% d/dt y_1 = y_2

i t e r a t i o n s = [ 10 , 2 0 , 4 0 , 8 0 , 1 60 , 3 20 ] ;

s t e p s i z e = 1 ./ i t e r a t i o n s ;

data = ze ro s ( l ength ( i t e r a t i o n s ) ,3 ) ;

f 1 = i n l i n e ( ' y2 ' , ' y1 ' , ' y2 ' ) ;

f 2 = i n l i n e ( '−y1 −(y1^2 − 1) *y2 ' , ' y1 ' , ' y2 ' ) ;

f o r k=1: l ength ( i t e r a t i o n s )

h = s t e p s i z e ( k ) ;

n = i t e r a t i o n s (k ) ;

y1 = [ 1 : n+1] ; %array to s t o r e y1

y2 = [ 1 : n+1] ; %array to s t o r e y2

y1 (1 ) = 1 ;

y2 (1 ) = 0 ;

%implement runge−kutta f o r system o f two coupled

f i r s t order DEs

f o r i =1:(n)

k1 = f1 ( y1 ( i ) , y2 ( i ) ) ;

m1 = f2 ( y1 ( i ) , y2 ( i ) ) ;

k2 = f1 ( y1 ( i )+h*k1 , y2 ( i )+h*m1) ;

Page 11: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 10

m2 = f2 ( y1 ( i )+h*k1 , y2 ( i )+h*m1) ;

k3 = f1 ( y1 ( i ) +0.25*h*k1+0.25*h*k2 , y2 ( i ) +0.25*h*

m1+0.25*h*m2) ;

m3 = f2 ( y1 ( i ) +0.25*h*k1+0.25*h*k2 , y2 ( i ) +0.25*h*

m1+0.25*h*m2) ;

y1 ( i +1) = y1 ( i ) + h/6*( k1 + k2 + 4*k3 ) ;

y2 ( i +1) = y2 ( i ) + h/6*(m1 + m2 + 4*m3) ;

end

%wr i t e r e s u l t s from inner f o r loop in to data matrix

data (k , 2 ) = y1 (n+1) ;

data (k , 3 ) = y2 (n+1) ;

c l e a r y1 ;

c l e a r y2 ;

end

%ca l c u l a t e the e r r o r

y_max = data ( l ength ( i t e r a t i o n s ) ,2 ) ;

e r r o r = abs ( data ( : , 2 )− y_max) ;

e r r o r = e r r o r ( 1 : 5 ) ;

s t e p s i z e = s t e p s i z e ( 1 : 5 ) ;

%use data from l i n e a r f i t to p l o t l i n e a r f i t i n to same

p lo t

l i n f i t=log (−4.3328) +3.0019* l og ( s t e p s i z e ) ;

%p lo t e r r o r vs s t e p s i z e

l o g l o g ( s t ep s i z e , e r ro r , s t e p s i z e , l i n f i t )

t i t l e ( ' Accuracy RKM fo r VdPDE' )

x l ab e l ( ' s t e p s i z e h ' )

y l ab e l ( ' e r r o r in comparison to h = 1/320 ') ;

Page 12: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 11

% %th i s i s j u s t f o r e s t imat ing the the e r r o r

% s t e p s i z e=log ( s t e p s i z e )

% e r r o r=log ( e r r o r )

% p lo t ( s t ep s i z e , e r r o r )

Page 13: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 12

A.2 Constant Stepsize Experiment

% f i r s t r ewr i t e 2nd order DE in to system o f f i r s t order

DEs

% d/dt y_2 = −y_1 − ep s i l o n (y_1^2 −1)*y_2% d/dt y_1 = y_2

%format long ;

c1 = 0 . 0 4 ;

c2 = 10^4;

c3 = 3*10^7;

i t e r a t i o n s = [125 , 250 , 500 , 1000 , 2000 ] ;

s t e p s i z e = 1 ./ i t e r a t i o n s ;

data = ze ro s ( l ength ( i t e r a t i o n s ) ,4 ) ;

f 11 = i n l i n e ( '−0.04*y1 + (10^4) *y2*y3 ' , ' y1 ' , ' y2 ' , ' y3 ' ) ;

f 22 = i n l i n e ( ' 0 . 0 4* y1 − (10^4) *y2*y3 − (3*10^7) *( y2 )

^2 ' , ' y1 ' , ' y2 ' , ' y3 ' ) ;

f 33 = i n l i n e ( '3*10^7*( y2 ) ^2 ' , ' y1 ' , ' y2 ' , ' y3 ' ) ;

f o r k=1: l ength ( i t e r a t i o n s )

h = s t e p s i z e ( k ) ;

n = i t e r a t i o n s ( k ) ;

t = [ 1 : n+1] ; %use t as parameter

y1 = [ 1 : n+1] ; %array to s t o r e y1

y2 = [ 1 : n+1] ; %array to s t o r e y2

y3 = [ 1 : n+1] ;

t (1 ) = 0 ;

Page 14: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 13

y1 (1 ) = 1 ;

y2 (1 ) = 0 ;

y3 (1 ) = 0 ;

%implement runge−kutta f o r system o f two coupled

f i r s t order DEs

f o r i =1:(n)

k1 = f11 ( y1 ( i ) , y2 ( i ) , y3 ( i ) ) ;

m1 = f22 ( y1 ( i ) , y2 ( i ) , y3 ( i ) ) ;

l 1 = f33 ( y1 ( i ) , y2 ( i ) , y3 ( i ) ) ;

k2 = f11 ( y1 ( i )+h*k1 , y2 ( i )+h*m1, y3 ( i )+h* l 1 ) ;

m2 = f22 ( y1 ( i )+h*k1 , y2 ( i )+h*m1, y3 ( i )+h* l 1 ) ;

l 2 = f33 ( y1 ( i )+h*k1 , y2 ( i )+h*m1, y3 ( i )+h* l 1 ) ;

k3 = f11 ( y1 ( i ) +0.25*h*k1+0.25*h*k2 , y2 ( i ) +0.25*h

*m1+0.25*h*m2, y3 ( i ) +0.25*h* l 1 +0.25*h* l 2 ) ;

m3 = f22 ( y1 ( i ) +0.25*h*k1+0.25*h*k2 , y2 ( i ) +0.25*h

*m1+0.25*h*m2, y3 ( i ) +0.25*h* l 1 +0.25*h* l 2 ) ;

l 3 = f33 ( y1 ( i ) +0.25*h*k1+0.25*h*k2 , y2 ( i ) +0.25*h

*m1+0.25*h*m2, y3 ( i ) +0.25*h* l 1 +0.25*h* l 2 ) ;

t ( i +1) = t ( i ) + h ;

y1 ( i +1) = y1 ( i ) + h/6*( k1 + k2 + 4*k3 ) ;

y2 ( i +1) = y2 ( i ) + h/6*(m1 + m2 + 4*m3) ;

y3 ( i +1) = y3 ( i ) + h/6*( l 1 + l 2 + 4* l 3 ) ;

end

%wr i t e r e s u l t s from inner f o r loop in to data matrix

data (k , 1 ) = t (n+1) ;

data (k , 2 ) = y1 (n+1) ;

data (k , 3 ) = y2 (n+1) ;

data (k , 4 ) = y2 (n+1) ;

Page 15: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 14

%s t o r e s o l u t i o n t r a j e c t o r y f o r k=5or n = 2000

t r a j 1 = y1 ;

t r a j 2 = y2 ;

t r a j 3 = y3 ;

end

%d i sp l ay r e s u l t s

d i sp ( data )

%export r e s u l t s to l a t e x us ing l a t ex .m l i b r a r y from

MathWorks . com

la t ex ( data , ' nomath ' )

l o g l o g ( t , t ra j1 , t , t r a j 2 , t , t r a j 3 )

t i t l e ( ' s o l u t i o n t r a j e c t o r y Robertsons Problem f o r N =

2000 ')

ax i s ([10^(−4) 10^0 10^(−6) 10^1])

x l ab e l ( ' parameter ln ( t ) ' )

y l ab e l ( ' s o l u t i o n components ln (y ) ' ) ;

l egend ( 'A( t ) ' , 'B( t ) ' , 'C( t ) ' ) ;

%pr in t ( '−dpdf ' , '− r300 ' , ' . . / f i g u r e s /part_2a . jpg ' )

Page 16: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 15

A.3 Adaptive Stepsize Using MATLAB functions

%crea t e column vec to r to plug in to ode23s

format long ;

t o l e r an c e = [1 e−3, 1e−4, 1e−5, 1e−6] ;range = [0 1 ; 0 1 0 0 0 ] ;

%so l v e the DE system with ode23 and range [ 0 1 ]

f o r i =1:4

opt ions = odeset ( ' RelTol ' , t o l e r an c e ( i ) ) ;

[T,Y] = ode23 ( @rigid , [ 0 1 ] , [ 1 0 0 ] , opt i ons ) ;

n_ode23_steps ( i ) = length (T)−1;end

% make an array f o r s t e p s i z e to be p l o t t ed

f o r j = 1 : ( l ength (T)−1)h( j ) = T( j +1)−T( j ) ;time ( j ) = T( j +1) ;

end

subplot ( 2 , 1 , 1 )

p l o t ( time , h)

t i t l e ( ' Robertsons Problem with ode23 and range [ 0

1 ] ' )

x l ab e l ( ' time t ' )

y l ab e l ( ' s t e p s i z e h ' )

%so l v e the DE system with ode23s and range [ 0 1000 ]

f o r i =1:4

opt ions = odeset ( ' RelTol ' , t o l e r an c e ( i ) ) ;

Page 17: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 16

[T,Y] = ode23s ( @rigid , [ 0 1 0 00 ] , [ 1 0 0 ] , opt i ons ) ;

n_ode23s_steps ( i ) = length (T)−1;end

% make an array f o r s t e p s i z e to be p l o t t ed

f o r j = 1 : ( l ength (T)−1)h2 ( j ) = T( j +1)−T( j ) ;time2 ( j ) = T( j +1) ;

end

subplot ( 2 , 1 , 2 )

p l o t ( time2 , h2 )

t i t l e ( ' Robertsons Problem with ode23s and range [ 0

1000 ] ' )

x l ab e l ( ' time t ' )

y l ab e l ( ' s t e p s i z e h ' )

func t i on [ dy ] = r i g i d ( t , y )

k1 = 0 . 0 4 ;

k2 = 10^4;

k3 = 3*10^7;

dy = ze ro s (3 , 1 ) ;

dy (1 ) = −k1*y (1 ) + k2*y (2 ) *y (3 ) ;

dy (2 ) = k1*y (1 ) − k2*y (2 ) *y (3 ) − k3 *( y (2 ) ) ^2;

dy (3 ) = k3 *( y (2 ) ) ^2;

end

Page 18: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 17

A.4 Particle Flow Past a Cylinder

N=1000;h=10/N;

y l i s t = [ 0 . 2 0 .6 1 1 . 6 ] ;

f o r k=1:4

u = [−4; y l i s t ( k ) ] ;r e s u l t =[u ] ; h l i s t = [ 0 ] ;

f o r j =1:N;

u1 = [ u (1 ) ; u (2 ) ] ;

k1 = Cyl inder ( u1 ) ;

u2 = [ u (1 )+h*k1 (1 ) ; u (2 )+h*k1 (2 ) ] ;

k2 = Cyl inder ( u2 ) ;

u3 = [ u (1 )+(h/4) *k1 (1 )+(h/4) *k2 (1 ) ; u (2 )+(h/4) *

k1 (2 )+(h/4) *k2 (2 ) ] ;

k3 = Cyl inder ( u3 ) ;

u = u+(h/6) *( k1+k2+4*k3 ) ;

r e s u l t =[ r e s u l t u ] ;

h l i s t =[ h l i s t j *h ] ;

end

hold on

p lo t ( r e s u l t ( 1 , : ) , r e s u l t ( 2 , : ) ) ; ax i s equal ;

Page 19: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 18

t i t l e ( ' parametr ic p l o t o f p a r t i c l e f low around

cy l inde r ' ) ; x l ab e l ( ' x ' ) ; y l ab e l ( ' y ' ) ;

end

func t i on [ u_prime ] = Cyl inder ( u )

R = 2 ;

r_sq = (u (1 )^2+u (2) ^2) ^2;

u_prime = [1−R^2*(u (1 )^2−u (2) ^2)/r_sq ; −2*R^2*u (1) *u (2)/r_sq ] ;

end

Page 20: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 19

A.5 Motion of a Particle

h=10^(−3) ;

f o r i =1:2 ;

k=[0.02 0 . 0 6 5 ] ;

f o r j =1:3

alpha = [ p i /6 p i /4 p i / 3 ] ;

r = [ 0 ; 1 . 5 ] ;

v = [20* cos ( alpha ( j ) ) ; 20* s i n ( alpha ( j ) ) ] ;

r e s u l t =[ r ] ; h l i s t = [ 0 ] ;

whi l e r (2 )>0

v1 = [ v (1 ) ; v (2 ) ] ;

k1 = Motion ( v1 , k ( i ) ) ;

v2 = [ v (1 )+h*k1 (1 ) ; v (2 )+h*k1 (2 ) ] ;

k2 = Motion ( v2 , k ( i ) ) ;

v3 = [ v (1 )+(h/4) *k1 (1 )+(h/4) *k2 (1 ) ; v (2 )+(h

/4) *k1 (2 )+(h/4) *k2 (2 ) ] ;

k3 = Motion ( v3 , k ( i ) ) ;

v = v+(h/6) *( k1+k2+4*k3 ) ;

r = r + h*v ;

r e s u l t = [ r e s u l t r ] ;

Page 21: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 20

end

hold on

subplot (1 , 2 , i ) ;

p l o t ( r e s u l t ( 1 , : ) , r e s u l t ( 2 , : ) ) ;

t i t l e ( [ ' k = ' , num2str ( k ( i ) ) ] ) ; x l ab e l ( ' x ' ) ;

y l ab e l ( ' y ' ) ;

ax i s ( [ 0 30 0 15 ] ) ;

end

end

func t i on [ v_prime ] = Motion ( v , k )

r_prime = (v (1 )^2+v (2) ^2) ^(1/2) ;

v_prime = [−k*v (1 ) * r_prime ; −9.81−k*abs (v (2 ) ) * r_prime ] ;

end

Page 22: KTH, DN2221, Computer Lab 2

DN2221 - HS 2012 - Computer Lab 2 - S.Arnoldt & J. Norell 21

B Bibliography

References

[1] L. Edsberg, 2008. Introduction to Computation and Modelling for Dif-

ferential Equations. 1st edition. New Jersey: John Wiley & Sons.

[2] MATLAB, 2012. MATLAB R2012a (7.14.0.739). Natick, MA: The

MathWorks Inc.