substitute of persistent command
7 views (last 30 days)
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
%% Call ode15i
[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);
if (some condition satisfied )
some equations with sij ejesj Tj variables
% do not update value of sij ej esj Tj
some equation with sij ejesj Tj variables
% Update value of sij Tj esj ej
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
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:
sij = ;
Tj = 
esj = ;
ej = ;
[yo_new,ypo_new] = decic(@tryingode,to,yo,[fixed_yo],ypo,);
sol = ode15i(@tryingode,[0 20],yo_new,ypo_new);
% nested function:
function dydt = tryingode(t,y,yp)
sij = 80;
dydt = .. some equations