MATLAB Answers

0

numerical solution of predator-prey model

Asked by ahed salman on 8 Oct 2019
Latest activity Commented on by Stephan
on 8 Oct 2019
Hello all, I have a 3d predator prey system and i want to solve it numerically in order to analyze it, I am new to Matlab, used it couples of time before and i beg you for help as i am writing my thesis now, below is the code i used but it seems not to work and i have errors i do not understant in order to fix! Please Help!!!
function signal
%initial conditions
x0=1; y0=1; z0=1;
IC=[x0,y0,z0];
t = 0:.1:60;
r=15;
b=0.05433;
beta=0.9;
rho= .7;
delta= .2;
m= .2;
[t,s]= ode45(@RHS, t,IC );
plot(t ,s(:,2),'linewidth',2,'color','b')
grid on;
xlabel('t', 'FontSize', 20);
ylabel('\omega', 'FontSize', 20);
function dsdt= RHS(t,s)
dsdt_1=r*(s(1)+s(2))-b*s(1)*(s(1)+s(2))-beta*s(1)*s(3)/(1+beta*s(1))-s(1)*(1+s(3)*s(2))/(1+s(3));
dsdt_2=s(1)*(1+s(3)*s(2))/(1+s(3))-b*s(2)*(s(1)+s(2))-rho*beta*s(2)*s(3)/(1+beta*s(2));
dsdt_3=delta*s(1)*s(3)/(1+beta*s(1))+rho*delta*s(2)*s(3)/(1+beta*s(2))-m*s(3);
dsdt=[dsdt_1;dsdt_2,dsdt_3];
end
end

  0 Comments

Sign in to comment.

1 Answer

Answer by Stephan
on 8 Oct 2019
Edited by Stephan
on 8 Oct 2019
 Accepted Answer

Problem was here:
dsdt=[dsdt_1;dsdt_2,dsdt_3];
it should be:
dsdt=[dsdt_1;dsdt_2;dsdt_3];
Then it works.
function signal
%initial conditions
x0=1; y0=1; z0=1;
IC=[x0,y0,z0];
t = 0:.1:60;
r=15;
b=0.05433;
beta=0.9;
rho= .7;
delta= .2;
m= .2;
[t,s]= ode45(@RHS, t,IC );
plot(t ,s(:,2),'linewidth',2,'color','b')
grid on;
xlabel('t', 'FontSize', 20);
ylabel('\omega', 'FontSize', 20);
function dsdt= RHS(t,s)
dsdt_1=r*(s(1)+s(2))-b*s(1)*(s(1)+s(2))-beta*s(1)*s(3)/(1+beta*s(1))-s(1)*(1+s(3)*s(2))/(1+s(3));
dsdt_2=s(1)*(1+s(3)*s(2))/(1+s(3))-b*s(2)*(s(1)+s(2))-rho*beta*s(2)*s(3)/(1+beta*s(2));
dsdt_3=delta*s(1)*s(3)/(1+beta*s(1))+rho*delta*s(2)*s(3)/(1+beta*s(2))-m*s(3);
dsdt=[dsdt_1;dsdt_2;dsdt_3];
end
end

  6 Comments

So you need to plot 3D data over time?
yes, i want to see how x,y,z behave over time
One way would be the usage of subplot, which allows to arrange several plots in one figure. s should be an array of 3 columns (x,y,z) and as many lines as time steps. If you grab the components one by one this could be a propper way to analyze your results.

Sign in to comment.