**2.4 Programming of neural network PID controller**

Here is a programming example of neural network PID controller simulation, the simulation results are shown in **Figures 14**–**18**, write the MATLAB program as follows:

% Calculation error error = [r1 (k) -y1 (k); r2 (k) -y2 (k); r3 (k) -y3 (k)]; error1 (k) = error (1); error2 (k) = error (2); error3 (k) = error (3); J (k) = 0.5 \* (error (1) ^ 2 + error (2) ^ 2 + error (3) ^ 2);% adjust size ypc = [y1 (k) -y\_1 (1); y2 (k) -y\_1 (2); y3 (k) -y\_1 (3)]; uhc = [u\_1 (1) -u\_2 (1); u\_1 (2) -u\_2 (2); u\_1 (3) -u\_2 (3)];

#### **Figure 14.**

*Simulation result of test loss comparison of train and validation.*

**Figure 15.** *Simulation result of test accuracy comparison of train and validation.*

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

**Figure 16.** *Simulation result of test error comparison of train and validation.*

#### **Figure 18.**

*Change curve of Kp, Ki, Kd parameters.*

% Hidden layer and output layer weight adjustment % Adjust w21 Sig1 = sign (ypc ./ (uhc (1) +0.00001));

```
dw21 = sum (error. * Sig1) * qo ';
   w21 = w21 + rate2 * dw21;
   % Adjust w22
   Sig2 = sign (ypc ./ (uh (2) +0.00001));
   dw22 = sum (error. * Sig2) * qo ';
   w22 = w22 + rate2 * dw22;
   % Adjust w23
   Sig3 = sign (ypc ./ (uh (3) +0.00001));
   dw23 = sum (error. * Sig3) * qo ';
   w23 = w23 + rate2 * dw23;
   % Input layer and hidden layer weight adjustment
   delta2 = zeros (3,3);
   wshi = [w21; w22; w23];
   for t = 1: 1: 3
     delta2 (1: 3, t) = error (1: 3). * sign (ypc (1: 3) ./ (uhc (t) +0.00000001));
   end
   for j = 1: 1: 3
     sgn (j) = sign ((h1i (j) -h1i_1 (j)) / (x1i (j) -x1i_1 (j) +0.00001));
   end
s1 = sgn '* [r1 (k), y1 (k)];
   wshi2_1 = wshi (1: 3,1: 3);
   alter = zeros (3,1);
   dws1 = zeros (3,2);
   for j = 1: 1: 3
      for p = 1: 1: 3
         alter (j) = alter (j) + delta2 (p,:) * wshi2_1 (:, j);
      end
   end
   for p = 1: 1: 3
      dws1 (p,:) = alter (p) * s1 (p, :);
   end
   w11 = w11 + rate1 * dws1;
   % Adjust w12
   for j = 1: 1: 3
      sgn (j) = sign ((h2i (j) -h2i_1 (j)) / (x2i (j) -x2i_1 (j) +0.0000001))
   end
   s2 = sgn '* [r2 (k), y2 (k)];
   wshi2_2 = wshi (:, 4: 6);
   alter2 = zeros (3,1);
   dws2 = zeros (3,2);
   for j = 1: 1: 3
   for p = 1: 1: 3
   alter2 (j) = alter2 (j) + delta2 (p,:) * wshi2_2 (:, j);
   end
   end
   for p = 1: 1: 3
   dws2 (p,:) = alter2 (p) * s2 (p, :);
   end
   w12 = w12 + rate1 * dws2;
   % Adjust w13
   for j = 1: 1: 3
   sgn (j) = sign ((h3i (j) -h3i_1 (j)) / (x3i (j) -x3i_1 (j) +0.0000001));
   end
```
*Overview of Some Intelligent Control Structures and Dedicated Algorithms DOI: http://dx.doi.org/10.5772/intechopen.91966*

```
s3 = sgn '* [r3 (k), y3 (k)];
wshi2_3 = wshi (:, 7: 9);
alter3 = zeros (3,1);
dws3 = zeros (3,2);
for j = 1: 1: 3
for p = 1: 1: 3
alter3 (j) = (alter3 (j) + delta2 (p,:) * wshi2_3 (:, j));
end
end
for p = 1: 1: 3
dws3 (p,:) = alter2 (p) * s3 (p, :);
end
w13 = w13 + rate1 * dws3;
% Parameter update
u_3 = u_2; u_2 = u_1; u_1 = uh;
y_2 = y_1; y_1 = yn;
h1i_1 = h1i; h2i_1 = h2i; h3i_1 = h3i;
x1i_1 = x1i; x2i_1 = x2i; x3i_1 = x3i;
end
time = 0.001 * (1: k);
figure (1)
subplot (3,1,1)
plot (time, r1, 'r-', time, y1, 'b-');
title ('PID neural network control');
ylabel ('Controlled amount 1');
legend ('control the target', 'actual output', 'fontsize', 12);
subplot (3,1,2)
plot (time, r2, 'r-', time, y2, 'b-');
ylabel ('Controlled amount 2');
legend ('control the target', 'actual output', 'fontsize', 12);
axis ([0,0.2,0,1])
subplot (3,1,3)
plot (time, r3, 'r-', time, y3, 'b-');
xlabel ('time / s');
ylabel ('Controlled amount 3');
legend ('control the target', 'actual output', 'fontsize', 12);
print -dtiff -r600
figure (3)
plot (time, u1, 'r-', time, u2, 'g-', time, u3, 'b');
title ('PID control input provided by the neural network to the object');
xlabel ('time'), ylabel ('control law');
legend ('u1', 'u2', 'u3'); grid
figure (4)
plot (time, J, 'r-');
axis ([0,0.1,0,0.5]); grid
title ('network learning objective function J dynamic curve');
xlabel ('time'); ylabel ('control error');
% BPy1 = y1;
% BPy2 = y2;
% BPy3 = y3;
% BPu1 = u1;
% BPu2 = u2;
% BPu3 = u3;
```
% BPJ = J % save BP r1 r2 r3 BPy1 BPy2 BPy3 BPu1 BPu2 BPu3 BPJ
