# Neural PD Controller for an Unmanned Aerial Vehicle Trained with Extended Kalman Filter
## 4 MLP trained with the EKF  
The architecture of an ANN was inspired by biological neurons. Like synaptic connection in a regular biological neuron, each node in an ANN receives a signal from an adjacent node and its output is computed by some nonlinear function of the sum of the inputs. These connections between adjacent neurons have weights that adjusts as the network learns [19]. The training process of the MLP can be seen as an estimation problem for a nonlinear system that can be solved with the EKF [54; 55; 56]. Neural networks trained with the EKF have demonstrated faster learning speeds and convergence times than networks trained with algorithms based on backpropagation, which is ideal for real time applications [57; 58]. The objective is to find the optimal weights that minimize the prediction error [59]. Consider an MLP with \(L\) weights and \(m\) output nodes. The neural network can be modeled as follows  
\[\mathbf{w}(k+1)=\mathbf{w}(k) \tag{7}\]  
\[\hat{\mathbf{y}}\left(k\right)=h\left(\mathbf{w}\left(k\right),\mathbf{u}\left(k\right)\right) \tag{8}\]  
Figure 4: \(U_{2}\), \(U_{3}\) and \(U_{4}\) are inputs for the rotational subsystem; \(U_{1}\), roll, pitch and yaw are inputs for the following translation subsystem. where \(\mathbf{w}(k)\) is the state vector, \(\mathbf{u}(k)\) is the input vector, \(\hat{\mathbf{y}}\) is the output vector and \(h\) is the nonlinear output function. From Kalman Filter equations, it is known that  
\[\mathbf{K}\left(k\right)=\mathbf{P}\left(k\right)\mathbf{H}^{T} \left(k\right)\left[\mathbf{R}\left(k\right)+\mathbf{H}\left(k\right)\mathbf{P }\left(k\right)\mathbf{H}^{T}\left(k\right)\right]^{-1} \tag{9}\] \[\mathbf{w}\left(k+1\right)=\mathbf{w}\left(k\right)+\eta\mathbf{K}\left(k \right)\left[\mathbf{y}\left(k\right)-\hat{\mathbf{y}}\left(k\right)\right]\] (10) \[\mathbf{P}\left(k\right)=\mathbf{P}\left(k\right)-\mathbf{K} \left(k\right)\mathbf{H}\left(k\right)\mathbf{P}\left(k\right)+\mathbf{Q} \left(k\right) \tag{11}\]  
where \(L\) is the total number of weights, \(\eta\) is the learning rate, \(\mathbf{P}(k)\in\Re^{L\times L}\) and \(\mathbf{P}(k+1)\in\Re^{L\times L}\) are the prediction error covariance matrices in \(k\) and \(k+1\). \(\mathbf{K}(k)\in\Re^{L\times m}\) is the Kalman gain matrix. \(\mathbf{y}\in\Re^{m}\) is the system output, and \(\hat{\mathbf{y}}\) is the network output. \(\mathbf{Q}\in\Re^{L\times L}\) is the process noise covariance matrix, and \(\mathbf{R}\in\Re^{m\times m}\) represents the measurement covariance error. \(\mathbf{w}\in\Re^{L}\) is the weights (state) vector, and \(\mathbf{H}\in\Re^{m\times L}\) contains the partial derivatives of each output of the neural network \(\hat{\mathbf{y}}\) with respect to each weight \(w_{j}\) and it is defined as  
\[\mathbf{H}_{ij}\left(k\right)=\left[\frac{\partial\hat{y}_{i}\left(k\right)}{ \partial w_{j}\left(k\right)}\right]_{w\left(k\right)=\hat{w}\left(k+1\right) },i=1\ldots m,j=1\ldots L \tag{12}\]  
Consider the MLP in Figure 5 with one hidden layer and one node at the output layer, where \(p\) denotes the number of inputs to the network and \(h\) the number of nodes in the hidden layer. The output of the neural network is defined by  
\[\sigma_{i} = \frac{1}{1+e^{-n_{i}}}\quad i=1\ldots h \tag{13}\] \[n_{i} = \sum_{j=0}^{p}w_{ij}^{(1)}x_{j}\quad x_{0}=+1\] (14) \[v_{1} = \sum_{k=0}^{h}w_{1k}^{(2)}u_{k}\quad u_{0}=+1\] (15) \[\hat{\mathbf{y}}\left(k\right)=h\left(\mathbf{w}\left(k\right),\mathbf{u}\left(k\right)\right) \tag{8}\]0  
\[\hat{\mathbf{y}}\left(k\right)=h\left(\mathbf{w}\left(k\right),\mathbf{u}\left(k\right)\right) \tag{8}\]1 Multilayer Perceptron architecture. The networks has \(p\) inputs and \(h\) nodes in the hidden layer. the weights from the input layer to the hidden layer are denoted by \(w_{ij}^{(1)}\) while the weights from the hidden layer to the output layer are described by \(w_{ij}^{(2)}\)  
Finally, vector \(\mathbf{H}\) can be expressed as  
\[\mathbf{H}=\frac{\partial\hat{q}}{\partial w}=\left[