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')

 採用された回答

Star Strider
Star Strider 2019 年 11 月 15 日

0 投票

Put them all in subplots, with the appropriate indexing:
tspan = linspace(0, 5, 25);
for k=1:5
y0 = 0;
[t,y(:,k)] = ode45(@(t,y) k*t, tspan, y0);
subplot(4,1,1)
title('Exponential Model')
grid on
end
plot(t,y,'-o')
% tspan = [0 5];
r=1;
for k=1:5
y0 = 0;
[t,y(:,k)] = ode45(@(t,y) r*(1-t/k)*t, tspan, y0);
% hold on
title('Logistic Model')
grid on
end
subplot(4,1,2)
plot(t,y,'-o')
% tspan = [0 5];
r=1;
k=1;
for H=1:5
y0 = 0;
[t,y(:,H)] = ode45(@(t,y) r*(1-t/k)*t-H, tspan, y0);
title('Constant Harvesting')
% hold on
grid on
end
subplot(4,1,3)
plot(t,y,'-o')
% tspan = [0 5];
r=1;
k=1;
H=1;
for A=1:5
y0 = 0;
[t,y(:,A)] = ode45(@(t,y) r*(1-t/k)*t-H*(t/(A+t)), tspan, y0);
title('Population dependent Harvesting')
hold on
grid on
end
subplot(4,1,4)
plot(t,y,'-o')
I also defined ‘tspan’ as a vector of 25 elements so it would be straightforward to save them to matrices, then plot them.

2 件のコメント

Giovanni Virgen
Giovanni Virgen 2019 年 11 月 15 日
I got this as an error:
Unable to perform assignment because the size of the left side is 41-by-1 and the size of the right side is 25-by-1.
Star Strider
Star Strider 2019 年 11 月 15 日
編集済み: Star Strider 2019 年 11 月 15 日
My code ran without error.
Be sure you eliminate all the ‘tspan’ assignments except for the first one that uses the linspace function.
I completely eliminated all of the others, corrected some of the syntax and call orders here.
Try this:
tspan = linspace(0, 5, 25);
for k=1:5
y0 = 0;
[t,y(:,k)] = ode45(@(t,y) k*t, tspan, y0);
subplot(4,1,1)
grid on
end
plot(t,y,'-o')
title('Exponential Model')
grid
r=1;
for k=1:5
y0 = 0;
[t,y(:,k)] = ode45(@(t,y) r*(1-t/k)*t, tspan, y0);
end
subplot(4,1,2)
plot(t,y,'-o')
title('Logistic Model')
grid on
r=1;
k=1;
for H=1:5
y0 = 0;
[t,y(:,H)] = ode45(@(t,y) r*(1-t/k)*t-H, tspan, y0);
end
subplot(4,1,3)
plot(t,y,'-o')
grid on
title('Constant Harvesting')
r=1;
k=1;
H=1;
for A=1:5
y0 = 0;
[t,y(:,A)] = ode45(@(t,y) r*(1-t/k)*t-H*(t/(A+t)), tspan, y0);
hold on
end
subplot(4,1,4)
plot(t,y,'-o')
title('Population dependent Harvesting')
grid on
They should all plot correctly, and you should not have any problems with them.
EDIT — (15 Nov 2019 at 4:29)
Added plot figure:
How do I plot these for loops - 2019 11 14.png

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 11 月 15 日

1 投票

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.

カテゴリ

ヘルプ センター および File ExchangeGraphics についてさらに検索

タグ

質問済み:

2019 年 11 月 15 日

編集済み:

2019 年 11 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by