For loop plotting all values in one graph, how do I create separate plots for my for loop values

5 views (last 30 days)
% Mesh Grid in (Diamtoms(D),Zooplankton(P))-plane
[D, P] = meshgrid(-0.8:1:10, -0.8:1:10);
% Parameters
hold on
for K=1:10
s=1;
e=1;
r=1;
H=1;
d=0.75;
% System as a 2-D function
f = @(t,X) [X(1)*(r*(1-X(1)*(1/K))-(s*X(1)/1+s*H*X(1))*X(2));
X(2)*(e*(s*X(1))/(1+s*H*X(1))-d)];
%Direction Filed
% Ddot is dD/dt and Pdot is dP/dt derivatives
Ddot = D-(1./K).*D.*D-(s*D)./((1+s.*H.*D)).*P;
Pdot = P.*((s.*D)./(1+s.*H.*D))-P.*d;
% Vector Field
figure(1)
quiver(D,P,Ddot,Pdot,'LineWidth',1.5)
timespan=[0 100];
% Phase Trajectories
X0=0.5*K*rand;Y0=K*rand;
[ts, Xs] = ode45(f,timespan, [X0, Y0]);
% plot of several trajectories
plot(Xs(:,1), Xs(:,2), 'Linewidth', 2)
% Nullclines.
x = linspace(0, 10, 50);
y = linspace(0, 10, 50);
[X, Y] = meshgrid(x, y);
nullcline_x = x .* (1 - x / K - Y ./ (1 + x));
nullcline_y = (4 .* X) ./ (1 + X);
contour(X, Y, nullcline_x, [0, 0], 'r', 'LineWidth', 1);
contour(X, Y, nullcline_y, [0, 0], 'b', 'LineWidth', 1);
xlabel('Prey, D', 'FontSize',14)
ylabel('Predator, P', 'FontSize',14)
set(gca, 'FontSize', 16)
xlim([-0.8 10])
ylim([-0.8 10])
end

Accepted Answer

Les Beckham
Les Beckham on 3 Nov 2023
Edited: Les Beckham on 3 Nov 2023
In your loop you are only ever plotting into Figure(1). See updates below.
% Mesh Grid in (Diamtoms(D),Zooplankton(P))-plane
[D, P] = meshgrid(-0.8:1:10, -0.8:1:10);
% Parameters
for K=1:10
s=1;
e=1;
r=1;
H=1;
d=0.75;
% System as a 2-D function
f = @(t,X) [X(1)*(r*(1-X(1)*(1/K))-(s*X(1)/1+s*H*X(1))*X(2));
X(2)*(e*(s*X(1))/(1+s*H*X(1))-d)];
%Direction Filed
% Ddot is dD/dt and Pdot is dP/dt derivatives
Ddot = D-(1./K).*D.*D-(s*D)./((1+s.*H.*D)).*P;
Pdot = P.*((s.*D)./(1+s.*H.*D))-P.*d;
% Vector Field
figure(K) % << changed from figure(1) which was telling Matlab to always use the same figure
quiver(D,P,Ddot,Pdot,'LineWidth',1.5)
hold on % << moved inside the loop
title(sprintf('K = %d', K)) % << added
timespan=[0 100];
% Phase Trajectories
X0=0.5*K*rand;Y0=K*rand;
[ts, Xs] = ode45(f,timespan, [X0, Y0]);
% plot of several trajectories
plot(Xs(:,1), Xs(:,2), 'Linewidth', 2)
% Nullclines.
x = linspace(0, 10, 50);
y = linspace(0, 10, 50);
[X, Y] = meshgrid(x, y);
nullcline_x = x .* (1 - x / K - Y ./ (1 + x));
nullcline_y = (4 .* X) ./ (1 + X);
contour(X, Y, nullcline_x, [0, 0], 'r', 'LineWidth', 1);
contour(X, Y, nullcline_y, [0, 0], 'b', 'LineWidth', 1);
xlabel('Prey, D', 'FontSize',14)
ylabel('Predator, P', 'FontSize',14)
set(gca, 'FontSize', 16)
xlim([-0.8 10])
ylim([-0.8 10])
end

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!