# The Optimal Erection of the Inverted Pendulum
## 3 Optimal Control Problem  
The optimal control problem (OCP) is one of finding a control \(u\), with \(|u|\leq u_{\text{max}}=1\), which moves the system from the stable equilibrium \(y=\dot{y}=\theta=0\) (straight-down) to the unstable equilibrium \(y=\dot{y}=\dot{\theta}=0\) and \(\theta=\pi\) (straight-up) in the minimum time. Note that both the initial and terminal states are specified.  
The optimal control problem will be tackled in the following steps. First, the control Hamiltonian is derived, which is shown to be linear in the control \(u\). Next, the switching function is derived (from the control Hamiltonian). Finally, under the assumption that a singular arc might exist, a feedback control law for the singular sub-arc is derived. This calculation is based on the assumption that the switching function and its various time derivatives must be zero. The control law for the singular arc is expressed both explicitly and as the a solution of a Riccati differential equations--these control laws are equivalent.  
The OCP cost is simply the elapsed time  
\[J=t_{f}. \tag{8}\]  
The control Hamiltonian is given by  
\[\mathcal{H}=\lambda_{1}v_{y}+\lambda_{2}v_{\theta}+\lambda_{3}\frac{(v_{\theta }^{2}+\cos\theta)\epsilon\sin\theta+u}{1-\epsilon\cos^{2}\theta}-\lambda_{4} \frac{\epsilon v_{\theta}^{2}\sin\theta\cos\theta+\sin\theta+u\cos\theta}{1- \epsilon\cos^{2}\theta} \tag{9}\]where \(\lambda=[\lambda_{1},\lambda_{2},\lambda_{3},\lambda_{4}]^{T}\) are the Lagrange multipliers. Evidently  
\[\mathcal{H}_{u}=\frac{\lambda_{3}-\lambda_{4}\cos\theta}{1-\epsilon\cos^{2} \theta}. \tag{10}\]  
The co-state equations for the OCP are given by  
\[\dot{\lambda}=-\frac{\partial\mathcal{H}}{\partial x}, \tag{11}\]  
which in expanded form are  
\[\dot{\lambda}_{1} = 0, \tag{12}\] \[\dot{\lambda}_{2} = \frac{\epsilon(1-2\cos^{2}\theta)(\lambda_{3}-\lambda_{4}v_{\theta }^{2})+(\lambda_{4}-\lambda_{3}ev_{\theta}^{2})\cos\theta-\lambda_{4}u\sin \theta}{1-\epsilon\cos^{2}\theta}+\] (13) \[\frac{2\{[(\lambda_{3}-\lambda_{4}v_{\theta}^{2})\epsilon\sin \theta-\lambda_{4}u]\cos\theta-(\lambda_{4}-\lambda_{3}ev_{\theta}^{2})\sin \theta+\lambda_{3}u\}\epsilon\sin\theta\cos\theta}{(1-\epsilon\cos^{2} \theta)^{2}},\] \[\dot{\lambda}_{3} = -\lambda_{1},\] (14) \[\dot{\lambda}_{4} = -\lambda_{2}-2\epsilon v_{\theta}\sin\theta\frac{\lambda_{3}- \lambda_{4}\cos\theta}{1-\epsilon\cos^{2}\theta}. \tag{15}\]  
The optimal control \(u^{*}\) is given by  
\[u^{*}=\min_{u}\mathcal{H}(x^{*},\lambda^{*},u). \tag{16}\]  
Since \(\mathcal{H}\) is linear in the control \(u\), the optimal control is given by  
\[u^{*}=\begin{cases}-1&\text{if}\quad\Phi>0\\ \text{unknown}&\text{if}\quad\Phi=0\\ 1&\text{if}\quad\Phi<0,\end{cases} \tag{17}\]  
where the switching function is  
\[\Phi=\lambda_{3}-\lambda_{4}\cos\theta; \tag{18}\]  
note that \((1-\epsilon\cos^{2}\theta)>0\). The control is either bang-bang, or possibly a bang-singular-bang (if \(\Phi=0\) over a finite interval).  
The transversality condition associated to the free final time is  
\[\mathcal{H}(t_{f})=-1; \tag{19}\]  
see, for example, Equation (8.73) in [1]. Since \(\mathcal{H}\) is not explicitly time-dependent, (19) implies that \(\mathcal{H}(t)=-1\). Since the state variables are specified at both the initial and final times, there are no pre-specified boundary conditions on the Lagrange multipliers. See Equation (8.72) in [1].