MATLAB: Implemention of a state machine in ode45 (not in Simulink)
MATLABode45state machinestateflow
How can I implement a state machine (just like Simulink StateFlow) in Matlab ode45, without using Simulink?
Best Answer
You cannot. State machines imply discontinuous equations. Any one call to ode45 must have continuous equations.
You need to use event functions to detect the change of state and signal termination of integration. Then use the output of ode45 to provide the initial values to another ode45 call.
You cannot use the ode45 function directly in the above example code as it will output integrated function values for all the time points instead of a single time-step value as required here. However, you can implement single-step updates of higher order methods such as Runge-Kutta (https://en.wikipedia.org/wiki/Runge%2525E2%252580%252593Kutta_methods ) instead of the Euler method for better accuracy.
Best Answer