Main Content

Solve Predator-Prey Equations

This example shows how to solve a differential equation representing a predator/prey model using bothode23andode45。These functions are for the numerical solution of ordinary differential equations using variable step size Runge-Kutta integration methods.ode23uses a simple 2nd and 3rd order pair of formulas for medium accuracy andode45uses a 4th and 5th order pair for higher accuracy.

Consider the pair of first-order ordinary differential equations known as theLotka-Volterra equations, orpredator-prey model:

dx dt = x - α xy dy dt = - y + β xy

The variables x and y measure the sizes of the prey and predator populations, respectively. The quadratic cross term accounts for the interactions between the species. The prey population increases when no predators are present, and the predator population decreases when prey are scarce.

Code Equations

To simulate the system, create a function that returns a column vector of state derivatives, given state and time values. The two variables x and y can be represented in MATLAB as the first two values in a vectory。Similarly, the derivatives are the first two values in a vectoryp。The function must accept values fortandyand return the values produced by the equations inyp

yp(1) = (1 - alpha*y(2))*y(1)

yp(2) = (-1 + beta*y(1))*y(2)

In this example, the equations are contained in a file calledlotka.m。This file uses parameter values of α = 0 01 and β = 0 02

typelotka
function yp = lotka(t,y) %LOTKA Lotka-Volterra predator-prey model. % Copyright 1984-2014 The MathWorks, Inc. yp = diag([1 - .01*y(2), -1 + .02*y(1)])*y;

Simulate System

Useode23to solve the differential equation defined inlotkaover the interval 0 < t < 15 。使用一个初始条件 x ( 0 ) = y ( 0 ) = 20 so that the populations of predators and prey are equal.

t0 = 0; tfinal = 15; y0 = [20; 20]; [t,y] = ode23(@lotka,[t0 tfinal],y0);

Plot Results

Plot the resulting populations against time.

plot(t,y) title('Predator/Prey Populations Over Time') xlabel('t') ylabel('Population') legend('Prey','Predators','Location','North')

Figure contains an axes object. The axes object with title Predator/Prey Populations Over Time contains 2 objects of type line. These objects represent Prey, Predators.

Now plot the populations against each other. The resulting phase plane plot makes the cyclic relationship between the populations very clear.

plot(y(:,1),y(:,2)) title('Phase Plane Plot') xlabel('Prey Population') ylabel('Predator Population')

Figure contains an axes object. The axes object with title Phase Plane Plot contains an object of type line.

Compare Results of Different Solvers

Solve the system a second time usingode45, instead ofode23。Theode45solver takes longer for each step, but it also takes larger steps. Nevertheless, the output ofode45is smooth because by default the solver uses a continuous extension formula to produce output at four equally spaced time points in the span of each step taken. (You can adjust the number of points with the'Refine'option.) Plot both solutions for comparison.

[T,Y] = ode45(@lotka,[t0 tfinal],y0); plot(y(:,1),y(:,2),'-',Y(:,1),Y(:,2),'-'); title('Phase Plane Plot') legend('ode23','ode45')

Figure contains an axes object. The axes object with title Phase Plane Plot contains 2 objects of type line. These objects represent ode23, ode45.

The results show that solving differential equations using different numerical methods can produce slightly different answers.

See Also

|

Related Topics