How can I plot a three-species Lotka-Volterra model?

21 views (last 30 days)
Hi everyone!
I am a student of cultural studies and thus really out of my depth with MatLab. I have a seminar on Modeling Systems using linear and non-linear differential equations. I am now doing my exam project but I am quite confused by MatLab (this program is completely new to me).
I am trying to model a Lotka-Volterra system with three "species". I have already found my interaction parameters (using the least squares method). The equations now look like this:
dx(t)/dt = x(t)*(a0+a1*x(t)+a2*y(t)+a3*z(t))
dy(t)/dt = y(t)*(b0+b1*x(t)+b2*y(t)+b3*z(t))
dz(t)/dt = z(t)*(c0+c1*x(t)+c2*y(t)+c3*z(t))
a0, a1, a2, a3, b0, b1, b2, b3, c0, c1, c2, c3 are the parameters and constants. I have the values for them. I have tried to figure out how to write this function as a MatLab function but I have failed so far (and thus I cannot plot this system).
I tried naming x, y and z as the first three values of a vector (v), so that I have a function that looks a little like this:
function LV=lotka (t,v)
LV(1)=v(1)*(.......);
LV(2)=v(2)*(....);
LV(3)=v(3)*(.....);
end
I also used "ode45" (where I specified the time interval t) and the "figure" command to try to plot this but something along the way failed. Any help with writing this function would be appreciated.
I hope I made the problem clear. If needed I can provide the full code I have tried so far. However, as it is obviously wrong, I don't think it will help here.
Thank you!

Accepted Answer

William Rose
William Rose on 1 Jun 2022
@Hagen Gersie, The answer from @Sam Chak is excellent . It has the virtues of conciseness and clarity.
You may be wondering why your way did not work. It is clear that you made a solid attempt. Your function lotka.m is actually a very good start. You got farther than a lot of people would. Please share your a,b,c,d constants, and the rest of your code, if you want to see how close you did or did not come to succeeding.
  6 Comments
William Rose
William Rose on 2 Jun 2022
Your script stopped at 3.9 seconds because the equations were blowing up, and the integration routine, ode45() could not complete another time step. This is a result of the particular values for the constants a0..a3, b0..b3, c0..c3, and the initial condition.
Hagen Gersie
Hagen Gersie on 2 Jun 2022
Thanks, I figured that out as well and adapted accordingly. It now runs smoothly.

Sign in to comment.

More Answers (1)

Sam Chak
Sam Chak on 1 Jun 2022
Read doc ode45.
You can also follow the example and do something like this:
sigma = 10;
beta = 8/3;
rho = 28;
% Ordinary differential equations of the Lorenz system
f = @(t, x) [-sigma*x(1) + sigma*x(2); ...
rho*x(1) - x(2) - x(1)*x(3); ...
-beta*x(3) + x(1)*x(2)];
tspan = [0 100];
init = [1 1 1]; % initial condition
% Runge-Kutta Dormand-Prince 4/5 solver
[t, x] = ode45(f, tspan, init);
plot3(x(:,1), x(:,2), x(:,3)) % plot lines in 3D space
view(30, 30) % adjust view angles (in deg)
  3 Comments
Sam Chak
Sam Chak on 2 Jun 2022
You are welcome, @Hagen Gersie. Please vote 👍 if you like the support, but consider accepting@William Rose's Answer for his kind effort in correcting and writing the MATLAB code for you. Have a nice day.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!