function [sys,x0,str,ts,simStateCompliance] = BPsjpid10(t,x,u,flag,T,nh,xite,alfa)switch flag,case 0,[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes(T,nh);%初始化函数case 3,sys=mdlOutputs(t,x,u,nh,xite,alfa);%输出函数case {1,2,4,9},sys=[];otherwiseDAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));endfunction [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes(T,nh)%调用初始画函数,两个外部输入参数 参数T确定采样时间,参数nh确定隐含层层数sizes = simsizes;sizes.NumContStates = 0;sizes.NumDiscStates = 0;sizes.NumOutputs = 4+6*nh;%定义输出变量,包括控制变量u,隐含层+输出层所有加权系数sizes.NumInputs = 7+12*nh;%定义输入变量,包括前7个参数[e(k);e(k-1);e(k-2);y(k);y(k-1);r(k);u(k-1)]%隐含层+输出层权值系数(k-2),隐含层+输出层权值系数(k-1)sizes.DirFeedthrough = 1;sizes.NumSampleTimes = 1;sys = simsizes(sizes);x0 = [];str = [];ts = [T 0];simStateCompliance = 'UnknownSimState';function sys=mdlOutputs(t,x,u,nh,xite,alfa)%调用输出函数global wi_2 wi_1 wo_2 wo_1%wi_2 = reshape(u(8:7+3*nh),nh,3);%wi_2 =rand(nh,3).*2-1;wi_2 = [0.6085 0.7615 -0.1291-0.7173 -0.4271 -0.79990.1132 0.7454 -0.1284-0.6874 -0.3522 0.23370.4793 -0.1192 -0.1563-0.3471 0.4021 0.5249-0.1770 -0.1310 0.2309-0.8013 -0.1833 -0.4459];%隐含层(k-2)权值系数矩阵,维数nh*3%wo_2 = reshape(u(8+3*nh:7+6*nh),3,nh);%wo_2 = rand(3,nh);wo_2 = [ 0.0102 0.2024 0.6434 0.3661 0.5577 0.6807 0.0495 0.09240.8304 0.8168 0.3359 0.7246 0.9865 0.7437 0.4536 0.26100.0045 0.3463 0.2680 0.3935 0.4218 0.0696 0.8964 0.5358];%输出层(k-2)权值系数矩阵,维数3*nh%wi_1 = reshape(u(8+6*nh:7+9*nh),nh,3);%wi_1 =wi_2;wi_1 = [ 0.6326 -0.5180 0.32270.0886 -0.3733 0.0104-0.3241 0.5663 -0.61420.6098 0.9529 -0.7881-0.7164 -0.9048 -0.31400.3888 -0.0376 0.7160-0.3820 0.7210 0.07060.8466 0.0902 0.7309];%隐含层(k-1)权值系数矩阵,维数nh*3%wo_1 = reshape(u(8+9*nh:7+12*nh),3,nh);%wo_1 = wo_2;wo_1 = [0.4857 0.5758 0.0107 0.5840 0.7934 0.4377 0.5828 0.06320.9401 0.6855 0.9250 0.8067 0.7609 0.9478 0.1634 0.62070.5881 0.3521 0.4033 0.0247 0.5051 0.3393 0.3937 0.1264];%输出层(k-1)权值系数矩阵,维数3*nhxi = [u(6),u(4),u(1)];%神经网络的输入xi=[u(6),u(4),u(1)]=[r(k),y(k),e(k)]xx = [u(1)-u(2);u(1);u(1)+u(3)-2*u(2)];%xx=[u(1)-u(2);u(1);u(1)+u(3)-2*u(2)]=[e(k)-e(k-1);e(k);e(k)+e(k-2)-2*e(k-1)]I = xi*wi_1';%计算隐含层的输入,I=神经网络的输入*隐含层权值系数矩阵的转置wi_1',结果为:%I=[net0(k),net1(k)...netnh(k)]为1*nh矩阵%Oh = (exp(I)-exp(-I))./(exp(I)+exp(-I));Oh = exp(I)./(exp(I)+exp(-I));%激活函数,可更改%计算隐含层的输出,(exp(I)-exp(-I))./(exp(I)+exp(-I))为隐含层的激活函数Sigmoid%Oh=[o0(k),o1(k)...onh(k)],为1*nh的矩阵O = wo_1*Oh';%计算输出层的输入,维数3*1M=[300;0.8;0.8];K = exp(O)./(exp(O)+exp(-O));K(1)=M(1)*K(1);K(2)=M(2)*K(2);K(3)=M(3)*K(3);%激活函数,可更改%计算输出层的输出K=[Kp,Ki,Kd],维数为1*3%exp(Oh)./(exp(Oh)+exp(-Oh))为输出层的激活函数Sigmoiduu = u(7)+K'*xx;%根据增量式PID控制算法计算控制变量u(k)dyu = sign((u(4)-u(5))/(uu-u(7)+0.0000001));%计算输出层加权系数修正公式的sgn%sign((y(k)-y(k-1))/(u(k)-u(k-1)+0.0000001)近似代表偏导%dK = 2./(exp(O)+exp(-O)).^2;dK = 2./(exp(K)+exp(-K)).^2;%激活函数,可更改delta3 = u(1)*dyu*xx.*dK;wo = zeros(3,8);wo = wo_1+xite*delta3*Oh+alfa*(wo_1-wo_2);%输出层加权系数矩阵的修正dOh = 2./(exp(I)+exp(-I)).^2;%激活函数,可更改wi = zeros(8,3);wi = wi_1+xite*(dOh.*(delta3'*wo))'*xi+alfa*(wi_1-wi_2);%隐含层加权系数修正wo_1 = wo;wi_1 = wi;sys = [uu;K(:);wi(:);wo(:)];%输出层输出sys=[uu;K(:);wi(:);wo(:)]=%[uu;Kp;Ki;Kd;隐含层+输出层所有权值系数]%K(:),wi(:),wo(:),把这三个矩阵按顺序排为列向量
参考:基于BP神经网络PID控制+Simulink仿真_小师兄1995的博客-CSDN博客_基于神经网络的pid控制
BP神经网络整定PID_小羊学习记录的博客-CSDN博客_bp神经网络pid