# Solving ODEs of Switched Systems using MATLAB ODE suite

I have the following code to execute:

$X$ = [0;0;0;0]; $sw$ = 0;

for k = 1 : n
if (sw(k) == 0 && X(2,k)> 0.7 ) || (sw(k) == 1 && X(1,k) >0)
sw(k+1) = 1;
X(:,k+1) = X(:,k) +h*(A1*X(:,k) + B1*u);
else
sw(k+1) =0;
X(:,k+1) = X(:,k) +h*(A*X(:,k) + B*u);
end

end


This is fine. I wanted to solve the same ODE using the bulit-in ode suite. How do I check for $sw$ and $X$ after each iteration to see which one of the two equations to solve.

• Welcome to SciComp.SE! It would help if you actually wrote down (using LaTeX/MathJax) the ODE you are trying to solve. You should also look at the odeset documentation in Matlab (especially about Events). But be aware that pure "how do I use this function in Matlab" questions are off-topic here, and better asked at Matlab Answers. (Your question is borderline, depending on how you edit it.) – Christian Clason Aug 28 '16 at 20:21

## 1 Answer

You can use the "event location" feature of MATLAB's ODE suite to integrate the system of equations in one of the two states and stop the integration when the condition has been reached for a state change. You can then restart the integration in the other state and setup an event trigger to stop the integration at the next state change. See

http://www.mathworks.com/help/matlab/math/ode-event-location.html