标题:求助:有关神经网络PID控制的一个小程序改动!急急!!!!
只看楼主
lilijing
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-5-28
 问题点数:0 回复次数:0 
求助:有关神经网络PID控制的一个小程序改动!急急!!!!
请帮我把这两个程序的被控对象,也就是差分方程改成一个,我改过,一改就出错,要毕业了,帮帮我好吗?这两个程序是有关神经网络PID控制算法的
程序代码:
clear all;
close all;

ts=0.001;
sys=tf(5.235e005,[1,87.35,1.047e004,0]);
dsys=c2d(sys,ts,'z');
[num,den]=tfdata(dsys,'v');

u_1=0.0;u_2=0.0;u_3=0.0;
y_1=0.0;y_2=0.0;y_3=0.0;
x=[0,0,0]';
error_1=0;
for k=1:1:1500
time(k)=k*ts;
   
S=1;
if S==1
    kp=0.50;ki=0.001;kd=0.001;          
    rin(k)=1;                            %Step Signal
elseif S==2
    kp=0.50;ki=0.001;kd=0.001;          
    rin(k)=sign(sin(2*2*pi*k*ts));  %Square Wave Signal
elseif S==3
    kp=1.5;ki=1.0;kd=0.01;            %Sine Signal
    rin(k)=0.5*sin(2*2*pi*k*ts);           
end

u(k)=kp*x(1)+kd*x(2)+ki*x(3);   %PID Controller
%Restricting the output of controller
if u(k)>=10       
   u(k)=10;
end
if u(k)<=-10
   u(k)=-10;
end
%Linear model
yout(k)=-den(2)*y_1-den(3)*y_2-den(4)*y_3+num(2)*u_1+num(3)*u_2+num(4)*u_3;

error(k)=rin(k)-yout(k);

%Return of parameters
u_3=u_2;u_2=u_1;u_1=u(k);
y_3=y_2;y_2=y_1;y_1=yout(k);
   
x(1)=error(k);                 %Calculating P
x(2)=(error(k)-error_1)/ts;  %Calculating D
x(3)=x(3)+error(k)*ts;        %Calculating I

error_1=error(k);
end
figure(1);
plot(time,rin,'k',time,yout,'k');
xlabel('time(s)'),ylabel('rin,yout');
第二个程序
程序代码:
%BP based PID Control
clear all;
close all;

xite=0.20;
alfa=0.05;

S=2; %Signal type

IN=4;H=5;Out=3;  %NN Structure
if S==1  %Step Signal
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.8352  0.2205  0.4508;
    0.7201 0.4566 0.7672  0.4962  0.3632];
%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   -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=[0,0,0];
du_1=0;
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);    %Output from NN middle layer
I=Oh;             %Input to NN middle layer
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

%Unlinear model
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=[rin(k),yout(k),error(k),1];

x(1)=error(k)-error_1;
x(2)=error(k);
x(3)=error(k)-2*error_1+error_2;

epid=[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))); %Middle Layer
end
K=wo*Oh;             %Output Layer
for l=1:1:Out
    K(l)=exp(K(l))/(exp(K(l))+exp(-K(l)));        %Getting kp,ki,kd
end
kp(k)=K(1);ki(k)=K(2);kd(k)=K(3);
Kpid=[kp(k),ki(k),kd(k)];

du(k)=Kpid*epid;
u(k)=u_1+du(k);

dyu(k)=sign((yout(k)-y_1)/(du(k)-du_1+0.0001));

%Output layer
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);
%Hidden layer
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);

%Parameters Update
du_1=du(k);
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('time(s)');ylabel('rin,yout');
figure(2);
plot(time,error,'r');
xlabel('time(s)');ylabel('error');
figure(3);
plot(time,u,'r');
xlabel('time(s)');ylabel('u');
figure(4);
subplot(311);
plot(time,kp,'r');
xlabel('time(s)');ylabel('kp');
subplot(312);
plot(time,ki,'g');
xlabel('time(s)');ylabel('ki');
subplot(313);
plot(time,kd,'b');
xlabel('time(s)');ylabel('kd');
搜索更多相关主题的帖子: 神经网络 PID控制 sys rin 
2008-05-28 10:27



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-216276-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.754836 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved