How to vary a variable in a system of ODE's?

2 ビュー (過去 30 日間)
Danny Helwegen
Danny Helwegen 2021 年 3 月 6 日
コメント済み: Star Strider 2021 年 3 月 6 日
Hello everybody,
I want to make a plot where I from where I can find and show the best value for a parameter. In order to do this I have to vary a variable in my system of ODE's (here a simplified example from the Matlab site). In the lines of codes below I show the set of ODE's, here Tau is the variable that I want to vary. In other words, I want solutions for a system where Tau = 1, Tau = 2, Tau = 3, etc. I tried to do this using a for-loop but it doesn't seems to be working as only the first two columns are calculated and the last ones remain their inital values. Does somebody have a smart solution?
function dpdt = Tester(t,p,val)
dpdt = zeros(2*val,1);
delta = 0.02;
beta = 0.01;
for tau = 1:1:val
dpdt(1) = tau * p(1) .* (1 - beta*p(2));
dpdt(2) = tau * p(2) .* (-1 + delta*p(1));
end
end
And the solution script is given below:
tspan = [0 15]
val = 2;
[t,p] = ode45(@Tester,tspan,[5 5 50 50],[],val);
plot(t,p)
title('Predator/Prey Populations Over Time')
xlabel('t')
ylabel('Population')
legend('Prey','Predators')
Thanks in advance for your help.

採用された回答

Star Strider
Star Strider 2021 年 3 月 6 日
Try this slightly changed version of your code:
function dpdt = Tester(t,p,tau)
dpdt = zeros(2,1);
delta = 0.02;
beta = 0.01;
dpdt(1) = tau * p(1) .* (1 - beta*p(2));
dpdt(2) = tau * p(2) .* (-1 + delta*p(1));
end
tspan = linspace(0, 15, 150);
Tau = 1:5;
for k = 1:numel(Tau)
[t,p{k}] = ode45(@(t,p)Tester(t,p,Tau(k)),tspan,[5 50]);
end
Np = numel(Tau);
figure
for k = 1:Np
subplot(Np,1,k)
plot(t,p{k})
title(sprintf('Predator/Prey Populations Over Time \\tau = %d',Tau(k)))
xlabel('t')
ylabel('Population')
legend('Prey','Predators', 'Location','EastOutside')
end
pos = get(gcf,'Position');
set(gcf,'Position',pos+[0 -200 0 200])
.
  4 件のコメント
Danny Helwegen
Danny Helwegen 2021 年 3 月 6 日
Ah yes, didn't know that was possible. This is exactly what I needed, thank you very much for all you help.
Star Strider
Star Strider 2021 年 3 月 6 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by