# The Optimal Erection of the Inverted Pendulum
## 4 Numerical Solution
### Baseline  
The first solution to the OCP is found numerically using a direct collocation method. The transcription of the OCP into a nonlinear program (NLP) is achieved using GPOPS-II [27], which employs a Legendre-Gauss-Radau (LGR) discretization with mesh refinement. The derivatives are computed through the automatic-differentiation (AD) tool ADIGATOR [28]. The NLP solver is IPOPT [29]. The setup includes a mesh tolerance for GPOPS-II of \(10^{-6}\), with a error for the IPOPT solver of \(10^{-7}\). All simulations are started with the default mesh of GPOPS-II, which consists of 10 mesh fractions and 4 collocation points per fraction. The mesh refinement method employed is the hp-PattersonRao [27]. The solution is shown in Figure 2a: the optimal control is bang (\(u=+1\) for \(t<0.69\)), bang (\(u=-1\) for \(t<0.70\)), singular (\(t<1.17\)), bang (\(u=-1\) for \(t<2.72\)), bang (\(u=+1\) for \(t<4.32\)) and bang (\(u=-1\) for \(t<4.915\)). The maneuver time is \(t_{f}^{(i)}=4.9151\) in this case. Not surprisingly, numerical issues are clearly visible along the singular arc. As a side note, the computed solution was used to verify that \(\mathcal{H}\equiv-1\) for the entire simulation, and that \(\mathcal{H}_{u}=0\) between 0.70 and 1.17, i.e., along the singular arc. This is possible since the co-state of the OCP, which is used to evaluate \(\mathcal{H}\), can be estimated from the co-state of the associated NLP [30; 31]. In the second solution, the OCP is divided in three phases: before, during and after the singular arc (as was suggested by the solution in Figure 2a). In the second phase, the optimal control is computed using (29), while the first and third bang phases are computed as before. The switching time between the phases is subject to optimization. In order to compute the singular phase, the state vector was inflated to include the control, i.e., \(x^{(ii)}=[y,\theta,v_{y},v_{\theta},u]^{T}\). To this end, the dynamic system (7) included (29)--the related initial condition was left free to be optimized (\(u_{0}\approx-0.96\) was obtained in this case). The solution is shown in Figure 3a, while in the (b) part of the figure, a side view of the pendulum and cart during the optimal section is shown; the singular-arc phase is shown in gray. The switching times obtained by the optimization process are \(0.6880\,\mathrm{s}\), \(0.7079\,\mathrm{s}\), \(1.1737\,\mathrm{s}\), \(2.7218\,\mathrm{s}\), and \(4.3217\,\mathrm{s}\), with the singular arc occurring between \(0.7079\,\mathrm{s}\) and \(1.1737\,\mathrm{s}\). The maneuver time is again \(t_{f}^{(ii)}=4.9151\), which is identical to \(t_{f}^{(i)}\) (indeed, the singular control is essentially a low-pass filtered version of the oscillating control shown in the first simulation). The optimal control over the singular arc satisfies (27). In the third solution, a pure bang-bang solution is pre-imposed, as shown in [21]. In other words, the control is only allowed to be \(u=\pm 1\). Again, the GPOPS-II-ADIGATOR-IPOPT combination is employed, with the switching times as optimization parameters. In this case, Figure 2b shows the solution, which is identical to the one reported in [21], with \(t_{f}^{(iii)}=4.9161\), which is greater than \(t_{f}^{(i)}\) and is thus sub-optimal. The fourth solution protocol employs a regularized control that was designed to avoid the singular arc. This is a well-known approach which is employed practically in a number of applications including the minimum-time maneuvering of road vehicles [32]. In this case, a Lagrange term is added to the performance index so that  
\[J=t_{f}+\int_{0}^{t_{f}}w_{u}u^{2}dt, \tag{34}\]  
Figure 3: **(a)** Solution with three phases, and (b) stroboscopic movie with 50 equally spaced time steps; the gray lines denote the singular arc. Figure 2: **(a)** Single-stage numerical solution, and (b) bang-bang solution [21]. where \(w_{u}\) is a control weighting that must be chosen'small enough'.