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

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

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

Use an event function and stop/interrupt integration when a has reached the critical value. After returning to the calling program, you can change your equations to be integrated and continue.
The ballode example does essentially this, stopping when the ball's height reaches y = 0 (the ball hitting the ground) and starting the solver anew after modifying the initial conditions based on the results of the previous call to the solver (reflecting the ball bouncing off the ground.)
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.
In short: After "a" has reached the critical value, ode45 will return control to the calling program and you can do anything you like with the result obtained so far, your differential equations, your initial conditions and your tspan.

Sign in to comment.

More Answers (0)

Categories

Find more on Numerical Integration and 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!