How can I saturate the state variable in ODE solver function in MATLAB?
16 views (last 30 days)
Show older comments
MathWorks Support Team
on 14 Jul 2020
I would like to saturate my state variable "y" to an upper limit when solving an ODE in MATLAB. This is possible in Simulink using Saturation Dynamic. But is this possible in MATLAB?
The closest available option in documentation appears to be the "event" function. But this will terminate the integration when the limit is reached and I would like to continue integration with the value of the specific variable at the saturated level.
Accepted Answer
MathWorks Support Team
on 14 Jul 2020
Specifying custom lower or upper saturation bounds for variables inside the ODE solvers is not currently supported with MATLAB. The only such condition that is supported directly is non-negativity using the ode-options parameters.
One way to work around this limitation is to have a conditional statement inside "odefun" to check if the value of the state variable "y" is greater than or equal to the upper limit. If so, you can set the corresponding "dydt" field to be "min(0,dyComputed)", where "dyComputed" is the originally computed estimate of the derivative of "y". This will make the value saturate once it reaches/passes the upper limit and let the integration continue.
4 Comments
Steven Lord
on 8 May 2023
In ballode the ODE being solved doesn't change, just the initial conditions, but you could switch to using a different ODE function. You can also start the tspan at a time later than 0, which ballode does do. It solves the ODE from tstart to tfinal and the loop that resets the initial conditions also changes tstart to the time the event (the ball bouncing on the ground) occurred.
More Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!