How to add restrictions to our group of differential equation? For example, in predator-prey model I want the sum of predator and prey to be fixed.

2 views (last 30 days)
%Here is a code of sample predator-prey model.
%y0=20,20 means 20 predators and 20 preys.
%How can I let the sum of predators and preys to be 40 all the time, and see their population change with time?
t0 = 0;
tfinal = 15;
y0 = [20; 20];
[t,y] = ode23(@lotka,[t0 tfinal],y0);
t0 = 0;
tf = 15;
nsteps = 50;
y0 = [20; 20];
[t,y] = ode23(@lotka,linspace(t0,tf,nsteps),y0);
title('Predator/Prey Populations Over Time')
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;

Answers (1)

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath on 25 Aug 2023
Here we go,
t0 = 0;
tfinal = 15;
y0 = [20; 20]; % Initial conditions [prey, predator]
[t, y] = ode23(@lotka, [t0 tfinal], y0);
plot(t, y)
title('Predator/Prey Populations Over Time with Fixed Sum')
legend('Prey', 'Predator', 'Location', 'North')
function yp = lotka(t, y)
a = 1; % Growth rate of prey when there are no predators
b = 0.01; % Rate at which predators consume prey
c = 1; % Decay rate of predators when there are no prey
d = 0.02; % Rate at which predators increase by consuming prey
dpdt = a * y(1) - b * y(1) * y(2);
dqdt = -dpdt; % Making sure that the sum remains constant
yp = [dpdt; dqdt];
This will give you a plot where the sum of predators and prey remains constant at 40 (or whatever you set y0 to sum to). The populations will still oscillate, but in a manner constrained by this sum.


Find more on Thermodynamics & Statistical Physics in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!