How to use changing logical states in a MATLAB intregrator

1 view (last 30 days)
Hello, I would appreciate any help on the following problem:
In a nutshell:
  • I need to make an integrator like ode45() to access a variable during integration whose value is changed during integration
More in detail:
I'm trying to integrate a vector field such as with an integrator like ode45(). During the integration, at each step t(i) I need to edit a vector that contains boolean values, for example:
vec is declared in the main as vec = logical([0 0 0]');
  • Step t(1): perform computations inside and assign vec(2) = true
  • Step t(2): perform computations inside and assign vec(2) = false
  • Step t(2): perform computations inside and assign vec(1) = false
  • ...
The issue is that I need to keep the information on what are the components of vec between steps (e.g. from step t(2) to step t(3) I'd need to know the values of vec([1,2,3]). MATLAB can access a variable declared in the main during the ODE integration but it cannot modify it.
I've tried to introduce the components of vec as states in x, but that is a problem because their derivative can't be specified as .
How would you recommend me to approach this problem?
Thank you very much.
  2 Comments
Steven Lord
Steven Lord on 2 Jul 2021
What is the mathematical form of the differential equations that you're trying to solve? How is this vec vector used in those equations?
How do you want to handle changes to your vec vector when the ODE solver tries to take a step, realizes that step does not satisfy the tolerances, rejects the step, and takes a smaller step?
Ícar
Ícar on 2 Jul 2021
Edited: Ícar on 2 Jul 2021
Hi Steven,
the mathematical form of the differential eqs is very complicated, it's a non-linearlly controlled astrodynamics problem.
Regarding your second question, I would like that the value of vec at the step t(i) was fixed once the step t(i) is passed, and that the subsequent step computed by the solver (and by your words the finally used smaller step) takes the value of vec at t(i) to compute vec at t(i+1).
Actually I have found a solution which consists in:
Declare inside a persistent variable such as follows:
par.vec0 = logical(zeros(3,1));
sol = ode45(@(x,t) vectorField(x,t,par),x0,..)
function dx = vectorField(x,t,par)
persistent vec
if isempty(vec)
vec = par.vec0;
end
vec(1) = not(vec(1))
if vec(1) == true
dx = 1;
else
dx = -1;
end
end
I suspect that with this technique in the case that the solver computes first a big step (changes value of vec), and then recomputes a smaller step, as you mentioned, the vec value wouldn't be OK since it would have been recomputed and not be the one of t(i). Would you have any recommendation in that sense?

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 2 Jul 2021
It sounds from your description that the solution to your system of differential equations at a given time depends on the solution of the system at an earlier time. If that's the case you have a delay differential equation rather than an ordinary differential equation and you should use dde23, ddesd, or ddensd instead of an ODE solver like ode45. See this category in the documentation for more information about these functions and several examples

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!