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

9 visualizaciones (últimos 30 días)
Danny Helwegen
Danny Helwegen el 6 de Mzo. de 2021
Comentada: Star Strider el 6 de Mzo. de 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.

Respuesta aceptada

Star Strider
Star Strider el 6 de Mzo. de 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 comentarios
Danny Helwegen
Danny Helwegen el 6 de Mzo. de 2021
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 el 6 de Mzo. de 2021
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by