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

4 views (last 30 days)
Hagen Gersie on 1 Jun 2022
Commented: Hagen Gersie on 2 Jun 2022
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!

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.
Hagen Gersie on 2 Jun 2022
Thanks, I figured that out as well and adapted accordingly. It now runs smoothly.

Sam Chak on 1 Jun 2022
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)
William Rose on 2 Jun 2022

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by