# A Wheeled Inverted Pendulum Learning Stable and Accurate Control from Demonstrations
## 4 Learning Path Following and Balance Controlling Simultaneously
### Teaching the Control Model with the Nonholonomic and Underactuated Constraints
\begin{array}{c}\hat{x}=\hat{f}_{1}\left(x,y,z,\theta,\varphi|\beta^ {*}\right)\\ \hat{y}=\hat{f}_{2}\left(x,y,z,\theta,\varphi|\beta^{*}\right)\\ \hat{z}=\hat{f}_{3}\left(x,y,z,\theta,\varphi|\beta^{*}\right)\end{array} \right., \tag{16}\]  
without the influences of the external factors (Figure 5). Additionally, we fixed the values of \(\theta^{k}\) and \(\varphi^{k}\); i.e., considering them as constants at that instant. Therefore, the corresponding input of the sub-system \(\left(x_{I}^{k},y_{I}^{k},z_{I}^{k}\right)^{\mathrm{T}}\) satisfying that  
\[\left\{ \begin{array}{c}x^{k+1}-x_{I}^{k}=\hat{f}_{1}\left(x_{I}^{k},y_{I}^{k },z_{I}^{k},\theta^{k},\varphi^{k}|\beta^{*}\right)\cdot\Delta t\\ y^{k+1}-y_{I}^{k}=\hat{f}_{2}\left(x_{I}^{k},y_{I}^{k},z_{I}^{k},\theta^{k}, \varphi^{k}|\beta^{*}\right)\cdot\Delta t\\ z^{k+1}-z_{I}^{k}=\hat{f}_{3}\left(x_{I}^{k},y_{I}^{k},z_{I}^{k},\theta^{k}, \varphi^{k}|\beta^{*}\right)\cdot\Delta t\end{array} \right. \tag{17}\]  
where \(\Delta t\) is the sampling instant during the reproduction process. Figure 5: Adjusting the extra-dimensional component by a constructed constrained optimization problem. In (**a**), the learned dynamic system outputs the predicted robot position and the corresponding extra-dimensional component at \(k+1\)th instant (with the state variable at the \(k\)th instant as system input). The external factors, including the nonholonomic constraints and underactuated constraints, influence the robot’s movement, and the robot moves to an actual position deviating from the predicted one. In order to adjust the extra-dimensional component which cannot be influenced by the external factors, we suppose there is a similar robot motion trajectory (red curve on the \(x\)-\(y\) plane in (**b**)) passing near the current robot trajectory (blue curve on the \(x\)-\(y\) plane in (**b**)). The robot motion trajectory passes through actual robot position at the \(k+1\)th instant, which is also yielded by the learned dynamic system while it starts from another start point. In (**c**), considering the two trajectories (red and blue curves in the \(x\)-\(y\)-\(z\) Cartesian space) are alike, the derivative directions at the reproduced points on them at the \(k\)th instant should be also similar. Using those as constraints and combining the learned dynamic system function, the previous point on the red marked trajectory at \(k\)th instant can be acquired by solving the constrained optimization in Equation (19). Inputting it to the learned dynamic system, the next state variable can be predicted, the extra-dimensional component of which is the value to be used for adjusting the current component at \(k+1\)th instant. Meanwhile, \(\left(x_{I}^{k},y_{I}^{k},z_{I}^{k}\right)^{\mathrm{T}}\) is supposed to be near \(\left(x^{k},y^{k},z^{k}\right)^{\mathrm{T}}\); accordingly, the derivative directions at these two points are expected to be close as well, which constitutes an optimization constraint as  
\[ \begin{split}\cos\left\langle\left(\hat{f}_{1}\left(x_{I}^{k},y_{I }^{k},z_{I}^{k},\theta^{k},\varphi^{k}\left|\beta^{*}\right.\right),\hat{f}_{2 }\left(x_{I}^{k},y_{I}^{k},z_{I}^{k},\theta^{k},\varphi^{k}\left|\beta^{*} \right.\right),\hat{f}_{3}\left(x_{I}^{k},y_{I}^{k},z_{I}^{k},\theta^{k}, \varphi^{k}\left|\beta^{*}\right.\right)\right)^{\mathrm{T}},\\ \left(\hat{f}_{1}\left(x^{k},y^{k},z^{k},\theta^{k},\varphi^{k} \left|\beta^{*}\right.\right),\hat{f}_{2}\left(x^{k},y^{k},z^{k},\theta^{k}, \varphi^{k}\left|\beta^{*}\right.\right),\hat{f}_{3}\left(x^{k},y^{k},z^{k}, \theta^{k},\varphi^{k}\left|\beta^{*}\right.\right)\right)^{\mathrm{T}}\right\rangle <\omega\end{split} \,, \tag{18}\]  
where \(\omega\) is a small positive number. The assumed sub-system input \(\left(x_{I}^{k},y_{I}^{k},z_{I}^{k}\right)^{\mathrm{T}}\) in the \(k\)th instant is subsequently acquired by solving the following constrained optimization problem with \(\left(x^{k},y^{k},z^{k}\right)^{\mathrm{T}}\) as the initial guess. \[