Você está na página 1de 7

SOLVING DIFFERENTIAL EQUATIONS WITH MATLAB

– A Case Study
Lotka-Volterra Equations- [BASED ON WIKIPEDIA]
The Lotka-Volterra equations, also known as the predator-prey equations, are a pair
of first order, non-linear, differential equations frequently used to describe the
dynamics of biological systems in which two species interact, one a predator and one
its prey. They were proposed independently by Alfred J. Lotka in 1925 and Vito
Volterra in 1926.

where

• y is the number of some predator (for example, wolves);


• x is the number of its prey (for example, rabbits);
• dy/dt and dx/dt represents the growth of the two populations against time;
• t represents the time; and
• α, β, γ and δ are parameters representing the interaction of the two species.

Prey

The prey equation is:

The prey are assumed to have an unlimited food supply, and to reproduce
exponentially unless subject to predation; this exponential growth is represented in
equation above by the term αx. The rate of predation upon the prey is assumed to be
proportional to the rate at which the predators and the prey meet; this is represented
above by βxy. If either x or y is zero then there can be no predation.

With these two terms the equation above can be interpreted as: the change in the
prey's numbers is given by its own growth minus the rate at which it is preyed upon.

Predators

The predator equation is:


In this equation, δxy represents the growth of the predator population. (Note the
similarity to the predation rate; however, a different constant is used as the rate at
which the predator population grows is not necessarily equal to the rate at which it
consumes the prey). γy represents the natural death of the predators; it is an
exponential decay.

Hence the equation represents the change in the predator population as the growth of
the predator population, minus natural death.

Solutions to the equations


The equations have periodic solutions which do not have a simple expression in terms
of the usual trigonometric functions. However, an approximate linearised solution
yields a simple harmonic motion with the population of predators following that of
prey by 90°.

An example problem

Suppose there are two species of animals, a baboon (prey) and a cheetah (predator). If
the initial conditions are 80 baboons and 40 cheetahs, one can plot the progression of
the two species over time. Time is dimensionless.
One can also plot a solution which corresponds to the oscillatory nature of the
population of the two species. At any given time, the solution is somewhere on the
inside of these elliptical solutions.
In the model system, the predators thrive when there are plentiful prey but, ultimately,
outstrip their food supply and decline. As the predator population is low the prey
population will increase again. These dynamics continue in a cycle of growth and
decline.

Numerical Integration of Differential Equations


[BASED ON MATLAB DEMO: lotkademo.m ]
ODE23 and ODE45 are functions for the numerical solution of ordinary differential
equations. They employ variable step size Runge-Kutta integration methods. ODE23
uses a simple 2nd and 3rd order pair of formulas for medium accuracy and ODE45
uses a 4th and 5th order pair for higher accuracy. This demo shows their use on a
simple differential equation.

Consider the pair of first order ordinary differential equations known as the Lotka-
Volterra predator-prey model.

y1' = (1 - alpha*y2)*y1

y2' = (-1 + beta*y1)*y2

The functions y1 and y2 measure the sizes of the prey and predator populations
respectively. The quadratic cross term accounts for the interactions between the
species. Note that the prey population increases when there are no predators, but the
predator population decreases when there are no prey.
To simulate a system, create a function M-file that returns a column vector of state
derivatives, given state and time values. For this example, Matlab has already a demo
file called LOTKA.M. [note that this also defines the alpha and beta parameters]
type lotka

function yp = lotka(t,y)
yp = diag([1 - .01*y(2), -1 + .02*y(1)])*y;

Now simulate LOTKA using ODE45 (could also be using ODE23 for less precision
but more speed). ODE45 takes longer at each step, but also takes larger steps.
Nevertheless, the output of ODE45 is smooth because by default the solver uses a
continuous extension formula to produce output at 4 equally spaced time points in the
span of each step taken. The plot compares this result against the previous.
t0 = 0; % this is the start time of the experiment
tfinal = 85; % this is the end time of the experiment
y0 = [10 10]'; % these are the initial numbers of predator and prey

[t,y] = ode45('lotka',[t0 tfinal],y0);

figure;
% Plot the result of the simulation two different ways.
subplot(1,2,1)
plot(t,y)
title('Time history')
subplot(1,2,2)
plot(y(:,1),y(:,2))
title('Phase plane plot')

[Above discussion based on Matlab demo and on Wikipedia]


FROM MATLAB: ABOUT ODE45

ODE45 Solve non-stiff differential equations, medium order method.

[TOUT,YOUT] = ODE45(ODEFUN,TSPAN,Y0)
with TSPAN = [T0 TFINAL] integrates
the system of differential equations y' = f(t,y) from time T0 to TFINAL
with initial conditions Y0.

ODEFUN is a function handle.

For a scalar T
and a vector Y, ODEFUN(T,Y) must return a column vector corresponding
to f(t,y). Each row in the solution array YOUT corresponds to a time
returned in the column vector TOUT. To obtain solutions at specific
times T0,T1,...,TFINAL (all increasing or all decreasing), use TSPAN =
[T0 T1 ... TFINAL].

[TOUT,YOUT] = ODE45(ODEFUN,TSPAN,Y0,OPTIONS) solves as above with


default
integration properties replaced by values in OPTIONS, an argument created
with the ODESET function. See ODESET for details. Commonly used options
are scalar relative error tolerance 'RelTol' (1e-3 by default) and vector
of absolute error tolerances 'AbsTol' (all components 1e-6 by default).
If certain components of the solution must be non-negative, use
ODESET to set the 'NonNegative' property to the indices of these
components.

ODE45 can solve problems M(t,y)*y' = f(t,y) with mass matrix M that is
nonsingular. Use ODESET to set the 'Mass' property to a function handle
MASS if MASS(T,Y) returns the value of the mass matrix. If the mass matrix
is constant, the matrix can be used as the value of the 'Mass' option. If
the mass matrix does not depend on the state variable Y and the function
MASS is to be called with one input argument T, set 'MStateDependence' to
'none'. ODE15S and ODE23T can solve problems with singular mass matrices.

[TOUT,YOUT,TE,YE,IE] = ODE45(ODEFUN,TSPAN,Y0,OPTIONS) with the


'Events'
property in OPTIONS set to a function handle EVENTS, solves as above
while also finding where functions of (T,Y), called event functions,
are zero. For each function you specify whether the integration is
to terminate at a zero and whether the direction of the zero crossing
matters. These are the three column vectors returned by EVENTS:
[VALUE,ISTERMINAL,DIRECTION] = EVENTS(T,Y). For the I-th event
function:
VALUE(I) is the value of the function, ISTERMINAL(I)=1 if the integration
is to terminate at a zero of this event function and 0 otherwise.
DIRECTION(I)=0 if all zeros are to be computed (the default), +1 if only
zeros where the event function is increasing, and -1 if only zeros where
the event function is decreasing. Output TE is a column vector of times
at which events occur. Rows of YE are the corresponding solutions, and
indices in vector IE specify which event occurred.

SOL = ODE45(ODEFUN,[T0 TFINAL],Y0...) returns a structure that can be


used with DEVAL to evaluate the solution or its first derivative at
any point between T0 and TFINAL. The steps chosen by ODE45 are returned
in a row vector SOL.x. For each I, the column SOL.y(:,I) contains
the solution at SOL.x(I). If events were detected, SOL.xe is a row vector
of points at which events occurred. Columns of SOL.ye are the corresponding
solutions, and indices in vector SOL.ie specify which event occurred.

Example
[t,y]=ode45(@vdp1,[0 20],[2 0]);
plot(t,y(:,1));
solves the system y' = vdp1(t,y), using the default relative error
tolerance 1e-3 and the default absolute tolerance of 1e-6 for each
component, and plots the first component of the solution.

Você também pode gostar