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

조회 수: 1 (최근 30일)
% 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
Beñat Arribas
Beñat Arribas 2023년 10월 31일
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)

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2023년 10월 31일
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
Voss
Voss 2023년 10월 31일
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])

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Les Beckham
Les Beckham 2023년 10월 31일
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

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by