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

조회 수: 9 (최근 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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by