Data must be a single matrix Y or a list of pairs X,Y. Where might be the problem?

193 views (last 30 days)
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?
Stephen 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.

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
Star Strider on 13 Jan 2017
My pleasure!