Plot Population growth function with different values of b using for loop

I need to plot population growth model with different values of b
where b>d
and b=d
using for loops. Both results should be visible on the same graph with different colors.
Here is my function and intial value, but I am not getting how to get the plot .
H=@(t,N)[b*N-d*N];
[T,Y]=ode45(H,[0:1:time],[N0]);
Intitial value
b= 0.1 0.001
d=0.001
N0=400
I need two line for each b on the same plot.

回答 (1 件)

Torsten
Torsten 2023 年 2 月 2 日
f = @(t,b,d,N0) N0*exp((b-d)*t);
t = 0:0.1:10;
N0 = 400;
b = [0.1 0.001].';
d = 0.001;
plot(t,f(t,b,d,N0))
grid on

10 件のコメント

Saira
Saira 2023 年 2 月 2 日
編集済み: Saira 2023 年 2 月 2 日
As per requirement, I have to use forloop and ODE function. and T should be from 0 to 400. Sorry forget to mention
Torsten
Torsten 2023 年 2 月 2 日
編集済み: Torsten 2023 年 2 月 2 日
b = [0.1 0.001];
d = 0.001;
N0 = 400;
time = 400;
hold on
for i = 1:numel(b)
H=@(t,N)[b(i)*N-d*N];
[T,Y] = ode45(H,[0:1:time],[N0]);
plot(T,Y)
end
hold off
grid on
Saira
Saira 2023 年 2 月 2 日
Thanks, I was getting the same results.
But when I try an individual plots by putting b=.01, and b=.001. I get the results as attached. and you can see the plots dimentions are totally different. They should be same, either by putting in the seperate function or function inside a loop.
Saira
Saira 2023 年 2 月 2 日
Why you are too getting totally different plots for the same function and same inputs?
Torsten
Torsten 2023 年 2 月 2 日
編集済み: Torsten 2023 年 2 月 2 日
Show us the two codes for which you mean you get different results for the same input.
Include them as text here, not as a PNG file.
Saira
Saira 2023 年 2 月 2 日
function[T,Y]=model_growth(par,time)
b=par(1);
d=par(2);
N0=par(3);
H=@(t,N)[b*N-d*N];
[T,Y]=ode45(H,[0:1:time],[N0]);
end
Commands: when b=d
par=[0.01 0.01 400];
time=500;
[~,Y]=model_growth(par,time)
plot(T,Y)
and
par=[0.01 0.001 400];
time=500;
[~,Y]=model_growth(par,time)
plot(T,Y)
Torsten
Torsten 2023 年 2 月 2 日
I don't understand your point.
The parameters are different for the two cases - so the results are different.
Saira
Saira 2023 年 2 月 2 日
You are right, I think that was what I was doing wrong. but
par=[0.01 0.01 400];
time=500;
[T,Y]=model_growth(par,time)
plot(T,Y)
This gives a straight line at 400 point when I use it in indivdual function, but at 0 when in a loop. Can you elaborate, what is going on here.
Torsten
Torsten 2023 年 2 月 2 日
Maybe 400 looks like 0 in the loop plot because the second plot gives such large results for N.
Look at the plot from my code above. The red line looks like 0 because the blue line gets up to about 7e19, but is 400.
Saira
Saira 2023 年 2 月 2 日
Thank you so much.
I have just started coding so don't know these subtle changes.

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2023 年 2 月 1 日

コメント済み:

2023 年 2 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by