logo资料库

BP神经网络整定的PID算法_matlab源程序.docx

第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
资料共4页,全文预览结束
%BP based PID Control clear all; close all; xite=0.28; alfa=0.001; % 学习速率 %惯性系数 IN=4;H=5;Out=3; %NN Structure(构造,神经网络结构) wi=0.50*rands(H,IN); wi_1=wi;wi_2=wi;wi_3=wi; wo=0.50*rands(Out,H); wo_1=wo;wo_2=wo;wo_3=wo;%构成变量 %Output from NN middle layer %Input to NN middle layer Oh=zeros(H,1); I=Oh; error_2=0; error_1=0; %把传递函数离散化(零阶保持器法离散化) ts=0.01; sys=tf(2.6126,[1,3.201,2.7225]); %建立被控对象传递函数(LTI Viewer 对象模型 sys=tf(num,den) 将由传递函数模型所描述系统封装成对应的系统对象模型。 dsys=c2d(sys,ts,'z'); [num,den]=tfdata(dsys,'v'); %离散化后提取分子、分母(提取每项的常数) for k=1:1:2000 time(k)=k*ts; rin(k)= 40; yout(k)=-den(2)*y_1-den(3)*y_2+num(2)*u_2+num(3)*u_3; error(k)=rin(k)-yout(k); %频率参数,构成一维数组 xi=[rin(k),yout(k),error(k),1]; x(1)=error(k)-error_1; x(2)=error(k); %计算 P %计算 I
x(3)=error(k)-2*error_1+error_2; %计算 D epid=[x(1);x(2);x(3)]; I=xi*wi'; for j=1:1:H %the output of the input layer , and 1*5 Oh(j)=(exp(I(j))-exp(-I(j)))/(exp(I(j))+exp(-I(j))); %Middle layer's output end K=wo*Oh; for l=1:1:Out %Output Layer(the input of output layer) 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); if u(k)>=45 u(k)=45; end if u(k)<=-45 u(k)=-45; end % the increment(增加) of the output "u" % the output of the value of controlling % Restricting(限制)the output of controller dyu(k)=sign((yout(k)-y_1)/(u(k)-u_1+0.0000001)); %当 x<0 时,sign(x)=-1 当 x=0 时,sign(x)=0; 当 x>0 时,sign(x)=1。dyu(k)表示什么 %Output layer for j=1:1:Out dK(j)=2/(exp(K(j))+exp(-K(j)))^2; %the value of g'() 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); %wo 更新 %Hidden (隐藏)layer for i=1:1:H dO(i)=4/(exp(I(i))+exp(-I(i)))^2; %the value of the f'() end
segma=delta3*wo; for i=1:1:H delta2(i)=dO(i)*segma(i); end % the sum(总和) %求δ d_wi=xite*delta2'*xi; wi=wi_1+d_wi+alfa*(wi_1-wi_2);% 不就是 wi 修改更新的过程吗?个人认为:如果被控对象 的表达式:'yout'改变的话,权值修改的中间过程都不要改变,因为不管系统的输出是什么, bp 神经网络权值修改的公式都是一样的啊。有可能的话,可以把权值的初始值改一下,不 是必须的,反正神经网络会自己调整权值,只要不陷入局部极小值就可以了(对不同的系统 可以采取修改权值 学习效率和惯性系数来调整神经网络的控制效果) %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); 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'); 本来按照原来的传递函数是有图形的,但是换了传递函数就出现这个样子 是不是就像老师说的可以调节数轴。
分享到:
收藏