You are on page 1of 17

!

!"#$%&&$$'()* ! Cover page +"#*',-%.,',/$011/'+&2# for assignments


!

! ! !
Name, personal number, program and email for all345 students behind this submission : "#$%! !&!#'(#)*+&,-!./0%&%1 "#!2*/,.%%3$$*/4!2/.1/#$!! !*52.,+! ! Hadrien VAN LIERDE, 920909-T058, master in mathematical engineering, hvl@kth.se

+"#*)--)*$&0(2,&2#*$3.*$&6#*7)83.*(2,,)*',-%.,',/9* Pierre-Alexandre BEAUFORT, 920104-T093, master in mathematical engineering, beaufort@kth.se !

Course code, course name and program or course set : DN2221, Applied numerical methods - part 1, master of sciences in engineering

Teacher: Lennart EDSBERG Task name: :0#$83(;*80#$,).,*345*1#3/#).*2--2#*80#$3./6,/9 Computer Lab 2 - ODE-systems of LCC-type and !stability

Examiner: Ashraful KADIR Deadline: October 7th 2013

I/We have followed the Code of Honour of the Department of Numerical Analysis when the task was performed.

:0#$-2()#29!
Signature(s):

<#),$8)#2*=3.*),,),*%,*80#$-2()#2,>9!

Teacher of the ?11/'+&2,$* ,).,9 ! courses signature:

Assessment/Rating:

@,-%.,',/$()&0.9!

!
!

A)/BC'*5)#*+"-D&*52(2#$83(2E2,*16*)C(2-,',/2,*+"#*F0.2#'$8*G,)-H$* ,%#*011/'+&2,* 0&+"#&$!

Kungliga Tekniska hogskolan KTH SCI, Lindstedtsvagen 25, 100 44 Stockholm. Tel: 08-790 80 50. Fax: 08-723 17 88. e-mail : elevexp@math.kth.se

! ! !

Contents
1 2 Accuracy of Runge-Kutta method . . . . . . . . . Stability investigation of a Runge-Kutta method . 2.1 Constant Stepsize Experiment . . . . . . . 2.2 Adaptive Stepsize Experiment . . . . . . . Parameter study of solutions of an ODE-system . Appendix . . . . . . . . . . . . . . . . . . . . . . A.1 rkOrder.m . . . . . . . . . . . . . . . . . . A.2 RobertsonStabilityConstantstep.m . . A.3 RobertsonStabilityAdaptstepOde23.m . A.4 RobertsonStabilityAdaptstepOde23s.m A.5 DN2221_lab2_Q3p1.m . . . . . . . . . . . . A.6 DN2221_lab2_Q3p2.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 3 3 4 7 10 10 10 11 12 12 13

3 A

Accuracy of Runge-Kutta method


uk k1 k2 k3 = = = = uk 1 + h (k1 + k2 + 4k3 ) tk = tk1 + h, k = 1, 2, ..., N 6 f (tk1 , uk1 ) f (tk1 + h, uk1 + hk1 ) , uk1 + h k41 + h k42 ) f (tk1 + h 2

Let us consider the following Runge-Kutta method:

(1)

We may use this method to solve Van der Pols dierential equation : y (0) = 1 dy d2 y dy (0) = 0 + (y 2 1) + y = 0 for dt dt2 dt t [0; 1]

(2)

We solve this equation numerically, using the RK method above. The MATLAB program we wrote is in appendix A.1. We obtain the following results with constant stepsize and using 10, 20, 40, 80, 160, 320 steps respectively: N 10 20 40 80 160 320 yN (t = 1) 0.497602948923155 0.497613776979585 0.497615221146861 0.497615407556701 0.497615431235740 0.497615434219583

As we do not know the exact value of y (t = 1), we may consider that the last approximation is much more accurate than the others : y (1) y320 (1). For N much larger than 320, we estimate the error using N steps by : eN = yN (1) y320 (1). To evaluate the order of the method we make a loglog-plot of the error as a function of h. The order is then given by the slope of the (nearly) straight line we obtain in gure 1.

10

10

10 Error 10

10

10

10

10 Stepsize h

10

Figure 1: loglog plot of the approximate error in t=1 as a function of the stepsize h. The error is evaluated for N = 10, 20, 40, 80, 160 steps. From the gure 1, we measure an order of approximately 3. Another method to compute the order is to use the following MATLAB code (where eN is the vector of errors committed with each number of steps):
1 2

coeff = p o l y f i t ( l o g 1 0 ( h ) , l o g 1 0 ( eN ) ,1) ; order = coeff (1) ;

This code calculates the slope of the straight line that ts the logarithms of the datas in the least square sense. The result obtained is also 3.

Stability investigation of a Runge-Kutta method

We want to solve the sti problem known as Robertsons problem. The evolution of the concentration of the products involved in this problem is modelized by the following system of dierential equations : dx1 = k1 x1 + k2 x2 x3 dt x1 (0) = 1 (3)

dx2 = k1 x1 k2 x2 x3 k3 x2 2 x2 (0) = 0 dt dx3 = k3 x2 2 dt x3 (0) = 0

The values of the constant rates are k1 = 0.04, k2 = 104 and k3 = 3.107 .

2.1

Constant Stepsize Experiment

If we solve Robertsons problem with an explicit method, such as a Runge-Kutta method, the stepsize has to be very small to avoid numerical instability. This is because these kinds of method have a bounded stability area in the complex plane whereas a sti problem has the following property : the eigenvalues of the Jacobian of the system have real parts of very dierent sizes. We use the Runge-Kutta method given in section 1 in order to solve the problem with N = 125, 250, 500, 1000 and 2000 steps. The MATLAB code used to compute the solution is in appendix A.2. The concentrations obtained with N = 125, 250 and 500 reach physically aberrant values 1 . For N = 1000 and 2000, the concentrations are bounded between 0 and 1. The graphs obtained are similar for the two numbers of steps. The graph 2 shows the solution obtained for the smallest stepsize (N = 2000) in a loglog-plot.

1. More than 1032 for some concentrations, negative values for others.

10

N=2000 x1 x2 x3

10

Concentration(mol/l)

10

10

10

10

10

10

10

10 Time(s)

10

10

Figure 2: loglog-plot of the three concentrations for N = 2000 steps between t = 0 and t = 1. x1 (t), x2 (t) and x3 (t) are respectively in blue, red and green.

2.2

Adaptive Stepsize Experiment

We may use the MATLAB function ode23.m to solve Robertsons problem with adaptive stepsize on the t-interval [0,1]. The MATLAB code used to solve this problem is in appendix A.3. For the relative tolerances 103 , 104 , 105 and 106 , we obtain the following numbers of steps : Relative tolerance 103 104 105 106 Number of steps 867 868 869 869

For a relative tolerance of 106 , we obtain the graph 3 for the stepsize h as a function of t. We observe that the stepsize oscillates around 11.2 104 .

16

x 10

14

12

Stepsize

10

0.2

0.4 Time

0.6

0.8

Figure 3: Plot of the stepsize in function of the time for a relative tolerance of 106 when the ODE is solved with ode23.m. We may also use the function ode23s.m which is specially designed to solve sti problems. The MATLAB code used to solve this problem is appendix A.4. For the relative tolerances 103 , 104 , 105 and 106 and on the time interval [1,1000], we obtain the following number of steps: Relative tolerance 103 104 105 106 Number of steps 31 38 49 62

This shows the eciency of ode23s to solve sti problems, as the number of steps is much smaller than with ode23 whereas the time interval is bigger. With a relative tolerance of 106 , we obtain the graph 4:

70

60

50 Stepsize[s]

40

30

20

10

200

400 Time[s]

600

800

1000

Figure 4: Plot of the stepsize in function of the time for a relative tolerance of 106 when the ODE is solved with ode23s on the time interval [0,1000].

Parameter study of solutions of an ODE-system

We are going to study the eect of parameters on the solution of an ODE-system. The rst problem we consider is the particle ow past a cylinder. The position of the particle depends on time t : (x(t); y (t)). We want to follow the particle through the time from a certain initial position. This displacement is described by :

R dx = 1 ( x2 y 2 ) 2 dt x + y2 2 R dx = 2xy dt x2 + y 2

(4)

Here, we are working with R = 2[m] and we are following 4 particles with initial positions: [(4; 0.2); (4; 0.6); (4; 1); (4; 1.6)]. We notice that the initial positions have only their y coordinate which is dierent. We are going to study the eect of this parameter. The gure 5 displays the result is obtained by using ode45.m in our MATLAB implementation (see appendix A.5).

x(t=0) = 4 [m] and y(t=0) = 0.2 [m] 3 2 1 x(t), y(t) [m] x(t), y(t) [m] 0 1 2 3 4 0 2 4 t [s] 6 8 10 x(t) y(t) 4 3 2 1 0 1 2 3 4 0

x(t=0) = 4 [m] and y(t=0) = 0.6 [m]

x(t) y(t)

4 t [s]

10

x(t=0) = 4 [m] and y(t=0) = 1 [m] 6 4

x(t=0) = 4 [m] and y(t=0) = 1.6 [m]

4 x(t), y(t) [m] x(t) y(t)

x(t), y(t) [m]

0 x(t) y(t)

4 t [s]

10

4 t [s]

10

Figure 5: Solutions of ODE-system (4) for 4 particles with initial positions [(4; 0.2); (4; 0.6); (4; 1); (4; 1.6)]. The solutions are computed by using ode45.m. x(t) is in blue and y (t) is in green. Obviously, we observe that the solutions have a dierent graph for the y -coordinate. But, we also observe that the solutions have a dierent graph for x-coordinate. Actually, the variation 7

of x-coordinate in the ODE-system (4) depends on the y -coordinate. By the way, we notice that we would have the same eect with dierent initial x-coordinates for an identical initial y -coordinate for each particle. This is because the variation of y -coordinate is also conditioned by x-coordinate in the ODE-system (4). On the other hand, the graphs have nearly the same shape. It is normal as their variation is described by the same system, on the same interval. The gure 6 displays the solutions in the xy plan ; it conrms the shape of solutions are nearly the same.

x(t=0) = 4 [m] and y(t=0) = 0.2 [m] 3 y(t) [m] y(t) [m] 0 2 x(t) [m] x(t=0) = 4 [m] and y(t=0) = 1 [m] 4 y(t) [m] 2 0 y(t) [m] 2 2 1 0 1 4 3 2 1 0 1

x(t=0) = 4 [m] and y(t=0) = 0.6 [m]

0 2 4 x(t) [m] x(t=0) = 4 [m] and y(t=0) = 1.6 [m]

4 2 0

0 2 x(t) [m]

0 2 x(t) [m]

Figure 6: Solutions of ODE-system (4) for 4 particles with initial positions [(4; 0.2); (4; 0.6); (4; 1); (4; 1.6)]. The solutions are computed by using ode45.m. The second problem we are going to analyze is the motion of a particle. This particle is thrown with an elevation angle [rad] and an initial velocity v0 [ m ]. The trajectory of the s particle depends indeed on these parameters and on the air resistance coecient k [m1 ]. The trajectory is described by :

d2 x dx = k 2 dt dt

dx dt

dt dt dx dt
2

x(0) = 0 dt + dt
2

dx (0) = v0 cos() dt dx (0) = v0 sin() dt

d2 y dy = 9 . 81 k | | dt2 dt

(5)

y (0) = y0

k = 0.02 [1/m] and alpha = 0.5236[rad] 8 6 y(t) [m] y(t) [m] 4 2 0 2 0 5 10 x(t) [m] k = 0.02 [1/m] and alpha = 0.7854[rad] 10 6 8 y(t) [m] y(t) [m] 6 4 2 0 0 0 5 10 x(t) [m] k = 0.02 [1/m] and alpha = 1.0472[rad] 12 10 y(t) [m] y(t) [m] 8 6 4 2 0 0 5 10 x(t) [m] 15 20 2 0 (x(t);y(t)) 6 4 8 15 20 25 0 2 (x(t);y(t)) 4 15 20 (x(t);y(t)) 5 4 3 2 1 0 1 0

k = 0.065 [1/m] and alpha = 0.5236[rad]

(x(t);y(t))

5 x(t) [m]

10

15

k = 0.065 [1/m] and alpha = 0.7854[rad]

(x(t);y(t)) 2

8 x(t) [m]

10

12

14

k = 0.065 [1/m] and alpha = 1.0472[rad]

(x(t);y(t))

5 x(t) [m]

10

15

Figure 7: Solutions of ODE-system (5) for dierent values of parameters and k : = [ ; ; ] 6 4 3 in respect of the rows and k = [0.02; 0.065] in respect of the columns. Here, we are going to observe the solution of the ODE-system (5) for two dierent values of k = [0.02; 0.065] and three values of = [ ; ; ]. We are working with v0 = 20[ m ] and 6 4 3 s y0 = 1.5[m]. The gure 7 displays the solutions of ODE-system (5) for all the dierent combinations between the values of and k . The solution is computed by using ode45.m, with an option for ending the computation of the trajectory when the particle strikes the ground (see appendix A.6). The analysis of the results is quite obvious. The particle strikes further the ground for a weaker value of k and goes up higher for a greater value of . Here, we observe that for a smaller value of the particle strikes further the ground.

A
A.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

Appendix
rkOrder.m

f u n c t i o n order = rk_order () % rk - order : computes the order of a Runge - Kutta method % This function computes the solution of Van der Pol s equation using a % specific Runge - Kutta method ( noted RK ) and an approximation of the % order of this method . A loglog - graph of the error as a function of the % stepsize is also displayed . % % Computing the " exact " solution ( RK with 320 steps ) Y = RK (320) ; uexact = Y (1) ; % % Number of steps and stepsize N =[10 20 40 80 160]; h =1./ N ; % % Computing the solution for each number of steps with RK method . u = z e r o s (1 ,5) ; f o r j =1:5 Y = RK ( N ( j ) ) ; u ( j ) = Y (1) ; end % % Plotting a loglog - graph of the error as a function of the stepsize figure l o g l o g (h , abs (u - uexact ) ) x l a b e l ( Stepsize h ) y l a b e l ( Error ) g r i d on % % Computing the approximate order of the method coeff = p o l y f i t ( l o g 1 0 ( h ) , l o g 1 0 ( abs (u - uexact ) ) ,1) ; order = coeff (1) ; end % % Function used in RK method f u n c t i o n F=f(u) F =[ u (2) ; - u (1) -u (1) ^2* u (2) + u (2) ]; end % % RK method f u n c t i o n u = RK ( N ) u =[1;0]; h =1/ N ; f o r j =1: N k1 = f ( u ) ; k2 = f ( u + h * k1 ) ; k3 = f ( u + h * k1 /4+ h * k2 /4) ; u = u +( h /6) *( k1 + k2 +4* k3 ) ; end end

A.2
1 2 3 4 5 6 7

RobertsonStabilityConstantstep.m

f u n c t i o n R o b e r t s o n _ S t a b i l i t y _ C o n s t a n t s t e p () % R o b e r t s o n _ S t a b i l i t y _ C o n s t a n t s t e p : computes and displays the solution of % Robertson s problem . % This function computes the solution of Robertson s problem using a % Runge - Kutta method of order 3 with constant stepsize and for 5 % different numbers of steps . The graphs of the solutions are displayed % with a loglog scale . The solutions are computed in time interval [0 ,1]

10

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

% % Constants used in Robertson s problem k1 =0.04; k2 =1 e4 ; k3 =3 e7 ; % % Number of time steps N =[125 250 500 1000 2000]; % % Computing and plotting the solutions f o r j =1:5 x = l i n s p a c e (0 ,1 , N ( j ) +1) ; Y = RK ( N ( j ) ) ; figure l o g l o g (x , Y (1 ,:) ) h o l d on l o g l o g (x , Y (2 ,:) , r ) l o g l o g (x , Y (3 ,:) , g ) x l a b e l ( Time ) y l a b e l ( Concentration ) end % % Function used by RK method f u n c t i o n F=f(u) F =[ - k1 * u (1) + k2 * u (2) * u (3) ; k1 * u (1) - k2 * u (2) * u (3) - k3 * u (2) ^2; k3 * u (2) ^2]; end % % RK method of order 3 f u n c t i o n u = RK ( N ) u = z e r o s (3 , N +1) ; u (: ,1) =[1;0;0]; H =1/ N ; f o r i =1: N K1 = f ( u (: , i ) ) ; K2 = f ( u (: , i ) + H * K1 ) ; K3 = f ( u (: , i ) + H * K1 /4+ H * K2 /4) ; u (: , i +1) = u (: , i ) +( H /6) *( K1 + K2 +4* K3 ) ; end end end

A.3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

RobertsonStabilityAdaptstepOde23.m

f u n c t i o n N = R o b e r t s o n _ S t a b i l i t y _ A d a p t s t e p _ o d e 2 3 () % R o b e r t s o n _ S t a b i l i t y _ A d a p t s t e p _ o d e 2 3 : computes the solution of Robertson s % problem . % This function computes the solution of Robertson s problem using the % function ode23 ( second and third order methods ) . The solution is % computed for four different relative tolerances in time interval [0 ,1]. % The output variable is the number of time steps used to compute the % approximate solution and a graph of stepsize as a function of time is % displayed for each relative tolerance . % % Constants used in Robertson s problem k1 =0.04; k2 =1 e4 ; k3 =3 e7 ; % % Relative tolerances tol =[1 e -3 1e -4 1e -5 1e -6]; % % Computing the solution and the number of time steps for each tolerance N = z e r o s (1 ,4) ; f o r j =1:4 options = odeset ( RelTol , tol ( j ) ) ; [ Tout ,~]= ode23 ( @f ,[0 1] ,[1 0 0] , options ) ; N ( j ) = l e n g t h ( Tout )

11

25 26 27 28 29 30 31 32 33 34 35 36 37 38

figure p l o t ( Tout (1: N ( j ) -1) , Tout (2: N ( j ) ) - Tout (1: N ( j ) -1) ) x l a b e l ( Time ) y l a b e l ( Stepsize ) end h o l d on x = l i n s p a c e (0 ,1 ,1000) ; p l o t (x ,11.2 e -4) % % Function used in Runge - Kutta method ODE23 f u n c t i o n F = f (t , u ) F =[ - k1 * u (1) + k2 * u (2) * u (3) ; k1 * u (1) - k2 * u (2) * u (3) - k3 * u (2) ^2; k3 * u (2) ^2]; end end

A.4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

RobertsonStabilityAdaptstepOde23s.m

f u n c t i o n R o b e r t s o n _ S t a b i l i t y _ A d a p t s t e p _ o d e 2 3 s () % R o b e r t s o n _ S t a b i l i t y _ A d a p t s t e p _ o d e 2 3 s : computes the solution of Robertson s % problem . % This function computes the solution of Robertson s problem using the % function ode23s ( second and third order methods for stiff problems ) . % The solution is computed for four different relative tolerances in time % interval [0 ,1000]. The output variable is the number of time steps used % to compute the approximate solution and a graph of the stepsize as a % function of time is displayed for each relative tolerance . % % Constants used in Robertson s problem close all k1 =0.04; k2 =1 e4 ; k3 =3 e7 ; % % Relative tolerances tol =[1 e -3 1e -4 1e -5 1e -6]; % % Computing the solution and the number of time steps for each tolerance N = z e r o s (1 ,4) ; f o r j =1:4 options = odeset ( RelTol , tol ( j ) ) ; [ Tout ,~]= ode23s ( @f ,[0 1000] ,[1 0 0] , options ) ; N ( j ) = l e n g t h ( Tout ) figure p l o t ( Tout (1: N ( j ) -1) , Tout (2: N ( j ) ) - Tout (1: N ( j ) -1) ) x l a b e l ( Time [ s ] ) y l a b e l ( Stepsize [ s ] ) end % % Function used in Runge - Kutta method ODE23 f u n c t i o n F = f (t , u ) F =[ - k1 * u (1) + k2 * u (2) * u (3) ; k1 * u (1) - k2 * u (2) * u (3) - k3 * u (2) ^2; k3 * u (2) ^2]; end end

A.5
1 2 3 4 5 6 7

DN2221_lab2_Q3p1.m

% DN2221 - lab2 % Question 3 , first problem - Beaufort & Van Lierde % %%% % D N 2 2 2 1 _ l a b 2 _ Q 3 p1 () displays the solutions (t , x ( t ) ) and (t , y ( t ) ) of a % particule flow past a cylinder . The solutions are computed by using % ode45 . m %%

12

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

f u n c t i o n [] = D N 2 2 2 1 _ la b 2 _ Q 3 p 1 () R = 2; u0 = [ -4 -4 -4 -4 ;0.2 0.6 1 1.6 ]; T = [0 10]; f o r i = 1 : l e n g t h ( u0 ) [ t u ] = ode45 ( @f ,T , u0 (: , i ) ) ; s i z e (u) s u b p l o t (2 ,2 , i ) p l o t (t ,u , . - ) ; a x i s equal g r i d on x l a b e l ( t [ s ] ) ; y l a b e l ( x ( t ) , y ( t ) [ m ] ) ; t i t l e ([ x ( t =0) = num2str ( u0 (1 , i ) ) [ m ] and y ( t =0) = num2str ( u0 (2 , i ) ) [ m ] ] ) ; l e g e n d ( x ( t ) , y ( t ) ) ; % % % % % % % subplot (2 ,2 , i ) plot ( u (: ,1) ,u (: ,2) , . - ) ; xlabel ( x ( t ) [ m ] ) ; ylabel ( y ( t ) [ m ] ) ; axis equal grid on title ([ x ( t =0) = num2str ( u0 (1 , i ) ) [ m ] and y ( t =0) = num2str ( u0 (2 , i ) ) [ m ] ] ) ; end

f u n c t i o n dxdt = fx (t ,x , y ) dxdt = 1 - ( R /( x ^2 + y ^2) ) ^2 * ( x ^2 - y ^2) ; end f u n c t i o n dydt = fy (t ,x , y ) dydt = -2* x * y *( R /( x ^2+ y ^2) ) ^2; end f u n c t i o n F = f (t , U ) x = U (1) ; y = U (2) ; Fx = fx (t ,x , y ) ; Fx = Fx (:) ; Fy = fy (t ,x , y ) ; Fy = Fy (:) ; F = [ Fx Fy ] ; end end

A.6
1

DN2221_lab2_Q3p2.m

% DN2221 , lab2

13

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

% Question 3 , second problem - Beaufort & Van Lierde % %%% % D N 2 2 2 1 _ l a b 2 _ Q 3 p2 () displays the solutions ( x ( t ) ; y ( t ) ) of the motion of % a particle . %% f u n c t i o n [] = D N 2 2 2 1 _ la b 2 _ Q 3 p 2 ()

T = [0 1 e +18]; v0 = 20; x0 = 0; y0 = 1.5; K = [0.02 0.065]; ALPHA = [ p i /6 p i /4 p i /3]; END = odeset ( Events , @events ) ;

f o r i = 1 : l e n g t h ( ALPHA ) alpha = ALPHA ( i ) ; f o r j = 1 : length (K) k = K(j);

u0 = [ x0 y0 v0 * c o s ( alpha ) v0 * s i n ( alpha ) ]; [ t u ] = ode45 ( @f ,T , u0 , END ) ;

s u b p l o t ( l e n g t h ( ALPHA ) , l e n g t h ( K ) ,(i -1) * l e n g t h ( K ) + j ) p l o t ( u (: ,1) ,u (: ,2) , . - ) ; a x i s equal g r i d on x l a b e l ( x ( t ) [ m ] ) ; y l a b e l ( y ( t ) [ m ] ) ; t i t l e ([ k = num2str ( k ) [1/ m ] and alpha = num2str ( alpha ) [ rad ] ]) ; l e g e n d ( ( x ( t ) ; y ( t ) ) ) ;

d i s p ( i +j -1) end end % %%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%% f u n c t i o n dxdt = fx (t ,x ,y ,a , b ) dxdt = a ; end f u n c t i o n dydt = fy (t ,x ,y ,a , b ) dydt = b ; end f u n c t i o n dadt = fa (t ,x ,y ,a , b ) dadt = -k * a * s q r t ( a ^2+ b ^2) ; end f u n c t i o n dbdt = fb (t ,x ,y ,a , b )

14

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

dbdt = -9.81 - k * abs ( b ) * s q r t ( a ^2+ b ^2) ; end f u n c t i o n F = f (t , U ) x y a b = = = = U (1) ; U (2) ; U (3) ; U (4) ;

Fx = fx (t ,x ,y ,a , b ) ; Fx = Fx (:) ; Fy = fy (t ,x ,y ,a , b ) ; Fy = Fy (:) ; Fa = fa (t ,x ,y ,a , b ) ; Fa = Fa (:) ; Fb = fb (t ,x ,y ,a , b ) ; Fb = Fb (:) ; F = [ Fx Fy Fa Fb ] ; end % %%% % %%% f u n c t i o n [ value , isterminal , direction ] = events (t , U ) value = U (2) ; isterminal = 1; direction = -1; end end

15

You might also like