Save variables while using ode solver

3 visualizaciones (últimos 30 días)
Deepa Maheshvare
Deepa Maheshvare el 24 de Oct. de 2019
Comentada: Ibrahim Bakry el 5 de Mayo de 2021
[t,y]=ode45(@fun,tspan,yo);
function dy=fun(t,y)
dy=zeros(3,1);
constant1 = k1*y(3)
dy(1)= constant1;
dy(2)=f1(y(3),y(2))
dy(3)=f2(y(1),y(2))
end
I want to save the variable, constant1, each time the ode solver calls the function fun.
Is there a way to get the iteration count?
If yes, I can use
save_constant1(iter) = constant1.
Any suggestions?
  2 comentarios
Steven Lord
Steven Lord el 24 de Oct. de 2019
What do you want to happen if the ODE solver evaluates your function for a particular time step but then rejects that step and evaluates your function at an earlier time (a smaller time step from the previous time?)
Deepa Maheshvare
Deepa Maheshvare el 25 de Oct. de 2019
Thanks a lot for the response. I suppose the step that is rejected will also be counted in the total number of iterations that the solver takes to solve the differential equations. So, I'd like to save the value of variable, constant1, for all iterations. Or, I could also create two variables, one that stores for all iterations and the other that skips the values computed at the steps that are rejected.

Iniciar sesión para comentar.

Respuesta aceptada

Dinesh Yadav
Dinesh Yadav el 29 de Oct. de 2019
What you can try is at every iteration save the variables into a .mat file
save('odef.mat', 'constant1', '-append')
This will append the new values and keep your old values also.
Hope it helps.
  2 comentarios
alesmaz
alesmaz el 1 de Nov. de 2019
Hi, I've tried your method in my code, where p is a vector of 4 parameters that I need to save at each ODE iteration in order to calculate their confidence intervals. I've put your string before the 'end' but my code overwrites always p without keeping the previous one.
Is there a way to save p at each ODE iteration without losing the one obtained in the previous iteration? Thank you.
function f=bernardode(p,t)
t=temposp;
options=odeset('AbsTol',1e-6,'RelTol',1e-6);
[T,Z]=ode45(@bernard2,t,z0,options);
function dz = bernard2(t,z)
dzdt=zeros(4,1);
dzdt(1)=-(p(3)*(1-qmin/z(1)))*(((Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2))))/(p(4)+(Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2)))))*z(1);
dzdt(2)=(p(3)*(1-qmin/z(1)))*(((Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2))))/(p(4)+(Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2)))))*z(2);
dzdt(3)=p(2)*z(4)+(p(3)*(1-qmin/z(1)))*(((Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2))))/(p(4)+(Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2)))))*(1-p(1)-z(3));
dzdt(4)=(p(1)-z(4))*(p(3)*(1-qmin/z(1)))*(((Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2))))/(p(4)+(Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2)))))-p(2)*z(4);
dz=dzdt;
end
f=Z;
end
Ibrahim Bakry
Ibrahim Bakry el 5 de Mayo de 2021
Not working with ode45

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by