# substitute of persistent command

7 views (last 30 days)
nisha bhatt on 11 Oct 2020
Commented: Bjorn Gustavsson on 12 Oct 2020
Hi,
I am using persistent command for 4 varibles within a local function...That function is being called in ode15i solver ...The ode15i solver is being called in main function in a loop...for example
function main()
for i=1:1:400
%% Call ode15i
to=tdash(i);
[yo_new,ypo_new] = decic(@(t,y,yp)tryingode(t,y,yp),to,yo,[fixed_yo],ypo,[]);
[sol] = ode15i(@(t,y,yp)tryingode(t,y,yp),[0 20],yo_new,ypo_new);
[y,yp]=deval(sol,to);
end
function dydt=tryingode(t,y,yp)
if isempty(sij)
sij=80;
Tj=26;
ej=1;
esj=0.71
end
if (some condition satisfied )
some equations with sij ejesj Tj variables
% do not update value of sij ej esj Tj
else
some equation with sij ejesj Tj variables
% Update value of sij Tj esj ej
sij=y(2);
Tj=y(1);
esj=y(3);
ej=y(4);
end
dydt=[some equations]
end
While doing so, I am getting wrong values of sij esj ej and Tj also to resolve this problem i used clear tryingode in the main function before the loop starts ...but still some problem is there...now if i dont want to use persistent ..what are the other methods to resolve this issue?

Steven Lord on 11 Oct 2020
What is the mathematical form of the differential equations you're trying to solve? I suspect (since you're updating the persistent variables with the values of the variables that get passed into your ODE function) they may involve values of the variables at previous time steps, in which case you should use a delay differential equation solver like dde23 instead of an ordinary differential equation solver like ode15i. -- Ordinary differential equation -- Delay differential equation
Bjorn Gustavsson on 12 Oct 2020
We can continue guessing. What are the differences between your zones? Does your functions f_i vary in some unknown or in some known way between zones? If you know that you should be able to work out how to combine the implementation of those functions for the different zones and have any of the ode-functions respect the transitions if you use the events handling that they are capable of. Good luck.

Walter Roberson on 12 Oct 2020
but what if I want to use ode15i
ode15i is a variable-step, variable-order (VSVO) solver based on the backward differentiation formulas (BDFs) of orders 1 to 5.
Variable step algorithms do not just pick a step size, calculate the function at (current plus step) and multiply the returned derivatives by the step size to determine the next location.
Variable step algorithms also evaluate at a series of carefully chosen additional points and make predictions based upon some of the locations and and other predictions based upon a different subset of the locations, and cross-check the predictions. Then if the values changed too much or if the predictions do not match each other well enough, then the step attempt is rejected, and a smaller step is attempted, and on and on until eventually either a small enough step is accepted or else the algorithm reaches the lower limit on step size and rejects the system of equations as being discontinuous.
Because of this "evaluate multiple times at different locations" together with "reject steps", then you can never count on function values remembered from a previous call: the previous call might have been a different one of the carefully chosen points from the current series, or might have been from a rejected attempt.
If your system cannot be represented with one of the DDE solvers, then you are probably going to have to switch to a fixed-step solver and figure out the number of control points per iteration in order to know how many previous values to remember.

Stephen23 on 11 Oct 2020
Edited: Stephen23 on 11 Oct 2020
You could use nested functions for this:
function main()
sij = [];
Tj = []
esj = [];
ej = [];
.. etc.
[yo_new,ypo_new] = decic(@tryingode,to,yo,[fixed_yo],ypo,[]);
sol = ode15i(@tryingode,[0 20],yo_new,ypo_new);
.. etc.
% nested function:
function dydt = tryingode(t,y,yp)
if isempty(sij)
sij = 80;
.. etc.
else
.. etc.
end
dydt = .. some equations
end
end
Stephen23 on 12 Oct 2020
Edited: Stephen23 on 12 Oct 2020
"the updated value of sij etc..vanishes when second iteration starts...I am calling function in a loop..."
Values do not just "disappear" from a workspace. If the values are defined in the parent workspace and you are calling a nested function in a loop then they should not (in general) disappear. Perhaps something you are doing makes them disappear: does any of your code call clear or anything similar?
By not uploading your actual function file/s you make it very difficult and slow for us to help you.
In any case, as Bjorn Gustavsson wrote, it is unlikely that relying on previous values will return anything sensible as all ODE solvers adjust the step sizes dynamically and some can even backtrack. This is very similar to a recurring discussion on this forum where someone imagines using some internal values after the ODE solving routine, but because they are sampled at non-evenly-spaced timepoints (unrelated to the requested sample times) and are not neccesarily sorted in time order either, they turn out to be quite non-trivial to use in practice.