代写COSC2500/COSC7500—Semester 2, 2024 MODULE 3. ODES代写R编程

COSC2500/COSC7500—Semester 2, 2024

MODULE 3. ODES

Exercises—due 3:00 pm Friday 11th October

Required

The grade of 1–5 awarded for these exercises is based on the required exercises. If all of the required exercises are completed correctly, a grade of 5 will be obtained.

R3.1 From Sauer computer problems 6.1:

Using Euler’s method and a 4th-order Runge–Kutta method, solve the fol-lowing IVPs on the interval [0, 1] where y(0) = 1 and

a) y ′ = t

b) y ′ = 2(t + 1)y

c) y ′ = 1/y 2

for step sizes h = 0.1 × 2 −k , for 0 ≤ k ≤ 5 (i.e., h = 0.1, 0.05, 0.025, ...). Plot the solutions for h = 0.1, 0.05, 0.025, comparing with the exact solution. Plot a log–log plot of the error at t = 1 as a function of the step size h.

Also compare with the solution obtained using Matlab’s ode45 or another adaptive-step Runge–Kutta code.

A fixed-step Runge–Kutta code, RK4.m, is available on Blackboard.

R3.2 Consider the systems of linear DEs from Sauer computer problems 7.1–7.3, questions 1–2:

a) i. y ′′ = y + 3/2e t , y(0) = 0, y(1) = 3/1e.

ii. y ′′ = (2 + 4t 2 )y, y(0) = 1, y(1) = e.

b) i. y ′′ = 3y − 2y ′ , y(0) = e 3 , y(1) = 1.

a) Solve these DEs using the shooting method.

b) Solve these DEs using the finite difference method. Use the finite dif-ference substitutions for the derivatives to convert the DEs to linear systems, and solve the linear systems using the backslash operator or otherwise.

R3.3 Discuss and critique the following Matlab and C codes. Briefly compare them with the code for Matlab’s ode45 (you can display it in the command window using type ode45 or open it in the editor with open ode45).

function [t ,y , last ] = RK4 (f , tspan , yi , dt )

% Standard RK4 algorithm .

% This code is a standard RK4 algorithm with a

% fixed step - size .

% It can be used to solve explicit first - order ODEs .

% The local truncation error is O( dt ^5).

%" Time " is the name for the x - axis variable .

%

% Input :

% f is a function handle for the first - order ODE

% - dy / dt = f(t ,y)

% - f is assumed to be a row vector .

% tspan is a vector that contains ti and tf ,

% e.g. tspan = [ti , tf ]

% ti is the initial time

% tf is the final time

% yi is the initial values for the ODE .

% - yi is assumed to be a vector .

% dt is the step size

%

% Output :

% t is a vector of time values .

% y is the approximate solution curve for the

% initial value problem of the ODE .

% last is the last y values - useful if you are

% using the last values for something

%

% Hint - call the function like this :

% [T ,Y] = RK4 (f , tspan ,yi , dt )

ti = tspan (1); tf = tspan (2);

num_steps = ceil (( tf - ti )/ dt );

% ceil forces it to be an integer

t = linspace ( ti , tf , num_steps +1). ’;

% creates time vector , then transposes

if size ( yi ,2) > 1

yi = yi . ’;

end

% Initialising y

y = [ yi , zeros ( size (yi ,1) , num_steps )];

% Application of the RK4 algorithm .

for n = 1: num_steps

k1 = dt * f ( t ( n ) , y (: , n ));

k2 = dt * f ( t ( n ) + 0.5* dt , y (: , n ) + 0.5* k1 );

k3 = dt * f ( t ( n ) + 0.5* dt , y (: , n ) + 0.5* k2 );

k4 = dt * f ( t ( n ) + dt , y (: , n ) + k3 );

y (: , n +1) = y (: , n ) + (1/6)*( k1 + 2* k2 + 2* k3 + k4 );

end

y = y . ’;

last = y ( end ,:);

end

void runge4 ( double x , double y [] , double step )

{

double h = step /2.0 , /* the midpoint */

t1 [ N] , t2 [N ] , t3 [N ] ,

k1 [ N] , k2 [N ] , k3 [N ] , k4 [ N ];

int i ;

for ( i =0; i < N ; i ++)

{ t1 [ i ]= y [ i ]+0.5*( k1 [ i ]= step * f (x , y , i ));}

for ( i =0; i < N ; i ++)

{ t2 [ i ]= y [ i ]+0.5*( k2 [ i ]= step * f ( x +h , t1 , i ));}

for ( i =0; i < N ; i ++)

{ t3 [ i ]= y [ i ]+ ( k3 [ i ]= step * f ( x +h , t2 , i ));}

for ( i =0; i < N ; i ++)

{ k4 [ i ]= step * f ( x + step , t3 , i );}

for ( i =0; i < N ; i ++)

{ y [ i ]+=( k1 [ i ]+2* k2 [ i ]+2* k3 [i ]+ k4 [ i ])/6.0;}

}

double f ( double x , double y [] , int i )

{

/* derivative of first equation */

if ( i ==0) return ( exp ( -2* x ) - 3* y [0]);

if ( i !=0) exit (2);

}

R3.4 Discuss, with critical analysis, a paper from the research literature.

A list of papers will be available on Blackboard if you have difficulty finding a suitable paper to discuss.

Additional

Attempts at these exercises can earn additional marks, but will not count towards the grade of 1–5 for the exercises. Completing all of these exercises does not mean that 6 marks will be obtained—the marks depend on the quality of the answers. It is possible to earn all 6 marks without completing all of these additional exercises.

A3.1 Estimate the error in a numerical solution obtained using Euler’s method from the convergence of the solution as the step size is changed. Then com-pare your estimate of the error with actual error determined from compar-ison with the analytical solution.

A3.2 Below is a model that is a variation of the Lotka–Volterra equations (i.e., predator–prey equations):

x ′ = 1.3x − 0.5xy

y ′ = 0.2xy − 0.5y

Choose an ODE solver and solve this system for both x0 and y0 equal to 12 between t = 0 and t = 500. Plot x and y vs t and then a x-y plot. Briefly comment on the behaviour of the system and whether this model seems realistic (e.g., to model a system consisting of predators and prey).

One possible way to solve a system in Matlab using ode45 is to create an anonymous function that returns a column vector. To make it work with most of the Matlab ODE solvers, the inputs should be t and an arbitrary value (say) u. Every incidence of x is replaced with the indexed variable u(1) and every incidence of y is replaced with u(2).

For reference on how you would represent the system in Matlab, the fol-lowing example is provided. For the system

       x ′ = xy + x

y ′ = y

the corresponding anonymous function code is

func = @ (t , u ) [u (1)* u (2) + u (1); u (2)];

% You should not be solving this equation

% for the module exercise ,

% this is an example ...

A3.3 Investigate Sauer computer simulation 6.3.2 (the pendulum). For some spe-cific tasks, see computer problems 6.3.

A3.4 Investigate Sauer computer simulation 6.3.3 (orbital mechanics). For some specific tasks, see computer problems 6.3.

A3.5 Sauer reality check 6—The Tacoma Narrows bridge (Sauer pg 322 in 2E, 328 in 1E). As well as the suggested activities, try using Matlab’s ode45 solver.

A3.6 Simulate the motion of a paper airplane.

(Googling for "paper airplane" "differential equations" lift drag will provide a convenient starting point.)

A3.7 Consider the systems of non-linear DEs from Sauer computer problems 7.1–7.3, questions 3–4:

a) i. y ′′ = 18y 2 , y(1) = 3/1, y(2) = 12/1.

ii. y ′′ = 2e−2y (1 − t 2), y(0) = 0, y(1) = ln2.

b) i. y ′′ = e y , y(0) = 1, y(1) = 3.

ii. y ′′ = sin y ′ , y(0) = 1, y(1) = −1.

Solve these using the finite difference method, using the non-linear code from Sauer or otherwise. You might like to compare your solutions with those obtained using the shooting method.

A3.3 Solve the DEs from the required exercises and/or A3.7 using the finite ele-ment method, collocation method, or similar method.

A3.4 Estimate the error in a numerical solution obtained using the finite dif-ference method from the convergence of the solution as the step size is changed. Then compare your estimate of the error with actual error de-termined from comparison with the analytical solution.

A3.5 Solve the system of equations in Gramotnev and Nieminen 1999. The ap-pendix in the paper describes a finite difference solution of the system. Can you solve it using the shooting method?

A3.6 Model a multi-species predator–prey relationship using a homogeneous linear matrix DE. If we begin with known populations of each species, we have an initial value problem; if If we know a mixture of final and initial populations, we have a BVP. (You might like to try N = 5.)

Programming hints

R3.2(a) For the shooting method, you’re combining and initial value problem solver and a root-finder. You can use whichever IVP solver (from this mod-ule or elsewhere) and root-finder (from Module 2 or elsewhere) you prefer. The only tricks are that

1. Your IVP solver will want a system of 1st order ODEs. You are starting with a second order ODE. So, the first step is to convert the 2nd order ODE into a system of 2 1st order ODEs, as described at the start of Module 4.

2. You need to write a function that the root-finder will find the roots of. For the shooting method, we guess the missing initial condition, and solve the system of ODEs as an IVP. We can then compare our solution to the other boundary condition (which we haven’t used yet). We want the difference between our solutions and this boundary condition to be zero. That’s our root-finding problem. So we need a function like

function final_error = bc_mismatch (x )

% Find the roots of this for R5 .1 shooting method

% Solve the IVP

[t , y ]= ode45 ( @r51_1a ,[0 1] ,[0 x ]);

% Compare with final BC

bc = (1/3) * exp (1);

final_error = y ( end ,1) - bc ;

return

The parameter passed to this function, x, is your guess for the missing initial condition. Since your root-finder will only want to pass one parameter to the function it’s finding roots of, it’s easiest to hard-code things like the ODE system function name, the boundary conditions, etc. in the file as in this example. Depending on which order you write your 2 1st order ODEs in, you might need to swap the order of the 2 boundary conditions passed to your IVP solver, and to compare the final value y(end,2).

As far as your root-finder cares, this function is just another function to find roots of. You can also calculate values for different x and plot a graph; this will be easiest to do using a loop to cycle through the different values of x.

R3.2(b) If you want to solve these as linear systems, you’ll need to write your own code. Once you have your linear system, solving it is easy. There are two steps you will need to do first:

1. Substitute the finite difference approximations into your ODE. Here is an example, with a complicated ODE:

Our ODE: y ′′ = y ′ + t sin t

Our finite difference approximation for y ′′:

y ′′n = (yn+1 − 2yn + yn−1)/h 2

Note that yn = y(tn) and yn+1 = y(tn+1) = y(tn + h).

Let’s use the forward difference for y ′ : y ′ n = (yn+1 − yn)/h.

Finally, we’ll replace t by tn.

So, our ODE becomes:

We want to write this in our usual form. for a linear equation in y1, y2, etc., as An yn−1 + Bn yn + Cn yn+1 = Dn. Re-arranging, we get

2. Generate the linear system. The easy way is to put our initial and final boundary conditions as the 1st and last rows.

% How many points do we want ?

N = 10;

% Pre - allocate memory for matrix and vector

A = zeros (N , N );

c = zeros (N ,1);

tn = linspace (0 ,1 , N ); % Need to change for 2( a),

                                    % since it goes to 1.5

h = t (2) - t (1);

% Boundary conditions

A (1 ,1) = 1;

c (1) = 0; % First BC

A (N , N ) = 1;

c ( N ) = exp (1)/3; % End BC

% Make the matrix

for n = 2:( N -1)

A (n ,n -1) = 1/ h ^2;

A (n , n ) = 1/ h - 2/ h ^2

A (n , n +1) = 2/ h ^2 - 1/ h ;

c ( n ) = tn ( n ) * sin ( tn ( n ));

end

Finally, solve and plot!





热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图