Question 23.1: Employ ode45 to solve the following set of nonlinear ODEs fr...
Employ ode45 to solve the following set of nonlinear ODEs from t = 0 to 20:
\frac{d y_1}{d t}=1.2 y_1-0.6 y_1 y_2 \quad \quad \quad \frac{d y_2}{d t}=-0.8 y_2+0.3 y_1 y_2
where y_1 = 2 \text{ and } y_2 = 1 at t = 0. Such equations are referred to as predator-prey equations.
Before obtaining a solution with MATLAB, you must create a function to compute the right-hand side of the ODEs. One way to do this is to create an M-file as in
function yp = predprey(t,y)
yp = [1.2*y(1) −0.6*y(1)*y(2);−0.8*y(2)+0.3*y(1)*y(2)];
We stored this M-file under the name: predprey.m.
Next, enter the following commands to specify the integration range and the initial conditions:
>> tspan = [0 20];
>> y0 = [2, 1];
The solver can then be invoked by
>> [t,y] = ode45(@predprey, tspan, y0);
This command will then solve the differential equations in predprey.m over the range defined by tspan using the initial conditions found in y0. The results can be displayed by simply typing
>> plot(t,y)
which yields Fig. 23.2.
In addition to a time series plot, it is also instructive to generate a phase-plane plot — that is, a plot of the dependent variables versus each other by
>> plot(y(:,1),y(:,2))
which yields Fig. 23.3.

