Passing a temporary parameter from time step to time step using ode113?

7 visualizaciones (últimos 30 días)
Hello,
i'm using ode113 to solve some differential equations wich i stored in a function called RSDGL:
[t,F]=ode113(@RSDGL,[t0 tf],initial_conditions);
However in the function RSDGL i am using fzero to calculate zeros of functions wich appear in the differential equations as those zeros are time dependent. The problem i have is that i want to memorize those zeros temporarly to use them as starting points when i use fzero in the next time step (when ode113 calls RSDGL again). This should increase the performance as those zeros only vary little between each timestep. So i want to programm something like:
zero_new=fzero(function,zero_old);
inside my RSDGL function. I dont really have a clue how to store and overwrite those zeros and how to pass them each time ode113 calls my function RSDGL. I hope you understand my problem, as english is not my first language.

Respuesta aceptada

Jan
Jan el 22 de Jul. de 2018
Editada: Jan el 22 de Jul. de 2018
This is a job for a persistent variable:
function dx = RSDGL(t, x)
persistent zero_old
if isempty(zero_old)
zero_old = 1; % Use a meaningful value!
end
zero_new = fzero(function, zero_old);
zero_old = zero_new;
...
end
It is smart to implement a method to reset this variable, e.g. for different runs of your code. Using clear RSDGL works, but I'd prefer an explicit method:
function dx = RSDGL(t, x)
persistent zero_old
if nargin == 1 % Reset!
zero_old = [];
return;
end
if isempty(zero_old)
zero_old = 1; % Use a meaningful value!
end
...
Then this resets the persistently store value:
RSDGL('reset')
This is clear and easy to understand during reading the code.
  1 comentario
lu_po
lu_po el 22 de Jul. de 2018
Thanks a lot! This worked 100% for me. My programm is now running 50% faster wich is crucial for the calculations i want to make.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming 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