%基于BP神经网络的PID控制
clear all;
x1=0.25;a1=0.05;
S=1; %信号类型
In=4;h=5;Out=3; %神经网络结构
if S==1, %阶跃信号
wi=[-0.6394 -0.2696 -0.3756 -0.7023;
-0.8603 -0.2013 -0.05024 -0.2596;
-1.0749 0.5543 -1.6820 -0.5437;
-0.3625 -0.0724 -0.6463 -0.2859;
0.1425 0.0279 -0.5406 -0.7660];
%wi=0.50*rands(h,In);
wi1=wi;wi2=wi;wi3=wi;
wo=[0.7576 0.2616 0.5820 -0.1416 -0.1325;
-0.1146 0.2949 0.8352 0.2205 0.4508;
0.7201 0.4566 0.7672 0.4962 0.3632];
%wo=0.50*rands(Out,h);
wo1=wo;wo2=wo;wo3=wo;
end
if S==2 %正弦信号
wi=[-0.2846 0.2193 -0.5097 -1.0668;
-0.7484 -0.1210 -0.4708 0.0988;
-0.7176 0.8297 -1.6000 0.2049;
-0.0858 0.1925 -0.6346 0.0347;
0.4358 0.2369 -0.4564 -0.1324];
%wi=0.50*rands(h,In);
wi1=wi;wi2=wi;wi3=wi;
wo=[1.0438 0.5478 0.8682 0.1446 0.1537;
0.1716 0.5811 1.1214 0.5067 0.7370;
1.0063 0.7428 1.0534 0.7824 0.6494];
%wo=0.50*rands(Out,h);
wo1=wo;wo2=wo;wo3=wo;
end
x=[0,0,0];
u1=0;u2=0;u3=0;u4=0;u5=0;
y1=0;y2=0;y3=0;
oh=zeros(h,1); %神经网络中间层输出
I=oh;%神经网络中间层输入
error1=0;
error2=0;
ts=0.001;
for k=1:1:6000,
time(k)=k*ts;
if S==1,
rin(k)=1.0;
elseif S==2,
rin(k)=sin(1*2*pi*k*ts);
end
%非线性模型
a(k)=1.2*(1-0.8*exp(-0.1*k));
y(k)=a(k)*y1/(1+y1^2)+u1;
error(k)=rin(k)-y(k);
xi=[rin(k),y(k),error(k),1];
x(1)=error(k)-error1;
x(2)=error(k);
x(3)=error(k)-2*error1+error2;
ed=[x(1);x(2);x(3)];
I=xi*wi';
for j=1:1:h
oh(j)=(exp(I(j))-exp(-I(j)))/(exp(I(j))+exp(-I(j))); %中间层
end
K=wo*oh; %输出层
for j=1:1:Out
K(j)=exp(K(j))/(exp(K(j))+exp(-K(j))); %求kp,ti,td
end
kp(k)=K(1);ti(k)=K(2);td(k)=K(3);
Kd=[kp(k),ti(k),td(k)];
du(k)=Kd*ed;
u(k)=u1+du(k);
if u(k)>=10, %限制控制器输出
u(k)=10;
end
if u(k)<=-10,
u(k)=-10;
end
dyu(k)=sign((y(k)-y1)/(u(k)-u1+0.0000001));
%输出层
for j=1:1:Out,
dK(j)=2/(exp(K(j))+exp(-K(j)))^2;
end
for i=1:1:Out,
de3(i)=error(k)*dyu(k)*ed(i)*dK(i);
end
for j=1:1:Out,
for i=1:1:h,
dwo=x1*de3(j)*oh(i)+a1*(wo1-wo2);
end
end
wo=wo1+dwo+a1*(wo1-wo2);
%隐含层
for i=1:1:h,
dO(i)=4/(exp(I(i))+exp(-I(i)))^2;
end
seg=de3*wo;
for i=1:1:h
de2(i)=dO(i)*seg(i);
end
dwi=x1*de2'*xi;
wi=wi1+dwi+a1*(wi1-wi2);
%参数更新
u5=u4;u4=u3;u3=u2;u2=u1;u1=u(k);
y2=y1;y1=y(k);
wo3=wo2;wo2=wo1;wo1=wo;
wi3=wi2;wi2=wi1;wi1=wi;
error2=error1;
error1=error(k);
end
figure(1);
plot(time,rin,'r',time,y,'b');
xlabel('时间(秒)');ylabel('系统输入、输出');
figure(2);
plot(time,error,'r:');
xlabel('时间(秒)');ylabel('误差');
figure(3);
subplot(311);
plot(time,kp,'r');
xlabel('时间(秒)');ylabel('参数kp');
subplot(312);
plot(time,ti,'g--');
xlabel('时间(秒)');ylabel('参数ti');
subplot(313);
plot(time,td,'b*');
xlabel('时间(秒)');ylabel('参数td');
神经网络PID控制,不过为什么有newff,train这样的语句,还望高手指点一下。相互交流一下哈。