求助:BP神经网络的程序,调试不出来
ts=20;sys=tf(,,'inputdelay',80); %线性模型离散化
dsys=c2d(sys,ts,'zoh');
=tfdata(dsys,'v')
xite=0.25;
alfa=0.05;
S=1; %Signal type
IN=3;H=6;Out=1;%NN Structure
if S==1%Step Signal
wi=[-0.6394 -0.2696 -0.3756;
-0.8603 -0.2013 -0.5024;
-1.0749 0.5543 -1.6820;
-0.3625 -0.0724 -0.6463;
0.1425 0.0279 -0.5406;
-0.7023 -0.2596 -0.5437];
%wi=0.50*rands(H,IN);
wi_1=wi;wi_2=wi;wi_3=wi;
wo=;
%wo=0.50*rands(Out,H);
wo_1=wo;wo_2=wo;wo_3=wo;
end
if S==2%Sine Signal
wi=[-0.2846 0.2193 -0.5097;
-0.7484 -0.1210 -0.4708;
-0.7176 0.8297 -1.6000;
-0.0858 0.1925 -0.6346;
0.4358 0.2369 -0.4564;
-1.0668 0.0988 0.2049];
%wi=0.50*rands(H,IN);
wi_1=wi;wi_2=wi;wi_3=wi;
wo=;
%wo=0.50*rands(Out,H);
wo_1=wo;wo_2=wo;wo_3=wo;
end
x=;
% z=;
u_1=0;u_2=0;u_3=0;u_4=0;u_5=0;
y_2=0;y_1=0;
a_1=0;
Oh_1=0;
% xi_1=zeros(1,IN);
Oh=zeros(H,1); %Output from NN middle layer
I=Oh; %Input to NN middle layer
% Oh_1=zeros(H,1);
error_3=0;
error_2=0;
error_1=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
yout(k)=-den(2)*y_1+num(2)*u_5; % 线性模型
error(k)=rin(k)-yout(k);
xi=;
x(1)=error(k)-error_1;
x(2)=error(k);
x(3)=error(k)-2*error_1+error_2;
epid=;
I=xi*wi';
for j=1:1:H
Oh(j)=1/(1+exp(-I(j)));%Middle Layer'output
end
K=wo*Oh; %Output Layer
a(k)=1./(1+exp(-K));
kp(k)=100/(a(k)+40);
ki(k)=100;
kd(k)=20;
Kpid=;
du(k)=Kpid*epid;
u(k)=u_1+du(k);
delta3=error(k)*((80/(a_1+40)^2)*error_1-(80/(a_1+40)^2)*error_2)*a_1*(1-a_1);
for i=1:1:H
d_wo=xite*delta3*Oh(i);
end
end
wo=wo_1+d_wo;
% %Hidden layer
for i=1:1:H
dO(i)=exp(-I(i))/(1+exp(-I(i))); %求&'(O(i))
end
segma=delta3*wo_1;
for i=1:1:H
delta2(i)=dO(i)*segma(i);
end
d_wi=xite*delta2*I';
wi=wi_1+d_wi;
%Parameters Update
u_5=u_4;u_4=u_3;u_3=u_2;u_2=u_1;u_1=u(k);
y_2=y_1;y_1=yout(k);
a_1=a(k);
Oh_1=Oh;
wo_3=wo_2;
wo_2=wo_1;
wo_1=wo;
wi_3=wi_2;
wi_2=wi_1;
wi_1=wi;
error_3=error_2;
error_2=error_1;
error_1=error(k);
end
figure(1);
plot(time,rin,'r',time,yout,'b');
xlabel('time(s)');ylabel('rin,yout');
[ 本帖最后由 xinyuxf 于 2007-7-12 14:51 编辑 ] %基于BP神经网络的PID控制
clear all;
close all;
xite=0.25;
alfa=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.5024 -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);
wi_1=wi;wi_2=wi;wi_3=wi;
wo=[0.7576 0.2616 0.5820 -0.1416 -0.1325;
-0.1146 0.2949 0.83520.22050.4508;
0.7201 0.4566 0.76720.49620.3632];
%wo=0.50*rands(Out,H);
wo_1=wo;wo_2=wo;wo_3=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);
wi_1=wi;wi_2=wi;wi_3=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);
wo_1=wo;wo_2=wo;wo_3=wo;
end
x=;
u_1=0;u_2=0;u_3=0;u_4=0;u_5=0;
y_1=0;y_2=0;y_3=0;
Oh=zeros(H,1); %神经网络中间层输出
I=Oh; %神经网络中间层输入
error_2=0;
error_1=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));
yout(k)=a(k)*y_1/(1+y_1^2)+u_1;
error(k)=rin(k)-yout(k);
xi=;
x(1)=error(k)-error_1;
x(2)=error(k);
x(3)=error(k)-2*error_1+error_2;
epid=;
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 l=1:1:Out
K(l)=exp(K(l))/(exp(K(l))+exp(-K(l))); %求kp,ti,td
end
kp(k)=K(1);ti(k)=K(2);td(k)=K(3);
Kpid=;
du(k)=Kpid*epid;
u(k)=u_1+du(k);
if u(k)>=10 % 限制控制器输出
u(k)=10;
end
if u(k)<=-10
u(k)=-10;
end
dyu(k)=sign((yout(k)-y_1)/(u(k)-u_1+0.0000001));
%输出层
for j=1:1:Out
dK(j)=2/(exp(K(j))+exp(-K(j)))^2;
end
for l=1:1:Out
delta3(l)=error(k)*dyu(k)*epid(l)*dK(l);
end
for l=1:1:Out
for i=1:1:H
d_wo=xite*delta3(l)*Oh(i)+alfa*(wo_1-wo_2);
end
end
wo=wo_1+d_wo+alfa*(wo_1-wo_2);
%隐含层
for i=1:1:H
dO(i)=4/(exp(I(i))+exp(-I(i)))^2;
end
segma=delta3*wo;
for i=1:1:H
delta2(i)=dO(i)*segma(i);
end
d_wi=xite*delta2'*xi;
wi=wi_1+d_wi+alfa*(wi_1-wi_2);
%参数更新
u_5=u_4;u_4=u_3;u_3=u_2;u_2=u_1;u_1=u(k);
y_2=y_1;y_1=yout(k);
wo_3=wo_2;
wo_2=wo_1;
wo_1=wo;
wi_3=wi_2;
wi_2=wi_1;
wi_1=wi;
error_2=error_1;
error_1=error(k);
end
figure(1);
plot(time,rin,'r',time,yout,'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');
这个程序好运行的,和你那个应该差不多
你自己对照着看看吧
页:
[1]