Data must be a single matrix Y or a list of pairs X,Y. Where might be the problem?
147 views (last 30 days)
Show older comments
Kristyna Pospisilova
on 13 Jan 2017
Commented: Star Strider
on 13 Jan 2017
function Predator_prey2
alpha = 0.4;
beta = 0.8;
gamma = 0.7;
delta = 1;
epsilon = 0.5;
eta = 0.9;
lambda = 0.6;
xold = 0.8;
yold = 0.8;
zold = 0.8;
tMin = 0;
tMax = 50;
tChange = 0.001;
figure;
subplot(1,2,1);
hold on;
grid on;
axis( [-0.2 2 0.1 2 0.1 2]);
title ('Populations of foxes rabbits and chickens');
xlabel ('Population of chickens');
ylabel ('Population of rabbits');
zlabel ('Population of foxes');
xs = []; ys = []; zs = []; ts = [];
subplot(1,2,2);
hold on;
axis([tMin tMax 0.5 2.2]);
title('Population');
xlabel('Time');
ylabel('Population size');
for t = tMin:tChange:tMax;
xs = [xs, xold]; ys = [ys, yold]; zs = [zs, zold]; ts = [ts, t];
yNew = yold+tChange*(delta-epsilon*zold);
xNew = xold+tChange*(eta-lambda*zold);
zNew = zold+tChange*(beta*yold-alpha*zold+gamma*xold);
xold = xNew;
yold = yNew;
zold = zNew;
if mod(t,1)==0
subplot(1,2,1);
plot(xs, ys, zs,'e.');
subplot(1,2,2);
plot(ts,xs,'f.',ts,ys,'g.',ts,zs,'h.');
legend('Chickens','Rabbits','Foxes');
pause(0.01);
end
end
I have a predator-prey problem, where I have 1 predator and 2 preys. Could you please help me, where might be problem?
2 Comments
Adam
on 13 Jan 2017
Not if you don't give a complete error message and format your full code to make it readable!
The problem is independent of the fact you are working with predators and prey though I imagine, but until we know which line produces the error it isn't worth guessing.
Stephen23
on 13 Jan 2017
@Kristyna Pospisilova: please edit your question and show us the entire error message. This means all of the red text.
Also note that I formatted your code correctly for you. In future you can do it yourself by selecting all of the code text and then clicking the {} Code button above the textbox.
Accepted Answer
Star Strider
on 13 Jan 2017
You aren’t using the correct color arguments in your plots. See the documentation for the plot function for a list of the correct ones, and how to create your own colors.
This works:
alpha = 0.4;
beta = 0.8;
gamma = 0.7;
delta = 1;
epsilon = 0.5;
eta = 0.9;
lambda = 0.6;
xold = 0.8;
yold = 0.8;
zold = 0.8;
tMin = 0;
tMax = 50;
tChange = 0.001;
figure;
subplot(1,2,1);
hold on;
grid on;
axis( [-0.2 2 0.1 2 0.1 2]);
title ('Populations of foxes rabbits and chickens');
xlabel ('Population of chickens');
ylabel ('Population of rabbits');
zlabel ('Population of foxes');
xs = []; ys = []; zs = []; ts = [];
subplot(1,2,2);
hold on;
axis([tMin tMax 0.5 2.2]);
title('Population');
xlabel('Time');
ylabel('Population size');
for t = tMin:tChange:tMax;
xs = [xs, xold]; ys = [ys, yold]; zs = [zs, zold]; ts = [ts, t];
yNew = yold+tChange*(delta-epsilon*zold);
xNew = xold+tChange*(eta-lambda*zold);
zNew = zold+tChange*(beta*yold-alpha*zold+gamma*xold);
xold = xNew;
yold = yNew;
zold = zNew;
if mod(t,1)==0
subplot(1,2,1);
plot3(xs, ys, zs,'r.');
grid on
subplot(1,2,2);
plot(ts,xs,'b.',ts,ys,'g.',ts,zs,'c.');
legend('Chickens','Rabbits','Foxes');
pause(0.01);
end
end
2 Comments
More Answers (0)
See Also
Categories
Find more on Line Plots 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!