Why are my plots for K values and initial conditions all the same colour even with hold on

1 Ansicht (letzte 30 Tage)
% 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,'k','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),'k', 'Linewidth', 2)
xlabel('Prey, D', 'FontSize',14)
ylabel('Predator, P', 'FontSize',14)
set(gca, 'FontSize', 16)
xlim([-0.8 10])
ylim([-0.8 10])
end
  1 Kommentar
Beñat Arribas
Beñat Arribas am 31 Okt. 2023
The 'k' defines the color of the plots, which is equivalent to black color. Removing this will turn between the different colors set by default.
quiver(D,P,Ddot,Pdot,'k','LineWidth',1.5)
plot(Xs(:,1), Xs(:,2),'k', 'Linewidth', 2)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Voss
Voss am 31 Okt. 2023
They are all the same color because you have specified them all to be black when you plotted them. The 'k' means black in, e.g.:
quiver(D,P,Ddot,Pdot,'k','LineWidth',1.5)
% ...
plot(Xs(:,1), Xs(:,2),'k', 'Linewidth', 2)
Removing the 'k' gives lines with different colors:
% 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)
xlabel('Prey, D', 'FontSize',14)
ylabel('Predator, P', 'FontSize',14)
set(gca, 'FontSize', 16)
xlim([-0.8 10])
ylim([-0.8 10])
end
  1 Kommentar
Voss
Voss am 31 Okt. 2023
Also, you can move those things that don't depend on the value of the variable K out of the for loop:
% Mesh Grid in (Diamtoms(D),Zooplankton(P))-plane
[D, P] = meshgrid(-0.8:1:10, -0.8:1:10);
% Parameters
s=1;
e=1;
r=1;
H=1;
d=0.75;
timespan=[0 100];
Pdot = P.*((s.*D)./(1+s.*H.*D))-P.*d;
figure(1)
hold on
for K=1:10;
% 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;
% Vector Field
quiver(D,P,Ddot,Pdot, 'LineWidth',1.5)
% 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)
end
xlabel('Prey, D', 'FontSize',14)
ylabel('Predator, P', 'FontSize',14)
set(gca, 'FontSize', 16)
xlim([-0.8 10])
ylim([-0.8 10])

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Les Beckham
Les Beckham am 31 Okt. 2023
Because you told Matlab to make all of the lines black.
For example, the 'k' in this command means "make the lines black":
plot(Xs(:,1), Xs(:,2),'k', 'Linewidth', 2)
% Mesh Grid in (Diamtoms(D),Zooplankton(P))-plane
[D, P] = meshgrid(-0.8:1:10, -0.8:1:10);
% Parameters
% hold on
s=1;
e=1;
r=1;
H=1;
d=0.75;
for K=1:10;
% 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) % <<< removed color and linewidth specification
hold on % <<< hold on AFTER first plot command
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)) % <<< removed color and linewidth specification
xlabel('Prey, D', 'FontSize',14)
ylabel('Predator, P', 'FontSize',14)
set(gca, 'FontSize', 16)
xlim([-0.8 10])
ylim([-0.8 10])
end

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by