# Sensorless HSPMSM Control of an Improved SMC and EKF Based on Immune PSO
## 2 Methodology
### Estimation and Optimization of the EKF Rotor Angular Velocity and Rotor Position
#### 2.2.2 Optimizing the EKF Noise Covariance Matrix Using Immune-Based PSO  
(a) Elementary PSO:  
PSO is a new type of evolutionary algorithm (EA) for solving optimization problems. Random particles search and follow the current optimal particle in the solution space and find the optimal solution through repeated iterations. In each iteration, the particles update themselves by tracking the individual extremum \(p_{best}\) and the global extremum \(g_{best}\), and the particles always follow these two extremes to change their position and velocity until they find the optimal solution. PSO includes simpler rules than the genetic algorithm because it does not have the \"crossover\" and \"variation\" operations of the genetic algorithm, and it evaluates solution quality by the fitness of the solution [35]. When the two optimal values are found, the particle updates its velocity and position according to the following equation:  
\[\left\{ \begin{array}{l}v_{id}=c_{2}r_{2}(p_{gd}-x_{id})+c_{1}r_{1}(p_{id}-x _{id})+\omega v_{id}\\ x_{id}=v_{id}+x_{id}\end{array} \right. \tag{7}\]  
where \(\omega\) is the inertia weight, \(r_{2}\) and \(r_{1}\) are uniform random numbers in the range of [0, 1], \(c_{2}\) and \(c_{1}\) are learning factors, and \(V_{id}\) and \(X_{id}\) are the velocity and displacement numbers of the particles, respectively. (b) PSO based on adaptive weights of immunity:  
Adaptive weights refer to the fact that \(\omega\) changes dynamically depending on the position of the particle [35], with the inertia weight being related to the distance of the particle from the global optimum. Thus, the inertia weight of each different particle increases not only with the number of iterations but also with the distance from the global optimum, as follows:  
\[\omega= \begin{cases}\omega_{\text{min}}-\frac{(f-f_{\text{min}})*(\omega_{ \text{max}}-\omega_{\text{min}})}{f_{avg}-f_{\text{min}}},&f\leq f_{avg}\\ \omega_{\text{max}},&f>f_{avg}\end{cases}  \tag{8}\]  
where \(f,f_{min}\), and \(f_{avg}\) denote the real-time objective function value of particles, the minimum objective value, and the average value of all particles, respectively. \(\omega\) changes with \(f\). When \(f\) is consistent, \(\omega\) increases; when \(f\) is dispersed, \(\omega\) decreases. Immune-based PSO is used to update the antibody population on the basis of the immune algorithm, which can address the slow convergence of the immune algorithm. The steps of the immune-based hybrid PSO algorithm are as follows [35]. (1) Determine \(c_{2}\) and \(c_{1}\) (the learning factors) and \(M\) (the number of antibody populations). (2) Map the initial antibody population \(P_{0}\) by logistic regression analysis to produce \(M\) antibodies of \(x_{i}\) and \(v_{i}\) where \(i\) = 1, 2, \(\ldots\), \(M\). (3) Produce immune antibodies. Calculate the adaptation value of the antibody in the current \(P\). Determine whether the algorithm meets the end condition and output the result if yes; otherwise, continue to run. (4) Update \(p_{\text{best}}\) and \(g_{\text{best}}\), and update \(v_{i,j}\) and \(x_{i,j}\) according to the following equations:  
\[\left\{ \begin{array}{l}v_{i,j}(t+1)=c_{2}r_{2}[p_{g,j}-x_{i,j}(t)]+c_{1}r_{ 1}[p_{i,j}-x_{i,j}(t)]+\omega\cdot v_{i,j}(t)\\ x_{i,j}(t+1)=v_{i,j}(t+1)+x_{i,j}(t),j=1,2,\ldots,D\end{array} \right. \tag{9}\]  
(5) Generate \(N\) new antibodies by logistic mapping. (6) Select concentration-based antibodies. The probability of producing \(N+M\) new antibodies is calculated using the percentage of similar antibodies in the population. \(N\) antibodies are selected according to their probability of forming antibody cluster \(P\). Then, return to step (3). (c) PSO based on adaptive weights of immunity optimization of the EKF covariance matrix by a novel particle swarm:  
The selection of system matrices \(Q\) and \(R\) is crucial for the convergence and stability of the EKF system. The noise covariance optimization adjusts the parameters within the \(Q\) and \(R\) matrices in each simulation cycle.