*1.3.3 Simulation and programming of adaptive fuzzy control*

Adaptive fuzzy control simulation takes the controlled object as Eq. (3)*.*

$$
\ddot{x} = -2\mathbf{\hat{x}} + \mathbf{133u} \tag{3}
$$

Position instruction is sin(πt) take the membership function in Eqs. (4)–(9).

$$\mu\_{\text{N3}}(\mathfrak{x}\_i) = \frac{\mathbf{1}}{(\mathbf{1} + \exp\left(\mathfrak{F}(\mathfrak{x} + \mathfrak{Z})\right))} \tag{4}$$

$$
\mu\_{N2}(\mathbf{x}\_i) = \exp\left(\left(\mathbf{x} + \mathbf{1.5}\right)^2\right) \tag{5}
$$

$$\mu\_{\rm N1}(\mathbf{x}\_i) = \exp\left[\left(\left(\mathbf{x}\_i + \mathbf{0.5}\right)\right)^2\right] \tag{6}$$

$$\mu\_{p1}(\mathbf{x}\_i) = \exp\left[-\left(\left(\mathbf{x}\_i - \mathbf{0.5}\right)\right)^2\right] \tag{7}$$

$$\mu\_{p2}(\mathbf{x}\_i) = \exp\left[-\left((\mathbf{x}\_i - \mathbf{1.5})\right)^2\right] \tag{8}$$

$$
\mu\_{p3}(\mathbf{x}\_i) = \frac{1}{\left(\mathbf{1} + \exp\left(-\mathbf{5}(\mathbf{x} - \mathbf{2})\right)\right)}\tag{9}
$$

The initial state matrix of the system is ½ � **1**, **0** , and the initial values of each element in θ are all taken as 0. Control rule show in Eq. (10) and adaptive rule (11) are adopted. Then take *<sup>Q</sup>* <sup>¼</sup> **50 0 0 50** � �, *<sup>k</sup>***<sup>1</sup>** <sup>¼</sup> **<sup>1</sup>**, *<sup>k</sup>***<sup>2</sup>** <sup>¼</sup> **<sup>10</sup>**, and adaptive parameter γ = 50.

$$\mathfrak{u}\_{\mathbf{D}} = (\mathfrak{x}|\boldsymbol{\Theta}) = \frac{\sum\_{l\_1=1}^{m\_1} \dots \sum\_{l\_n=1}^{m\_n} \overline{\mathcal{Y}}\_{\boldsymbol{u}}^{l\_1 \dots l\_n} \left(\prod\_{i=1}^n \mu\_{Ai}^{l\_i}(\boldsymbol{x}\_i)\right)}{\sum\_{l\_1=1}^{m\_1} \dots \sum\_{l\_n=1}^{m\_n} \left(\prod\_{i=1}^n \mu\_{Ai}^{l\_i}(\boldsymbol{x}\_i)\right)} \tag{10}$$

$$\dot{\mathbf{V}} = -\frac{1}{2}\mathbf{e}^T \mathbf{Q} \mathbf{e} + \frac{\mathbf{b}}{\gamma} (\boldsymbol{\theta}^\* - \boldsymbol{\theta})^T \left[\gamma \mathbf{e}^T \mathbf{P}\_n \boldsymbol{\xi}(\mathbf{x}) - \dot{\boldsymbol{\theta}}\right] - \mathbf{e}^T \mathbf{p}\_n \mathbf{b} \mathbf{w} \tag{11}$$

According to the membership function, write the MATLAB program as follows:

```
% Adaptive fuzzy approximation
clc % Clear screen
clear all; % Remove workplace variables
close all; % Close the display graphics window
L1 = -3;
L2 = 3;
L = L2-L1; % Fuzzy set change range length
T = 0.001;
x = L1: T: L2; % Range of fuzzy set
figure (1);
for i = 1: 1: 6
   if i = 1
       u = 1 ./ (1 + exp (5 * (x + 2)));
   else if i = 6
       u = 1 ./ (1 + exp (-5 * (x-2)));
   else
   u = exp (-(x + 2.5- (i-1)). ^ 2);
end
   hold on;
   plot (x, u, 'r', 'Line Width', 2);
end
```
*Overview of Some Intelligent Control Structures and Dedicated Algorithms DOI: http://dx.doi.org/10.5772/intechopen.91966*

```
x label ('x'); y label ('Membership function fuzzy set');
   grid on
   axis tight
   The running program is shown in Figure 9 as a membership function graph.
   Directly and adaptively control the internal control objects. The MATLAB
program is written as follows. Results show in Figures 10–12.
   % S-function for continuous state equation for controlled object
   function [sys, x0, str, ts] = s_function (t, x, u, flag)
   switch flag,
   % Initialization
       case 0,
         [sys, x0, str, ts] = mdlInitializeSizes; % Initialization function
       case 1,
         sys = mdlDerivatives (t, x, u);% Differential function
   % Outputs
       case 3,
         sys = mdlOutputs (t, x, u);% Output function
   % Unhandled flags
       case {2, 4, 9}
         sys = [];
   % Unexpected flags
       otherwise
         error (['Unhandled flag =', num2str (flag)]);
   end
   function [sys, x0, str, ts] = mdlInitializeSizes
   sizes = simsizes;
   sizes.NumContStates = 2;
   sizes.NumDiscStates = 0;
   sizes.NumOutputs = 2; % 2 outputs
   sizes.NumInputs = 1; %1 input
   sizes.DirFeedthrough = 0;
   sizes.NumSampleTimes = 0;
   sys=simsizes(sizes);
   x0 = [1 0];
```
**Figure 9.** *Membership function graph.*

#### **Figure 10.**

*Simulation result of position tracking.*

**Figure 11.**

*Simulation result of position tracking error.*

**Figure 12.** *Simulation result of control input.*

*Overview of Some Intelligent Control Structures and Dedicated Algorithms DOI: http://dx.doi.org/10.5772/intechopen.91966*

```
str = [];
ts=[];
function sys = mdlDerivatives (t, x, u)
% Second-order system
sys (1) = x (2);
sys (2) =-25 * x (2) + 133 * u;
function sys = mdlOutputs (t, x, u)
sys (1) = x (1);
sys (2) = x (2);
% Drawing program writing
close all;
figure (1);
plot (t, y (:, 1), 'r', t, y (:, 2), 'b', 'LineWidth', 2);
xlabel ('time (s)'); ylabel ('Position tracking');
grid on
title ('Location tracking')
figure (2);
plot (t, y (:, 1) -y (:, 2), 'r', 'LineWidth', 2);
xlabel ('time (s)'); ylabel ('Position tracking error');
grid on
title ('Position tracking error')
figure (3);
plot (t, u (:, 1), 'r', 'LineWidth', 2);
xlabel('time(s)');ylabel('Control input');
grid on
title ('Control input signal')
```