MATLAB实现神经网络之单神经元自适应控制
系统对象 y(k) = 0.368y(k-1)+0.26y(k-2)+0.1u(k-1)+0.632u(k-2)
系统输入 rin(k)=0.5sgn(sin(4πt))
clc
clear
%定义输入矩阵
x = [0,0,0]';
%定义学习速率
xite1 = 0.4;
xite2 = 0.3;
xite3 = 0.5;
%初始化权重
wk1_1 = 0.1;
wk2_1 = 0.1;
wk3_1 = 0.1;
%初始化误差
erro_1 = 0;
erro_2 = 0;
%初始化被控对象模型
y_1 = 0;y_2 = 0;y_3 = 0;
u_1 = 0;u_2 = 0;u_3 = 0;
%定义时间步长
ts = 0.001;
%迭代学习
for k=1:1:1000 %循环迭代次数
time(k) = k*ts; %时间为K*ts总时间也就是1
yd(k) = 0.5*sign(sin(4*pi*time(k))); %目标期望值
y(k) = 0.368*y_1+0.26*y_2+0.1*u_1+0.632*u_2; %被控对象动态模型模型
erro(k) = yd(k)-y(k); %获取误差
%更新权重
wk1(k) = wk1_1+xite1*erro(k)*u_1*x(1);
wk2(k) = wk2_1+xite2*erro(k)*u_1*x(2);
wk3(k) = wk3_1+xite3*erro(k)*u_1*x(3);
%定义比例系数
K = 0.12;
%给输入矩阵赋值
x(1) = erro(k)-erro_1;
x(2) = erro(k);
x(3) = erro(k)-2*erro_1+erro_2;
%将权重求和
wadd(k) = abs(wk1(k))+abs(wk2(k))+abs(wk3(k));
w11(k) = wk1(k)/wadd(k); %得w11
w22(k) = wk2(k)/wadd(k); %w22
w33(k) = wk3(k)/wadd(k); %w33
w = [w11(k),w22(k),w33(k)];
%增量
u(k) = u_1 + K*w*x;
erro_2 = erro_1; %e(k-2)
erro_1 = erro(k);%e(k-1)
u_3 = u_2;u_2 = u_1;u_1 = u(k);
y_3 = y_2;y_2 = y_1;y_1 = y(k);
wk1_1 = wk1(k); %w1(k-1)
wk3_1 = wk3(k);
wk2_1 = wk2(k);
end
%---------------结果可视化-------------------%
%显示被控模型曲线和设定曲线
figure(1);
plot(time,yd,'r',time,y,'k','linewidth',2);
xlabel('time(s)');ylabel('yd,y');
legend('ideal position','position tracking');
title('被控模型曲线和设定曲线')
%显示误差
figure(2);
plot(time,erro,'r','linewidth',2);
xlabel('time(s)');ylabel('erro(t)');
title('误差曲线')
%显示控制u(t)输出曲线
figure(3);
plot(time,u,'r','linewidth',2);
xlabel('time(s)');ylabel('u(t)');
title('控制量u(t)输出曲线')
%显示w参数调整曲线
figure(4);
plot(time,w11,'g',time,w22,'r',time,w33,'b','linewidth',2);
xlabel('time(s)');ylabel('w(t)');
legend('w11(t)','w22(t)','w33(t)');
title('w11,w22,w33参数调整曲线')
|