# Design and Optimization of a Neuro-Fuzzy System for the Control of an Electromechanical Plant
## 4 Controller Parameter Training  
The structure of the closed-loop neuro-fuzzy controller can be assimilated as a recurrent neural network; according to [36], there are different alternatives to perform the training process. One way is to calculate the respective recurrent equation that updates each parameter using the respective fitness function derivatives. In this way, the system simulation will be carried out with the current parameters without modification; in the same simulation the adjustment of parameters is carried out in auxiliary variables, since these parameters are not used in the current simulation. When finishing the simulation, the system parameters are updated with those stored in the auxiliary variables and a new simulation is carried out until the value of the fitness function is reduced.  
To carry out the controller training, first the structure of the plant and the controller are established; thus, for a closed-loop system, the control action corresponds to:  
\[u[n]=f_{c}(e[n],e[n-1],u[n-1],\mathbf{H}_{c}) \tag{26}\]  
Likewise, the system's output employed for controller training is:  
\[\theta[n]=f_{p}\big{(}u[n],u[n-1],\theta[n-1],\theta[n-2],\mathbf{H}_{p}\big{)} \tag{27}\]  
where \(\mathbf{H}_{c}\) corresponds to the set of controller parameters and \(\mathbf{H}_{p}\) to the plant parameters. Considering \(h_{c}\) as one of the control system parameters, the adaptation of the controller parameters is carried out using Equation (28).  
\[h_{c}(k+1)=h_{c}(k)-\eta\frac{dJ}{dh_{c}} \tag{28}\]  
In Equation (28), parameter \(\eta\) is the learning rate and \(J\) corresponds to the adjustment function defined as:  
\[J=\frac{1}{2}\Big{[}Q(e[n])^{2}+R(u[n])^{2}\Big{]} \tag{29}\]  
where \(e[n]=r[n]-\theta[n]\) and the derivative of \(J\) depending on the adjustment parameters is given by Equation (30). Considering these equations, Figure 13 shows the algorithm employed for controller training.  
\[\frac{dJ}{dh_{c}}=Qe[n]\frac{de}{dh_{c}}+Ru[n]\frac{du}{dh_{c}} \tag{30}\]  
The first step consists of choosing the initial controller's parameter configuration; then, the next step involves the system output calculation using the model of the plant. Subsequently, the parameter's adjustment occurs employing the respective equations involved in the dynamics in the control system and the derivatives of the parameters (equations \(de/dh_{c}\), \(du/dh_{c}\) and subsequent). It is noteworthy that during the simulation the adjusted parameters are stored in auxiliary variables since during this process these values are not used by the controller. To continue the simulation and parameter adjustment, the algorithm returns to the step where the control system is evaluated with \(n=n+1\), repeating this process until simulation time \(N_{T}\) is complete. Once the simulation time is completed, the parameters of the controller are updated with the optimized values to return to the evaluation step of the control system to start a new iteration \(k=k+1\) until the objective function \(I_{T}(k)=(1/N_{T})\sum_{n=1}^{N_{T}}I(k,n)\) is less than a given \(\epsilon\) or until \(k\) is equal to a given number \(K_{T}\).