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

11 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by