How to vary a variable in a system of ODE's?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Danny Helwegen
am 6 Mär. 2021
Kommentiert: Star Strider
am 6 Mär. 2021
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.
0 Kommentare
Akzeptierte Antwort
Star Strider
am 6 Mär. 2021
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!