Recall that the analytical solution for velocity is [Eq. 1.9]: v
t =
gm c
d
tanh gc
d
m t
This result can be substituted into Eq. 22.47 which can be integrated to determine an analytical solution for distance as
x t
= m
c
d
ln cosh
gc
d
m t
Use these analytical solutions to compute the true relative errors of the results. Solution.
The ODEs can be used to compute the slopes at t = 0 as
d x dt
= 0 dv
dt = 9.81 −
0.25 68.1
2
= 9.81 Euler’s method is then used to compute the values at t
= 2 s, x
= 0 + 02 = 0 v
= 0 + 9.812 = 19.62 The analytical solutions can be computed as x2
= 19.16629 and v2 = 18.72919. Thus, the percent relative errors are 100 and 4.756, respectively.
The process can be repeated to compute the results at t = 4 as
x = 0 + 19.622 = 39.24
v = 19.62 +
9.81 −
0.25 68.1
19.62
2
2 = 36.41368
Proceeding in a like manner gives the results displayed in Table 22.3.
22.5 SYSTEMS OF EQUATIONS 573
TABLE 22.3 Distance and velocity of a free-falling bungee jumper as computed
numerically with Euler’s method.
t x
true
v
true
x
Euler
v
Euler
ε
t
x
ε
t
v
2 19.1663
18.7292 19.6200
100.00 4.76
4 71.9304
33.1118 39.2400
36.4137 45.45
9.97 6
147.9462 42.0762
112.0674 46.2983
24.25 10.03
8 237.5104
46.9575 204.6640
50.1802 13.83
6.86 10
334.1782 49.4214
305.0244 51.3123
8.72 3.83
Although the foregoing example illustrates how Euler’s method can be implemented for systems of ODEs, the results are not very accurate because of the large step size. In addition,
the results for distance are a bit unsatisfying because x does not change until the second iteration. Using a much smaller step greatly mitigates these deficiencies. As described next,
using a higher-order solver provides decent results even with a relatively large step size.
22.5.2 Runge-Kutta Methods
Note that any of the higher-order RK methods in this chapter can be applied to systems of equations. However, care must be taken in determining the slopes. Figure 22.7 is helpful in
visualizing the proper way to do this for the fourth-order method. That is, we first develop slopes for all variables at the initial value. These slopes a set of k
1
’s are then used to make predictions of the dependent variable at the midpoint of the interval. These midpoint val-
ues are in turn used to compute a set of slopes at the midpoint the k
2
’s. These new slopes are then taken back to the starting point to make another set of midpoint predictions that
lead to new slope predictions at the midpoint the k
3
’s. These are then employed to make predictions at the end of the interval that are used to develop slopes at the end of the inter-
val the k
4
’s. Finally, the k’s are combined into a set of increment functions [as in Eq. 22.44] that are brought back to the beginning to make the final predictions. The fol-
lowing example illustrates the approach. EXAMPLE 22.5
Solving Systems of ODEs with the Fourth-Order RK Method Problem Statement.
Use the fourth-order RK method to solve for the same problem we addressed in Example 22.4.
Solution. First, it is convenient to express the ODEs in the functional format of
Eq. 22.46 as d x
dt = f
1
t, x, v = v
dv dt
= f
2
t, x, v = g −
c
d
m v
2
The first step in obtaining the solution is to solve for all the slopes at the beginning of the interval:
k
1,1
= f
1
0, 0, 0 = 0
k
1,2
= f
2
0, 0, 0 = 9.81 −
0.25 68.1
2
= 9.81 where k
i, j
is the ith value of k for the jth dependent variable. Next, we must calculate the first values of x and v at the midpoint of the first step:
x 1
= x0 + k
1,1
h 2
= 0 + 0 2
2 = 0
v 1
= v0 + k
1,2
h 2
= 0 + 9.81 2
2 = 9.81
which can be used to compute the first set of midpoint slopes: k
2,1
= f
1
1, 0, 9.81 = 9.8100
k
2,2
= f
2
1, 0, 9.81 = 9.4567
These are used to determine the second set of midpoint predictions: x
1 = x0 + k
2,1
h 2
= 0 + 9.8100 2
2 = 9.8100
v 1
= v0 + k
2,2
h 2
= 0 + 9.4567 2
2 = 9.4567
which can be used to compute the second set of midpoint slopes: k
3,1
= f
1
1, 9.8100, 9.4567 = 9.4567
k
3,2
= f
2
1, 9.8100, 9.4567 = 9.4817
These are used to determine the predictions at the end of the interval: x
2 = x0 + k
3,1
h = 0 + 9.45672 = 18.9134
v 2
= v0 + k
3,2
h = 0 + 9.48172 = 18.9634
which can be used to compute the endpoint slopes: k
4,1
= f
1
2, 18.9134, 18.9634 = 18.9634
k
4,2
= f
2
2, 18.9134, 18.9634 = 8.4898
The values of k can then be used to compute [Eq. 22.44]: x
2 = 0 +
1 6
[0 + 29.8100 + 9.4567 + 18.9634] 2 = 19.1656
v 2
= 0 + 1
6 [9.8100
+ 29.4567 + 9.4817 + 8.4898] 2 = 18.7256 Proceeding in a like manner for the remaining steps yields the values displayed in
Table 22.4. In contrast to the results obtained with Euler’s method, the fourth-order RK predictions are much closer to the true values. Further, a highly accurate, nonzero value is
computed for distance on the first step.
22.5 SYSTEMS OF EQUATIONS 575
TABLE 22.4 Distance and velocity of a free-falling bungee jumper as computed
numerically with the fourth-order RK method.
t x
true
v
true
x
RK4
v
RK4
ε
t
x
ε
t
v
2 19.1663
18.7292 19.1656
18.7256 0.004
0.019 4
71.9304 33.1118
71.9311 33.0995
0.001 0.037
6 147.9462
42.0762 147.9521
42.0547 0.004
0.051 8
237.5104 46.9575
237.5104 46.9345
0.000 0.049
10 334.1782
49.4214 334.1626
49.4027 0.005
0.038
22.5.3 MATLAB M-file Function:
rk4sys
Figure 22.8 shows an M-file called
rk4sys
that uses the fourth-order Runge-Kutta method to solve a system of ODEs. This code is similar in many ways to the function developed
earlier Fig. 22.3 to solve a single ODE with Euler’s method. For example, it is passed the function name defining the ODEs through its argument.
FIGURE 22.8
An M-file to implement the RK4 method for a system of ODEs.
function [tp,yp] = rk4sysdydt,tspan,y0,h,varargin rk4sys: fourth-order Runge-Kutta for a system of ODEs
[t,y] = rk4sysdydt,tspan,y0,h,p1,p2,...: integrates a system of ODEs with fourth-order RK method
input: dydt = name of the M-file that evaluates the ODEs
tspan = [ti, tf]; initial and final times with output
generated at interval of h, or = [t0 t1 ... tf]; specific times where solution output
y0 = initial values of dependent variables h = step size
p1,p2,... = additional parameters used by dydt output:
tp = vector of independent variable yp = vector of solution for dependent variables
if nargin4,errorat least 4 input arguments required, end if anydifftspan=0,errortspan not ascending order, end
n = lengthtspan; ti = tspan1;tf = tspann;
if n == 2
t = ti:h:tf; n = lengtht; if tntf
tn+1 = tf; n = n+1;
end else
t = tspan; end
tt = ti; y1,: = y0; np = 1; tpnp = tt; ypnp,: = y1,:;
i=1; while1
tend = tnp+1; hh = tnp+1 - tnp;
Continued
22.5 SYSTEMS OF EQUATIONS 577
FIGURE 22.8 Continued
if hhh,hh = h;end while1
if tt+hhtend,hh = tend-tt;end k1 = dydttt,yi,:,varargin{:};
ymid = yi,: + k1.hh.2; k2 = dydttt+hh2,ymid,varargin{:};
ymid = yi,: + k2hh2; k3 = dydttt+hh2,ymid,varargin{:};
yend = yi,: + k3hh; k4 = dydttt+hh,yend,varargin{:};
phi = k1+2k2+k3+k46; yi+1,: = yi,: + phihh;
tt = tt+hh; i=i+1;
if tt=tend,break,end
end np = np+1; tpnp = tt; ypnp,: = yi,:;
if tt=tf,break,end end
However, it has an additional feature that allows you to generate output in two ways, depending on how the input variable
tspan
is specified. As was the case for Fig. 22.3, you can set
tspan = [ti tf]
, where
ti
and
tf
are the initial and final times, respectively. If done in this way, the routine automatically generates output values between these limits
at equal spaced intervals
h
. Alternatively, if you want to obtain results at specific times, you can define
tspan = [t0,t1,...,tf]
. Note that in both cases, the
tspan
values must be in ascending order.
We can employ
rk4sys
to solve the same problem as in Example 22.5. First, we can develop an M-file to hold the ODEs:
function dy = dydtsyst, y dy = [y2;9.81-0.2568.1y22];
where
y1
= distance x and
y2
= velocity v. The solution can then be generated as
[t y] = rk4sysdydtsys,[0 10],[0 0],2; disp[t y:,1 y:,2]
2.0000 19.1656 18.7256 4.0000 71.9311 33.0995
6.0000 147.9521 42.0547 8.0000 237.5104 46.9345
10.0000 334.1626 49.4027
We can also use
tspan
to generate results at specific values of the independent vari- able. For example,
tspan=[0 6 10]; [t y] = rk4sysdydtsys,tspan,[0 0],2;
disp[t y:,1 y:,2]
6.0000 147.9521 42.0547
10.0000 334.1626 49.4027
22.6 CASE STUDY PREDATOR-PREY MODELS AND CHAOS
Background.
Engineers and scientists deal with a variety of problems involving sys- tems of nonlinear ordinary differential equations. This case study focuses on two of these
applications. The first relates to predator-prey models that are used to study species inter- actions. The second are equations derived from fluid dynamics that are used to simulate the
atmosphere.
Predator-prey models were developed independently in the early part of the twentieth
century by the Italian mathematician Vito Volterra and the American biologist Alfred Lotka. These equations are commonly called Lotka-Volterra equations. The simplest ver-
sion is the following pairs of ODEs:
d x dt
= ax − bxy
22.49
d y dt
= −cy + dxy
22.50
where x and y = the number of prey and predators, respectively, a = the prey growth rate,
c = the predator death rate, and b and d = the rates characterizing the effect of the predator-
prey interactions on the prey death and the predator growth, respectively. The multiplica- tive terms i.e., those involving x y are what make such equations nonlinear.
An example of a simple nonlinear model based on atmospheric fluid dynamics is the Lorenz equations
created by the American meteorologist Edward Lorenz: d x
dt = −σ x − σ y
d y dt
= r x − y − xz d z
dt = −bz + xy
Lorenz developed these equations to relate the intensity of atmospheric fluid motion x to temperature variations y and z in the horizontal and vertical directions, respectively. As
22.6 CASE STUDY 579
with the predator-prey model, the nonlinearities stem from the simple multiplicative terms: xz
and xy. Use numerical methods to obtain solutions for these equations. Plot the results to
visualize how the dependent variables change temporally. In addition, graph the dependent variables versus each other to see whether any interesting patterns emerge.
Solution.
The following parameter values can be used for the predator-prey simula- tion: a
= 1.2, b = 0.6, c = 0.8, and d = 0.3. Employ initial conditions of x = 2 and y = 1 and integrate from t
= 0 to 30, using a step size of h = 0.0625. First, we can develop a function to hold the differential equations:
function yp = predpreyt,y,a,b,c,d yp = [ay1-by1y2;-cy2+dy1y2];
The following script employs this function to generate solutions with both the Euler and the fourth-order RK methods. Note that the function
eulersys
was based on modify- ing the
rk4sys
function Fig. 22.8. We will leave the development of such an M-file as a homework problem. In addition to displaying the solution as a time-series plot x and y
versus t, the script also generates a plot of y versus x. Such phase-plane plots are often useful in elucidating features of the model’s underlying structure that may not be evident
from the time series.
h=0.0625;tspan=[0 40];y0=[2 1]; a=1.2;b=0.6;c=0.8;d=0.3;
[t y] = eulersyspredprey,tspan,y0,h,a,b,c,d; subplot2,2,1;plott,y:,1,t,y:,2,--
legendprey,predator;titlea Euler time plot subplot2,2,2;ploty:,1,y:,2
titleb Euler phase plane plot [t y] = rk4syspredprey,tspan,y0,h,a,b,c,d;
subplot2,2,3;plott,y:,1,t,y:,2,-- titlec RK4 time plot
subplot2,2,4;ploty:,1,y:,2 titled RK4 phase plane plot
The solution obtained with Euler’s method is shown at the top of Fig. 22.9. The time series Fig. 22.9a indicates that the amplitudes of the oscillations are expanding. This is
reinforced by the phase-plane plot Fig. 22.9b. Hence, these results indicate that the crude Euler method would require a much smaller time step to obtain accurate results.
In contrast, because of its much smaller truncation error, the RK4 method yields good re- sults with the same time step. As in Fig. 22.9c, a cyclical pattern emerges in time. Because the
predator population is initially small, the prey grows exponentially. At a certain point, the prey become so numerous that the predator population begins to grow. Eventually, the in-
creased predators cause the prey to decline. This decrease, in turn, leads to a decrease of the predators. Eventually, the process repeats. Notice that, as expected, the predator peak lags the
prey. Also, observe that the process has a fixed period—that is, it repeats in a set time.
22.6 CASE STUDY continued