How to update a class property inside ode45 that depends on ode45's results?

1 view (last 30 days)
Hi, I am working on a research project that involves a binary search of a dynamic system. This binary search you can imagine as two 'bound' lines that determine the extrema of the search. See the image for reference.
These bounds rely on 'measurements' taken at particular time steps, and need to be updated for the next iteration. My question is how do I update these bounds during ode45? I first tried storing the bounds as a class property, but I have no way of 'reconstructing' the bounds to plot them after running ode45.
I then tried using a global variable, but it is an ugly way of solving the problem, in my opinion, plus I have to eliminate the time steps that don't 'line-up' with the fixed output steps. I have also started to include the bounds into the input and output (x and dx) vector of my dynamics function, but since ode45 will integrate this change over each time-step, I don't think this will give me the correct value of the bounds at each subsequent time step.
I appreciate any ideas and help!

Accepted Answer

Walter Roberson
Walter Roberson on 26 Feb 2023
Edited: Walter Roberson on 26 Feb 2023
You do not do that. Nothing like that is suitable for any ode*() function, all of which depend upon the ode function always returning the same value when evaluated with the same parameters.
Remember that every iteration of ode45() involves evaluating the function at 6 different combinations of input, and then carefully using the results to predict the way that the ODE is evolving. And then it cross-checks two kinds of predictions, and if there is too much of a mis-match then it goes back and tries again from the same location with a shorter step -- something that can happen repeatedly. So as an area of rapidly change approaches, the ode function could be evaluated dozens of times to try to figure out what is happening with the curve; it would be a Problem if the curve is altering as you go because one of the function evaluations decided to update parameters that are being used for the other calculations -- quite possibly evaluated at a location that ode45 has no intention of visiting but needs to know how steep the function is in that direction in order to figure out the best route.
  2 Comments
Steven Hyland
Steven Hyland on 26 Feb 2023
Ok, thank you Walter. That's what I figured would be the case. Then do you have any suggestions, say, if I have a rigid body (the circle) and I want it to change directions at certain moments? Would several separate calls to a solver work in that case? And is that a poor approach?
Walter Roberson
Walter Roberson on 26 Feb 2023
When you are using the ode*() functions, in each instance that you have a discontinuity in the ODE function or its first two derivatives, then you need to terminate the call to the ode*() function and resume from where you left off (possibly after having updated the state.)
The case where the discontinuities occur at known times are the easiest ones: you just arrange the tspan for each call to end at the time of the next discontinuity.
If the discontinuities occur at unknown times, then you need to create an event function. The event function is handed control at the place where new configuration would otherwise be accepted, and the event function has the opportunity to signal for termination. But be aware that the termination will not be immediate: the wind-up logic will proceed to try to create new points between the previously accepted points and the location of termination, trying to run the ode right up to the boundary.
For example if you were bouncing a ball, you would want to terminate each time that the ball hits the floor, but what the event function will be handed is a state in which the position is already below the floor, and the signal for termination would be interpreted as instructions to back up to try to figure out exactly where the floor is.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!