## 3 Dynamic Optimal Control of Industrial Robot in Trajectory Tracking
### Gauss Pseudo-Spectral Method
#### 3.1.2 Discretization of State and Control Variables  
Combining the Lagrange interpolation method from the previous section, Legendre polynomials are used to discretize and approximate the state variables and control variables. Firstly, a time variable is introduced as \(\tau\), and the time interval is scaled in Equation (8) using the formula \(t=\frac{t_{f}-t_{0}}{2}\tau+\frac{t_{f}+t_{0}}{2}\). In this way, the time interval \([t_{0},t_{f}]\) is transformed into the [-1, 1] interval. By substituting the scale transformation result into Equation (8), the optimized control problem of the transformed industrial robot is obtained as follows:  
\[ \begin{array}{l}\min\int=\phi(x_{0},t_{0},x_{f},t_{f})+\frac{t_{f}-t_{0}}{2} \int_{-1}^{1}g(x(\tau),u(\tau),\tau)d\tau\\ \text{s.t.}\frac{dx(\tau)}{d\tau}=\frac{t_{f}-t_{0}}{2}f(x(\tau),u(\tau),\tau) \\ E(x(-1),t_{0},x(1),t_{f})=0\\ C(x(\tau),u(\tau),\tau)\leq 0,\tau\in[-1,1]\end{array} . \tag{14}\]  
Furthermore, by performing Lagrange interpolation approximation on the state variables at the LG point, we can obtain  
\[ \begin{array}{l}x(\tau)\approx X(\tau)=\sum\limits_{i=1}^{N+1}L_{i}(\tau)X (\tau_{i})=\sum\limits_{i=1}^{N+1}L_{i}(\tau)X_{i}\\ L_{i}(\tau)=\prod\limits_{i=1,f\
eq i}^{N+1}\frac{\tau-\tau_{i}}{\tau_{i}- \tau_{j}}=\frac{b(\tau)}{(\tau-\tau_{i})\dot{b}(\tau_{i})}\end{array} , \tag{15}\]  
where \(b(\tau)=\prod\limits_{i=1}^{N+1}(\tau-\tau_{i})\). Meanwhile, by discretizing the derivative of the state variable, thus it can be obtained that \(\dot{x}(\tau)\approx\dot{X}(\tau)=\sum\limits_{i=1}^{N+1}\dot{L}_{i}(\tau)X_{i}\). Then, only considering the derivative values of the state variables at the LG distribution point and defining that \(\tau=\tau_{k}\), and substituting it into equation (16), it can be obtained that  
\[\dot{x}(\tau_{k})\approx\dot{X}(\tau_{k})=\sum\limits_{i=1}^{N+1}L_{i}(\tau_{ k})X_{i}=\sum\limits_{i=1}^{N+1}D_{k_{i}}X_{i}, \tag{16}\]  
\[D_{k,i}=\dot{L}_{i}(\tau_{k})= \begin{cases}\frac{\dot{b}(\tau_{k})}{(\tau_{k}- \tau_{k})(\tau_{i})},&k\
eq i\\ \frac{b(\tau_{k})}{2b(\tau_{k})},&k=i.\end{cases}  \tag{17}\]  
Similarly, by approximating the control variable with LG point Lagrange interpolation over the time interval [-1, 1], we can obtain  
\[u(\tau)\approx U(\tau)=\sum\limits_{i=1}^{N+1}L_{i}(\tau)U(\tau_{i})=\sum \limits_{i=1}^{N+1}L_{i}(\tau)U_{i}. \tag{18}\]  
Furthermore, the state equation can be replaced by the following constraint conditions  
\[\sum\limits_{i=1}^{N+1}D_{k,i}X_{i}=\frac{t_{f}-t_{0}}{2}f(X_{k},U_{k},\tau_{ k}). \tag{19}\]  
In the first stage, the state variables are discretized using Equation (17) for LG allocation points, and the control variables are discretized using pairwise Gaussian allocation points over the time interval [-1, 1], that is  
\[U_{2l-1}=U_{2l},l=1,2,\cdots,\frac{N+1}{2}. \tag{23}\]  
Among them, \(N\) is an odd number. Thus, the number of variable allocation points are reduced to \((N+1)/2\), and the approximation formula is  
\[u(\tau)\approx U(\tau)=\sum_{i=1}^{N+1}L_{i}(\tau)U(\tau_{i})=\sum_{l=1}^{N+1 }\sum_{i=1}^{2l}L_{i}(\tau)U_{l}. \tag{24}\]  
After substituting the control variables obtained from the allocation points into Equation (21), it can be analyzed that the number of optimized parameters is reduced to \((\tau_{u}/2+\tau_{x})\times N\). In the second stage, based on the solution in the first stage, the control variable allocation is reconstructed. Firstly, define the slope change of the \(r\)th control variable at the collocation point \(\tau_{2l+1}\) as  
\[S_{r,l+1}=\frac{U_{l+1}^{r}-U_{l}^{r}}{\tau_{2l+1}-\tau_{2l-1}},l=1,2,\cdots, \frac{N+1}{2}-1. \tag{25}\]  
Among them \(U_{l+1}^{r}\) and \(U_{l}^{r}\), respectively, represent the values of the \(r\)th control variable at the collocation points \(\tau_{2l+1}\) and \(\tau_{2l-1}\). Set the upper and lower threshold parameters for slope changes as \(\epsilon_{\text{U}}\) and \(\epsilon_{L}\), respectively, and then perform segmentation refinement and point merging judgments separately.