How can I saturate the state variable in ODE solver function in MATLAB?

17 views (last 30 days)
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
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!