( \delta_{i,j} - \delta_{i-1,j} ) \vw(z) + (t_{i}-t_{i-1}) \partial_{t_j}\!\vw(z)  \label{eq:riccati_nablaSv} \\
-\frac{d (\partial_{t_j}s(z))}{dz}    &= ( \delta_{i,j} - \delta_{i-1,j} ) w(z) + (t_{i}-t_{i-1}) \partial_{t_j}\!w(z) , \label{eq:riccati_nablas}\end{aligned}$$ where we have $$\begin{aligned}
&\partial_{t_j}\!\vW(z) = \vA(z)^\top \partial_{t_j}\vS(z) + \partial_{t_j}\vS(z)  \vA(z) - \partial_{t_j}\vL(z)^\top \vR(z) \vL(z) \notag \\
& \hspace{14mm} - \vL(z)^\top \vR(z) \partial_{t_j}\vL(z)  \\
&\partial_{t_j}\!\vw(z) = \partial_{t_j}\vq(z) + \vA(z)^\top \partial_{t_j}\vs(z) - \partial_{t_j}\vL(z)^\top \vR(z) \vl(z) \notag \\
& \hspace{14mm} - \vL(z)^\top \vR(z) \partial_{t_j}\vl(z) \\
&\partial_{t_j}\!w(z)  = \partial_{t_j}q(z) - 0.5\alpha (2-\alpha) \big( \partial_{t_j}\vl(z)^\top \vR(z) \vl(z)  \notag \\
& \hspace{14mm} + \vl(z)^\top \vR(z) \partial_{t_j}\vl(z) \big) \\
&\partial_{t_j}\vl(z) = - \vR(z)^{-1} \big( \partial_{t_j}\vr(z) + \vB(z)^\top \partial_{t_j}\vs(z) \big)  \\
&\partial_{t_j}\vL(z) = - \vR(z)^{-1} \vB(z)^\top \partial_{t_j}\vS(z) .\end{aligned}$$ The derivative of the cost coefficients with respect to $t_j$ is $$\begin{bmatrix} \partial_{t_j}\vq(z) \\ \partial_{t_j}\vr(z) \\ \partial_{t_j}q(z) \end{bmatrix} = 
\begin{bmatrix} \vQ(z) & \vP(z) \\ \vP(z)^\top & \vR(z) \\ \vq(z)^\top & \vr(z)^\top \end{bmatrix}
\begin{bmatrix} \partial_{t_j}\vx(z) \\ \partial_{t_j}\vu(z) \end{bmatrix} .  
\label{eq:intermidiate_cost_function_sensitivity}$$ Finally, the terminal condition are defined as $$\partial_{t_j}\!\vS(I)=\mathbf{0}, \hspace{2mm} \partial_{t_j}\!\vs(I)=\vQ_f \partial_{t_j}\!\vx(I), \hspace{2mm} \partial_{t_j}\!s(I)=\vq_f^\top \partial_{t_j}\!\vx(I),
\label{eq:final_cost_function_sensitivity}$$ and the transversality conditions are $$\partial_{t_j}\!\vS(i^-) = \partial_{t_j}\!\vS(i^+),  \hspace{2mm} 
\partial_{t_j}\!\vs(i^-) = \partial_{t_j}\!\vs(i^+),  \hspace{2mm}
\partial_{t_j}\!s(i^-) = \partial_{t_j\!}s(i^+).$$

**Given** - Mode switch sequence and switching times - Cost function and system dynamics **Initialization** - Initialize the controller with a stable control law, 