clear;
x = [0,0,0]';
%初始化输入向量
kP = 0.40;
kI = 0.35;
kD = 0.4;
%初始化PID的三个参数的学习速率
last_w_x1 = 0.1;
last_w_x2 = 0.1;
last_w_x3 = 0.1;
%初始化输入网络的三个权重系数
error_1 = 0;
%记录上一次误差以及上上次误差
y_1 = 0;
y_2 = 0;
y_3 = 0;
%被控量的离散序列,用于计算被控对象方程以及计算误差
u_1 = 0;
u_2 = 0;
u_3 = 0;
%控制输出的离散序列,用于计算被控对象方程以及更新神经元权重系数。
ts = 0.001;
%设置迭代周期为1ms
learning_mode = 4;
%设置学习规则
error_integral = 0;
%积分记录
for k = 1:1:1000
time(k)=k*ts;
%横轴变化
yd(k) = sin(50*2*pi*k*ts);%0.5*sign(sin(2*2*pi*k*ts));
%输入期望的信号函数-阶跃函数
y(k)=0.368*sin(y_1) + 0.26*y_2 + 4*sin(u_1) + 0.632*u_2;
%被控对象的离散方程。 x' = Ax + Bu
error(k) = yd(k) - y(k);
%计算当前误差
%神经元的输入
if learning_mode == 1
%无监督的HEBBE学习规则
tmp_x1 = last_w_x1 + kP*u_1*x(1);
tmp_x2 = last_w_x2 + kI*u_1*x(2);
tmp_x3 = last_w_x3 + kD*u_1*x(3);
w_x1(k) = exp(tmp_x1)/(exp(tmp_x1)+(1/exp(tmp_x1)));
w_x2(k) = exp(tmp_x2)/(exp(tmp_x2)+(1/exp(tmp_x2)));
w_x3(k) = exp(tmp_x3)/(exp(tmp_x3)+(1/exp(tmp_x3)));
K = 0.06;
%无监督的HEBBE学习规则
elseif learning_mode == 2
%有监督的delta学习规则
tmp_x1 = last_w_x1 + kP*error(k)*x(1);
tmp_x2 = last_w_x2 + kI*error(k)*x(2);
tmp_x3 = last_w_x3 + kD*error(k)*x(3);
w_x1(k) = exp(tmp_x1)/(exp(tmp_x1)+(1/exp(tmp_x1)));
w_x2(k) = exp(tmp_x2)/(exp(tmp_x2)+(1/exp(tmp_x2)));
w_x3(k) = exp(tmp_x3)/(exp(tmp_x3)+(1/exp(tmp_x3)));
K = 0.06;
%有监督的delta学习规则
elseif learning_mode == 3
%有监督的hebbe学习规则
tmp_x1 = last_w_x1 + kP*error(k)*u_1*x(1);
tmp_x2 = last_w_x2 + kI*error(k)*u_2*x(2);
tmp_x3 = last_w_x3 + kD*error(k)*u_3*x(3);
w_x1(k) = exp(tmp_x1)/(exp(tmp_x1)+(1/exp(tmp_x1)));
w_x2(k) = exp(tmp_x2)/(exp(tmp_x2)+(1/exp(tmp_x2)));
w_x3(k) = exp(tmp_x3)/(exp(tmp_x3)+(1/exp(tmp_x3)));
K = 0.12;
%有监督的hebbe学习规则
elseif learning_mode == 4
%改进后的有监督的hebbe学习规则
tmp_x1 = last_w_x1 + kP*error(k)*u_1*(error(k) - error_1);
tmp_x2 = last_w_x2 + kI*error(k)*u_2*(error(k) - error_1);
tmp_x3 = last_w_x3 + kD*error(k)*u_3*(error(k) - error_1);
w_x1(k) = exp(tmp_x1)/(exp(tmp_x1)+(1/exp(tmp_x1)));
w_x2(k) = exp(tmp_x2)/(exp(tmp_x2)+(1/exp(tmp_x2)));
w_x3(k) = exp(tmp_x3)/(exp(tmp_x3)+(1/exp(tmp_x3)));
K = 0.12;
%改进后的有监督的hebbe学习规则
end
wadd(k)=abs(w_x1(k))+abs(w_x2(k))+abs(w_x3(k));
w11(k) = w_x1(k)/wadd(k);
w22(k) = w_x2(k)/wadd(k);
w33(k) = w_x3(k)/wadd(k);
w=[w_x1(k),w_x2(k),w_x3(k)];
%权重系数归一化操作
x(1) = error(k); %比例
x(2) = error_integral; %积分
x(3) = error(k)-error_1; %微分
u(k)=K*w*x;
%控制律
error_1 = error(k);
%更新误差序列
error_integral = error_integral + error(k);
%更新误差积分
u_3 = u_2;
u_2 = u_1;
u_1 = u(k);
%更新神经元输出控制律序列
y_3=y_2;
y_2=y_1;
y_1=y(k);
%更新被控对象输出序列
last_w_x1=w_x1(k);
last_w_x2=w_x2(k);
last_w_x3=w_x3(k);
%更新权重系数
end
figure(1);
plot(time,yd,'r',time,y,'k:','linewidth',2);
xlabel('time');ylabel('yd;y');
title('track')
%打印被控对象跟踪结果
figure(2);
plot(time,u,'r','linewidth',2);
xlabel('time');ylabel('controlinput U');
title('control law')
%打印控制律曲线
figure(3);
plot(time,w11,'r',time,w_x1,'b','linewidth',2);
xlabel('time');ylabel('kp');
title('kp line')
%打印控制律曲线
figure(4);
plot(time,w22,'r',time,w_x2,'b','linewidth',2);
xlabel('time');ylabel('ki');
title('ki line')
%打印控制律曲线
figure(5);
plot(time,w33,'r',time,w_x3,'b','linewidth',2);
xlabel('time');ylabel('kd');
title('kd line')
%打印控制律曲线