law. In each iteration of the gradient-based method for finding the optimal switching time, we first solve a continuous-time optimal control problem for the system with a fixed switching times. Then, the cost function gradient with respect to the switching times is calculated in order to determine the descent direction for the switching times update. This approach is similar to [@xu04]. However, instead of using the two-point BVP solver for optimizing $J(\vt)$ with respect to $\vu$ and calculating its gradient with respect to $\vt$, we use a more efficient approach. This facilitates implementing this algorithm in real-time on complex systems such as legged robots.

Our **O**ptimal **C**ontrol for **S**witched **S**ystems algorithm (OCS2 algorithm) consists of two main steps: a method which synthesizes the continuous input controller and a method which calculates the parameterized cost function derivatives with respect to the switching times. For synthesizing the continuous input controller, OCS2 uses the SLQ algorithm in Algorithm \[alg:slq\]. As a dynamic programming approach, SLQ uses the Bellman equation of optimality to locally estimate the value function and consequently the optimal control law. In the second step of OCS2, we use the approximated problem for calculating the value function from the first step 