How do I plot these for loops?
Mostrar comentarios más antiguos
I have been trying to get these loops to plot on the same figure but I can't seem to make it work. Each for loop (4 in total) produces five lines which I'd like to be on the same figure. But either I end up with 20 figures or with 4 figures with missing plot lines. I was wondering if anyone could help me with this. Basically I want to get 4 figures with its corresponding five plot lines in each figure.
tspan = [0 5];
for k=1:5
y0 = 0;
[t,y] = ode45(@(t,y) k*t, tspan, y0);
hold on
title('Exponential Model')
grid on
end
plot(t,y,'-o')
tspan = [0 5];
r=1;
for k=1:5
y0 = 0;
[t,y] = ode45(@(t,y) r*(1-t/k)*t, tspan, y0);
hold on
title('Logistic Model')
grid on
end
plot(t,y,'-o')
tspan = [0 5];
r=1;
k=1;
for H=1:5
y0 = 0;
[t,y] = ode45(@(t,y) r*(1-t/k)*t-H, tspan, y0);
title('Constant Harvesting')
hold on
grid on
end
plot(t,y,'-o')
tspan = [0 5];
r=1;
k=1;
H=1;
for A=1:5
y0 = 0;
[t,y] = ode45(@(t,y) r*(1-t/k)*t-H*(t/(A+t)), tspan, y0);
title('Population dependent Harvesting')
hold on
grid on
end
plot(t,y,'-o')
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 15 de Nov. de 2019
for k=1:5
y0 = 0;
[t,y] = ode45(@(t,y) k*t, tspan, y0);
hold on
title('Exponential Model')
grid on
end
You are not plotting inside the for loop, so each iteration is overwriting all of the y variable, so you only plot 1 line total after the end.
tspan = linspace(0,5);
y0 = 0;
for k=1:5
[t,y(:,k)] = ode45(@(t,y) k*t, tspan, y0);
end
hold on
grid on
h = plot(t,y,'-^');
L(1) = h(1);
L(1).DisplayName = 'Exponential Model';
r=1;
y0 = 0;
for k=1:5
[t,y(:,k)] = ode45(@(t,y) r*(1-t/k)*t, tspan, y0);
end
hold on
grid on
h = plot(t,y,'-v');
L(2) = h(1);
L(2).DisplayName = 'Logistic Model';
r=1;
k=1;
y0 = 0;
for H=1:5
[t,y(:,H)] = ode45(@(t,y) r*(1-t/k)*t-H, tspan, y0);
end
hold on
grid on
h = plot(t,y,'-<');
L(3) = h(1);
L(3).DisplayName = 'Constant Harvesting';
r=1;
k=1;
H=1;
y0 = 0;
for A=1:5
[t,y(:,A)] = ode45(@(t,y) r*(1-t/k)*t-H*(t/(A+t)), tspan, y0);
end
hold on
grid on
h = plot(t,y,'->');
L(4) = h(1);
L(4).DisplayName = 'Population dependent Harvesting';
legend(L);
However you are going to have a rough time distinguishing the 20 different lines on the same graph.
Categorías
Más información sobre Graphics en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
