203
18.13 Note that students can take two approaches to developing this M-file. The first program
shown below is strictly developed to solve 2 equations.
function [t,y1,y2] = rk42dy1dt,dy2dt,tspan,y10,y20,h [t,y1,y2] = rk42dy1dt,dy2dt,tspan,y10,y20,h:
uses the fourth-order RK method to integrate a pair of ODEs input:
dy1dt = name of the M-file that evaluates the first ODE dy2dt = name of the M-file that evaluates the second ODE
tspan = [ti, tf] where ti and tf = initial and final values of independent variable
y10 = initial value of first dependent variable y20 = initial value of second dependent variable
h = step size output:
t = vector of independent variable y1 = vector of solution for first dependent variable
y2 = vector of solution for second dependent variable ti = tspan1;
tf = tspan2; t = ti:h:tf;
n = lengtht; if necessary, add an additional value of t
so that range goes from t = ti to tf if tntf
tn+1 = tf; n = n+1;
end y1 = y10onesn,1; preallocate ys to improve efficiency
y2 = y20onesn,1; for i = 1:n-1
hh = ti+1 - ti; k11 = fevaldy1dt,ti,y1i,y2i;
k12 = fevaldy2dt,ti,y1i,y2i; ymid1 = y1i + k11hh2;
ymid2 = y2i + k12hh2; k21 = fevaldy1dt,ti+hh2,ymid1,ymid2;
k22 = fevaldy2dt,ti+hh2,ymid1,ymid2; ymid1 = y1i + k21hh2;
ymid2 = y2i + k22hh2; k31 = fevaldy1dt,ti+hh2,ymid1,ymid2;
204
k32 = fevaldy2dt,ti+hh2,ymid1,ymid2; yend1 = y1i + k31hh;
yend2 = y2i + k32hh; k41 = fevaldy1dt,ti+hh,yend1,yend2;
k42 = fevaldy2dt,ti+hh,yend1,yend2; phi1 = k11+2k21+k31+k416;
phi2 = k12+2k22+k32+k426; y1i+1 = y1i + phi1hh;
y2i+1 = y2i + phi2hh; end
plott,y1,t,y2,--
Here is the test of the solution of Prob. 18.7. First, M-files holding the differential equations are written as
function dy = dy1dtt, y1, y2 dy = -2y1 + 5y2exp-t;
function dy = dy2dtt, y1, y2 dy = -y1y222;
Then the M-file can be invoked as in [t,y1,y2]=rk42dy1dt,dy2dt,[0 0.4],2,4,0.1;
disp[t,y1,y2] 0 2.0000 4.0000
0.1000 3.0410 2.6286 0.2000 3.3426 1.8453
0.3000 3.3020 1.4106 0.4000 3.1078 1.1500
The following plot is generated
A better approach is to develop an M-file that can be used for any number of simultaneous first-order ODEs as in the following code:
function [t,y] = rk4sysdydt,tspan,y0,h
205
[t,y] = rk4sysdydt,tspan,y0,h: uses the fourth-order RK method to integrate a pair of ODEs
input: dydt = name of the M-file that evaluates the ODEs
tspan = [ti, tf] where ti and tf = initial and final values of independent variable
y0 = initial values of dependent variables h = step size
output: t = vector of independent variable
y = vector of solution for dependent variables ti = tspan1;
tf = tspan2; t = ti:h:tf;
n = lengtht; if necessary, add an additional value of t
so that range goes from t = ti to tf if tntf
tn+1 = tf; n = n+1;
end y1,: = y0;
for i = 1:n-1 hh = ti+1 - ti;
k1 = fevaldydt,ti,yi,:; ymid = yi,: + k1hh2;
k2 = fevaldydt,ti+hh2,ymid; ymid = yi,: + k2hh2;
k3 = fevaldydt,ti+hh2,ymid; yend = yi,: + k3hh;
k4 = fevaldydt,ti+hh,yend; phi = k1+2k2+k3+k46;
yi+1,: = yi,: + phihh; end
plott,y:,1,t,y:,2,--
This code solves as many ODEs as are specified. Here is the test of the solution of Prob. 18.7. First, a single M-file holding the differential equations can be written as
function dy = dydtsyst, y dy = [-2y1 + 5y2exp-t;-y1y222];
Then the M-file can be invoked as in [t,y]=rk4sysdydtsys,[0 0.4],[2 4],0.1;
disp[t,y] 0 2.0000 4.0000
0.1000 3.0410 2.6286 0.2000 3.3426 1.8453
0.3000 3.3020 1.4106 0.4000 3.1078 1.1500
206
CHAPTER 19
19.1
a Euler’s method. Here are the first few steps t x
y dxdt dydt 0 2.0000 1.0000 1.2000
-0.2000 0.1 2.1200 0.9800 1.2974
-0.1607 0.2 2.2497 0.9639 1.3985
-0.1206 0.3 2.3896 0.9519 1.5028
-0.0791 0.4 2.5399 0.9440 1.6093
-0.0359 0.5 2.7008 0.9404 1.7171
0.0096 The computation can be continued and the results plotted versus time:
4 8
12 16
5 10
15 20
25 30
x y
Notice that the amplitudes of the oscillations are expanding. This is also illustrated by a state-space plot y versus x:
4 8
12 16
4 8
12 16
b
RK4. Here is the first step in detail.
1 .
1 2
4 .
1 9
. 1
, 2
, 6
. 1
1 2
7 .
2 5
. 1
1 ,
2 ,
2 2
, 1
1 1
, 1
− =
+ −
= =
= −
= =
f k
f k
207 995
. 05
. 1
. 1
05 .
08 .
2 05
. 6
. 1
2 05
. =
− =
= +
= y
x
06766 .
995 .
, 08
. 2
, 05
. 67128
. 1
995 .
, 08
. 2
, 05
.
2 2
, 2
1 1
, 2
− =
= =
= f
k f
k
996617 .
05 .
9 1
05 .
083564 .
2 05
. 67128
. 1
2 05
. =
− =
= +
= y
x
06635 .
996617 .
, 083564
. 2
, 05
. 671785
. 1
996617 .
, 083564
. 2
, 05
.
2 2
, 3
1 1
, 3
− =
= =
= f
k f
k
993365 .
1 .
06635 .
1 1
. 167179
. 2
1 .
671785 .
1 2
1 .
= −
= =
+ =
y x
03291 .
993365 .
, 167179
. 2
, 1
. 743808
. 1
993365 .
, 167179
. 2
, 1
.
2 2
, 4
1 1
, 4
− =
= =
= f
k f
k The k’s can then be used to compute the increment functions,
06682 .
6 03291
. 06635
. 06766
. 2
1 .
671656 .
1 6
743808 .
1 671785
. 1
67128 .
1 2
6 .
1
2 1
− =
− −
− +
− =
= +
+ +
=
φ φ
These slope estimates can then be used to make the prediction for the first step
993318 .
1 .
06682 .
1 1
. 16766
. 2
1 .
671656 .
1 2
1 .
= −
= =
+ =
y x
The remaining steps can be taken in a similar fashion and the first few results summarized as
t x y 0 2
1 0.1 2.167166
0.993318 0.2 2.348838
0.993588 0.3 2.545029
1.001398 0.4 2.755314
1.017509 0.5 2.978663
1.042891
208 A plot of all the values can be developed. Note that in contrast to Euler’s method, the
cycles do not amplify as time proceeds.
2 4
6
5 10
15 20
25 30
x y
This periodic nature is also evident from the state-space plot. Because this is the expected behavior we can see that the RK4 is far superior to Euler’s method for this particular
problem.
1 2
3 4
5
1 2
3 4
5
c
To implement ode45, first a function is developed to evaluate the predator-prey ODEs, function yp = predpreyt,y
yp = [1.5y1-0.7y1y2;-0.9y2+0.4y1y2]; Then, the solution and plot can be obtained:
[t,y] = ode45predprey,[0 30],[2 1]; plott,y:,1,t,y:,2,--
legendxprey,ypredator
209
19.2 a