numerical solution of initial value problems

17
%&&$$'() +"# ',-%.,',/ Cover page for assignments Name, personal number, program and email for all students behind this submission : Pierre-Alexandre BEAUFORT, 920104-T093, master in mathematical engineering, [email protected] Hadrien VAN LIERDE, 920909-T058, master in mathematical engineering, [email protected] Course code, course name and program or course set : DN2221, Applied numerical methods - part 1, master of sciences in engineering Teacher: Examiner: Lennart EDSBERG Ashraful KADIR Task name: Deadline: Computer Lab 2 - ODE-systems of LCC-type and stability October 7th 2013 I/We have followed the Code of Honour of the Department of Numerical Analysis when the task was performed. Signature(s): Teacher of the course’s signature: Assessment/Rating: Kungliga Tekniska hogskolan KTH SCI, Lindstedtsvagen 25, 100 44 Stockholm. Tel: 08-790 80 50. Fax: 08-723 17 88. e-mail : [email protected]

Upload: lambdastudentepl

Post on 19-Oct-2015

65 views

Category:

Documents


7 download

DESCRIPTION

Computer Lab 2 Supporting Chapter 3 of "Introduction to Computation and Modeling for Differential Equations"

TRANSCRIPT

  • ! !"#$%&&$$'()* +"#*',-%.,',/$011/'+&2#!

    !!!

    !"#$%!!&!#'(#)*+&,-!./0%&%1"#!2*/,.%%3$$*/4!2/.1/#$!!345!*52.,+! !+"#*)--)*$&0(2,&2#*$3.*$&6#*7)83.*(2,,)*',-%.,',/9*!

    !

    !:0#$83(;*80#$,).,*345*1#3/#).*2--2#*80#$3./6,/9!

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

    !9!

    !?11/'+&2,$*,).,9!

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

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

    !!!!F).,&248,',/=)#>!

    !

    !!!!!

    :0#$-2()#2,$*$'/,)&0#9** I2(".,',/B72&H/9!!!!!!!

    Kungliga Tekniska hgskolan !

    KTH SCI, Lindstedtsvgen 25, 100 44 Stockholm. Tel: 08-790 80 50. Fax: 08-723 17 88. E-post: [email protected]

    Cover page for assignments

    Name, personal number, program and email for all students behind this submission :

    Pierre-Alexandre BEAUFORT, 920104-T093, master in mathematical engineering, [email protected] VAN LIERDE, 920909-T058, master in mathematical engineering, [email protected]

    Course code, course name and program or course set :

    DN2221, Applied numerical methods - part 1, master of sciences in engineering

    Teacher: Examiner:Lennart EDSBERG Ashraful KADIRTask name: Deadline:Computer Lab 2 - ODE-systems of LCC-type and stability October 7th 2013

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

    Signature(s):

    Teacher of thecourses signature:

    Assessment/Rating:

    Kungliga Tekniska hogskolanKTH SCI, Lindstedtsvagen 25, 100 44 Stockholm. Tel: 08-790 80 50. Fax: 08-723 17 88.

    e-mail : [email protected]

  • Contents

    1 Accuracy of Runge-Kutta method . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Stability investigation of a Runge-Kutta method . . . . . . . . . . . . . . . . . . 3

    2.1 Constant Stepsize Experiment . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Adaptive Stepsize Experiment . . . . . . . . . . . . . . . . . . . . . . . . 4

    3 Parameter study of solutions of an ODE-system . . . . . . . . . . . . . . . . . . 7A Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    A.1 rkOrder.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10A.2 RobertsonStabilityConstantstep.m . . . . . . . . . . . . . . . . . . . 10A.3 RobertsonStabilityAdaptstepOde23.m . . . . . . . . . . . . . . . . . . 11A.4 RobertsonStabilityAdaptstepOde23s.m . . . . . . . . . . . . . . . . . 12A.5 DN2221_lab2_Q3p1.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12A.6 DN2221_lab2_Q3p2.m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

    0

  • 1 Accuracy of Runge-Kutta methodLet us consider the following Runge-Kutta method:

    uk = uk1 + h6 (k1 + k2 + 4k3) tk = tk1 + h, k = 1, 2, ..., Nk1 = f(tk1, uk1)k2 = f(tk1 + h, uk1 + hk1)k3 = f(tk1 + h2 , uk1 + h

    k14 + h

    k24 )

    (1)

    We may use this method to solve Van der Pols differential equation :

    d2y

    dt2+ (y2 1)dy

    dt+ y = 0 for

    y(0) = 1dy

    dt(0) = 0t [0; 1]

    (2)

    We solve this equation numerically, using the RK method above. The MATLAB program wewrote is in appendix A.1. We obtain the following results with constant stepsize and using 10,20, 40, 80, 160, 320 steps respectively:

    N yN(t = 1)10 0.49760294892315520 0.49761377697958540 0.49761522114686180 0.497615407556701160 0.497615431235740320 0.497615434219583

    As we do not know the exact value of y(t = 1), we may consider that the last approximation ismuch more accurate than the others : y(1) y320(1). For N much larger than 320, we estimatethe error using N steps by : eN = yN(1) y320(1). To evaluate the order of the method wemake 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 figure 1.

    1

  • 103 102 101109

    108

    107

    106

    105

    104

    Stepsize h

    Erro

    r

    Figure 1: loglog plot of the approximate error in t=1 as a function of the stepsize h. Theerror is evaluated for N = 10, 20, 40, 80, 160 steps.

    From the figure 1, we measure an order of approximately 3. Another method to compute theorder is to use the following MATLAB code (where eN is the vector of errors committed with eachnumber of steps):

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

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

    2

  • 2 Stability investigation of a Runge-Kutta methodWe want to solve the stiff problem known as Robertsons problem. The evolution of theconcentration of the products involved in this problem is modelized by the following system ofdifferential equations :

    dx1dt

    = k1x1 + k2x2x3 x1(0) = 1

    dx2dt

    = k1x1 k2x2x3 k3x22 x2(0) = 0

    dx3dt

    = k3x22 x3(0) = 0

    (3)

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

    2.1 Constant Stepsize ExperimentIf we solve Robertsons problem with an explicit method, such as a Runge-Kutta method, thestepsize has to be very small to avoid numerical instability. This is because these kinds ofmethod have a bounded stability area in the complex plane whereas a stiff problem has the fol-lowing property : the eigenvalues of the Jacobian of the system have real parts of very differentsizes.

    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 appendixA.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 obtainedare similar for the two numbers of steps. The graph 2 shows the solution obtained for thesmallest stepsize (N = 2000) in a loglog-plot.

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

    3

  • 104 103 102 101 100106

    105

    104

    103

    102

    101

    100

    Time(s)

    Conc

    entra

    tion(m

    ol/l)

    N=2000

    x1x2x3

    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 ExperimentWe may use the MATLAB function ode23.m to solve Robertsons problem with adaptive stepsizeon the t-interval [0,1]. The MATLAB code used to solve this problem is in appendix A.3. For therelative tolerances 103, 104, 105 and 106, we obtain the following numbers of steps :

    Relative tolerance Number of steps103 867104 868105 869106 869

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

    4

  • 0 0.2 0.4 0.6 0.8 12

    4

    6

    8

    10

    12

    14

    16x 104

    Time

    Step

    size

    Figure 3: Plot of the stepsize in function of the time for a relative tolerance of 106 when theODE is solved with ode23.m.

    We may also use the function ode23s.m which is specially designed to solve stiff problems. TheMATLAB 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 Number of steps103 31104 38105 49106 62

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

    5

  • 0 200 400 600 800 10000

    10

    20

    30

    40

    50

    60

    70

    Time[s]

    Step

    size[s

    ]

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

    6

  • 3 Parameter study of solutions of an ODE-systemWe are going to study the effect of parameters on the solution of an ODE-system.

    The first problem we consider is the particle flow past a cylinder. The position of the particledepends on time t : (x(t); y(t)). We want to follow the particle through the time from a certaininitial position. This displacement is described by :

    dx

    dt= 1

    (R

    x2 + y2

    )2 (x2 y2)

    dx

    dt= 2xy

    (R

    x2 + y2

    )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 different. We are going to study the effect of this parameter. The figure 5displays the result is obtained by using ode45.m in our MATLAB implementation (see appendixA.5).

    0 2 4 6 8 104

    3

    2

    1

    0

    1

    2

    3

    t [s]

    x(t), y

    (t) [m

    ]

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

    x(t)y(t)

    0 2 4 6 8 104

    3

    2

    1

    0

    1

    2

    3

    4

    t [s]

    x(t), y

    (t) [m

    ]

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

    x(t)y(t)

    0 2 4 6 8 104

    2

    0

    2

    4

    t [s]

    x(t), y

    (t) [m

    ]

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

    x(t)y(t)

    0 2 4 6 8 104

    2

    0

    2

    4

    6

    t [s]

    x(t), y

    (t) [m

    ]

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

    x(t)y(t)

    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 different graph for the y-coordinate. But, wealso observe that the solutions have a different 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 noticethat we would have the same effect with different initial x-coordinates for an identical initialy-coordinate for each particle. This is because the variation of y-coordinate is also conditionedby x-coordinate in the ODE-system (4).

    On the other hand, the graphs have nearly the same shape. It is normal as their variation isdescribed by the same system, on the same interval. The figure 6 displays the solutions in thexy plan ; it confirms the shape of solutions are nearly the same.

    2 0 2

    1

    0

    1

    2

    3

    x(t) [m]

    y(t) [m

    ]

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

    4 2 0 2 4

    101234

    x(t) [m]

    y(t) [m

    ]

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

    4 2 0 2 4

    0

    2

    4

    x(t) [m]

    y(t) [m

    ]

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

    4 2 0 2 4 6

    0

    2

    4

    x(t) [m]

    y(t) [m

    ]

    x(t=0) = 4 [m] and y(t=0) = 1.6 [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 isthrown with an elevation angle [rad] and an initial velocity v0[ms ]. The trajectory of theparticle depends indeed on these parameters and on the air resistance coefficient k[m1]. Thetrajectory is described by :

    d2x

    dt2= kdx

    dt

    (dxdt

    )2+(dt

    dt

    )2x(0) = 0 dx

    dt(0) = v0 cos()

    d2y

    dt2= 9.81 k|dy

    dt|(dx

    dt

    )2+(dt

    dt

    )2y(0) = y0

    dx

    dt(0) = v0 sin()

    (5)

    8

  • 0 5 10 15 20

    2

    0

    2

    4

    6

    8

    x(t) [m]

    y(t) [m

    ]

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

    (x(t);y(t))

    0 5 10 151

    012345

    x(t) [m]

    y(t) [m

    ]

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

    (x(t);y(t))

    0 5 10 15 20 25

    0

    2

    4

    6

    8

    10

    x(t) [m]

    y(t) [m

    ]

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

    (x(t);y(t))

    0 2 4 6 8 10 12 140

    2

    4

    6

    x(t) [m]

    y(t) [m

    ]

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

    (x(t);y(t))

    0 5 10 15 200

    2

    4

    6

    8

    10

    12

    x(t) [m]

    y(t) [m

    ]

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

    (x(t);y(t))

    0 5 10 150

    2

    4

    6

    8

    x(t) [m]

    y(t) [m

    ]

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

    (x(t);y(t))

    Figure 7: Solutions of ODE-system (5) for different values of parameters and k : = [pi6 ;pi4 ;

    pi3 ]

    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 different values ofk = [0.02; 0.065] and three values of = [pi6 ;

    pi4 ;

    pi3 ]. We are working with v0 = 20[

    ms] and

    y0 = 1.5[m]. The figure 7 displays the solutions of ODE-system (5) for all the different combi-nations between the values of and k. The solution is computed by using ode45.m, with anoption for ending the computation of the trajectory when the particle strikes the ground (seeappendix A.6).

    The analysis of the results is quite obvious. The particle strikes further the ground for a weakervalue of k and goes up higher for a greater value of . Here, we observe that for a smaller valueof the particle strikes further the ground.

    9

  • A AppendixA.1 rkOrder.m

    1 f unc t i on order=rk_order ()2 %rk -order: computes the order of a Runge -Kutta method3 % This function computes the solution of Van der Pol s equation using a4 % specific Runge -Kutta method (noted RK) and an approximation of the5 % order of this method. A loglog -graph of the error as a function of the6 % stepsize is also displayed.78 %% Computing the "exact" solution (RK with 320 steps)9 Y=RK(320);10 uexact=Y(1);1112 %% Number of steps and stepsize13 N=[10 20 40 80 160];14 h=1./N;1516 %% Computing the solution for each number of steps with RK method.17 u= z e ro s (1,5);18 f o r j=1:519 Y=RK(N(j));20 u(j)=Y(1);21 end2223 %% Plotting a loglog -graph of the error as a function of the stepsize24 f i g u r e25 l o g l o g (h,abs(u-uexact))26 x l ab e l (Stepsize h)27 y l ab e l (Error)28 g r id on2930 %% Computing the approximate order of the method31 coeff= p o l y f i t ( l og10 (h), l og10 (abs(u-uexact)) ,1);32 order=coeff (1);3334 end3536 %% Function used in RK method37 f unc t i on F=f(u)38 F=[u(2);-u(1)-u(1)^2*u(2)+u(2)];39 end4041 %% RK method42 f unc t i on u=RK(N)43 u=[1;0];44 h=1/N;45 f o r j=1:N46 k1=f(u);47 k2=f(u+h*k1);48 k3=f(u+h*k1/4+h*k2/4);49 u=u+(h/6)*(k1+k2+4*k3);50 end51 end

    A.2 RobertsonStabilityConstantstep.m

    1 f unc t i on Robertson_Stability_Constantstep ()2 %Robertson_Stability_Constantstep: computes and displays the solution of3 %Robertson s problem.4 % This function computes the solution of Robertson s problem using a5 % Runge -Kutta method of order 3 with constant stepsize and for 56 % different numbers of steps. The graphs of the solutions are displayed7 % with a loglog scale. The solutions are computed in time interval [0,1]

    10

  • 89 %% Constants used in Robertson s problem10 k1 =0.04;11 k2=1e4;12 k3=3e7;1314 %% Number of time steps15 N=[125 250 500 1000 2000];1617 %% Computing and plotting the solutions18 f o r j=1:519 x= l i n s p a c e (0,1,N(j)+1);20 Y=RK(N(j));21 f i g u r e22 l o g l o g (x,Y(1,:))23 hold on24 l o g l o g (x,Y(2,:),r)25 l o g l o g (x,Y(3,:),g)26 x l ab e l (Time)27 y l ab e l (Concentration )28 end2930 %% Function used by RK method31 f unc t i on F=f(u)32 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];33 end3435 %% RK method of order 336 f unc t i on u=RK(N)37 u= z e ro s (3,N+1);38 u(:,1) =[1;0;0];39 H=1/N;40 f o r i=1:N41 K1=f(u(:,i));42 K2=f(u(:,i)+H*K1);43 K3=f(u(:,i)+H*K1/4+H*K2/4);44 u(:,i+1)=u(:,i)+(H/6)*(K1+K2+4*K3);45 end46 end47 end

    A.3 RobertsonStabilityAdaptstepOde23.m

    1 f unc t i on N=Robertson_Stability_Adaptstep_ode23 ()2 %Robertson_Stability_Adaptstep_ode23: computes the solution of Robertson s3 %problem.4 % This function computes the solution of Robertson s problem using the5 % function ode23 (second and third order methods). The solution is6 % computed for four different relative tolerances in time interval [0,1].7 % The output variable is the number of time steps used to compute the8 % approximate solution and a graph of stepsize as a function of time is9 % displayed for each relative tolerance.1011 %% Constants used in Robertson s problem12 k1 =0.04;13 k2=1e4;14 k3=3e7;1516 %% Relative tolerances17 tol =[1e-3 1e-4 1e-5 1e-6];1819 %% Computing the solution and the number of time steps for each tolerance20 N= z e ro s (1,4);21 f o r j=1:422 options=odeset(RelTol ,tol(j));23 [Tout ,~]= ode23(@f ,[0 1],[1 0 0],options);24 N(j)= l ength (Tout)

    11

  • 25 f i g u r e26 p lo t (Tout (1:N(j) -1),Tout (2:N(j))-Tout (1:N(j) -1))27 x l ab e l (Time)28 y l ab e l (Stepsize )29 end30 hold on31 x= l i n s p a c e (0 ,1 ,1000);32 p lo t (x,11.2e-4)3334 %% Function used in Runge -Kutta method ODE2335 f unc t i on F=f(t,u)36 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];37 end38 end

    A.4 RobertsonStabilityAdaptstepOde23s.m

    1 f unc t i on Robertson_Stability_Adaptstep_ode23s ()2 %Robertson_Stability_Adaptstep_ode23s: computes the solution of Robertson s3 %problem.4 % This function computes the solution of Robertson s problem using the5 % function ode23s (second and third order methods for stiff problems).6 % The solution is computed for four different relative tolerances in time7 % interval [0 ,1000]. The output variable is the number of time steps used8 % to compute the approximate solution and a graph of the stepsize as a9 % function of time is displayed for each relative tolerance.1011 %% Constants used in Robertson s problem12 c l o s e a l l13 k1 =0.04;14 k2=1e4;15 k3=3e7;1617 %% Relative tolerances18 tol =[1e-3 1e-4 1e-5 1e-6];1920 %% Computing the solution and the number of time steps for each tolerance21 N= z e ro s (1,4);22 f o r j=1:423 options=odeset(RelTol ,tol(j));24 [Tout ,~]= ode23s(@f ,[0 1000] ,[1 0 0],options);25 N(j)= l ength (Tout)26 f i g u r e27 p lo t (Tout (1:N(j) -1),Tout (2:N(j))-Tout (1:N(j) -1))28 x l ab e l (Time[s])29 y l ab e l (Stepsize[s])30 end3132 %% Function used in Runge -Kutta method ODE2333 f unc t i on F=f(t,u)34 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];35 end36 end

    A.5 DN2221_lab2_Q3p1.m

    1 %DN2221 - lab22 %Question 3, first problem - Beaufort & Van Lierde3 %%%%4 %DN2221_lab2_Q3p1 () displays the solutions (t,x(t)) and (t,y(t)) of a5 %particule flow past a cylinder. The solutions are computed by using6 %ode45.m7 %%

    12

  • 8 f unc t i on [] = DN2221_lab2_Q3p1 ()910 R = 2;1112 u0 = [-4 -4 -4 -4 ;0.2 0.6 1 1.6 ];13 T = [0 10];1415 f o r i = 1 : l ength (u0)1617 [t u] = ode45(@f ,T,u0(:,i));1819 s i z e (u)2021 subplot (2,2,i)22 p lo t (t,u,.-);23 ax i s equal24 g r id on25 x l ab e l (t [s]);26 y l ab e l (x(t), y(t) [m]);27 t i t l e ([x(t=0) = num2str(u0(1,i)) [m] and y(t=0) = num2str(u0(2,i)) [m] ] );28 l egend (x(t),y(t));2930 % subplot(2,2,i)31 % plot(u(:,1),u(:,2) ,.-);32 % xlabel(x(t) [m]);33 % ylabel(y(t) [m]);34 % axis equal35 % grid on36 % title([x(t=0) = num2str(u0(1,i)) [m] and y(t=0) = num2str(u0(2,i)) [m] ] );3738 end3940414243 f unc t i on dxdt = fx(t,x,y)4445 dxdt = 1 - (R/(x^2 + y^2))^2 * (x^2 - y^2);4647 end4849 f unc t i on dydt = fy(t,x,y)5051 dydt = -2*x*y*(R/(x^2+y^2))^2;5253 end5455 f unc t i on F = f(t,U)5657 x = U(1);58 y = U(2);5960 Fx = fx(t,x,y);61 Fx = Fx(:);6263 Fy = fy(t,x,y);64 Fy = Fy(:);6566 F = [Fx Fy];6768 end6970 end

    A.6 DN2221_lab2_Q3p2.m

    1 %DN2221 , lab2

    13

  • 2 %Question 3, second problem - Beaufort & Van Lierde3 %%%%4 %DN2221_lab2_Q3p2 () displays the solutions (x(t);y(t)) of the motion of5 %a particle.6 %%7 f unc t i on [] = DN2221_lab2_Q3p2 ()8910 T = [0 1e+18];1112 v0 = 20;13 x0 = 0;14 y0 = 1.5;15 K = [0.02 0.065];16 ALPHA = [ pi /6 pi /4 pi /3];17 END = odeset(Events ,@events);1819202122 f o r i = 1 : l ength (ALPHA)2324 alpha = ALPHA(i);2526 f o r j = 1 : l ength (K)2728 k = K(j);293031 u0 = [x0 y0 v0* cos (alpha) v0* s i n (alpha)];32 [t u] = ode45(@f ,T,u0 ,END);33343536 subplot ( l ength (ALPHA), l ength (K) ,(i-1)* l ength (K)+j)37 p lo t (u(:,1),u(:,2),.-);38 ax i s equal39 g r id on40 x l ab e l (x(t) [m]);41 y l ab e l (y(t) [m]);42 t i t l e ([k = num2str(k) [1/m] and alpha = num2str(alpha) [rad]]);43 l egend ((x(t);y(t)));444546 di sp (i+j-1)47 end4849 end5051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%5354 f unc t i on dxdt = fx(t,x,y,a,b)5556 dxdt = a;5758 end5960 f unc t i on dydt = fy(t,x,y,a,b)6162 dydt = b;6364 end6566 f unc t i on dadt = fa(t,x,y,a,b)6768 dadt = -k*a* s q r t (a^2+b^2);6970 end7172 f unc t i on dbdt = fb(t,x,y,a,b)

    14

  • 7374 dbdt = -9.81 - k*abs(b)* s q r t (a^2+b^2);7576 end7778 f unc t i on F = f(t,U)7980 x = U(1);81 y = U(2);82 a = U(3);83 b = U(4);8485 Fx = fx(t,x,y,a,b);86 Fx = Fx(:);8788 Fy = fy(t,x,y,a,b);89 Fy = Fy(:);9091 Fa = fa(t,x,y,a,b);92 Fa = Fa(:);9394 Fb = fb(t,x,y,a,b);95 Fb = Fb(:);9697 F = [Fx Fy Fa Fb];9899 end100101 %%%%102 %%%%103104 f unc t i on [value ,isterminal ,direction] = events(t,U)105106 value = U(2);107 isterminal = 1;108 direction = -1;109110 end111112 end

    15

    Accuracy of Runge-Kutta methodStability investigation of a Runge-Kutta methodConstant Stepsize ExperimentAdaptive Stepsize Experiment

    Parameter study of solutions of an ODE-systemAppendixrkOrder.mRobertsonStabilityConstantstep.mRobertsonStabilityAdaptstepOde23.mRobertsonStabilityAdaptstepOde23s.mDN2221_lab2_Q3p1.mDN2221_lab2_Q3p2.m