PID tuning with Neural Network
17 次查看(过去 30 天)
显示 更早的评论
Hi, this code is to tune PID parameters with Neural Network. Something is wrong with this because when I change the plant, PID parameters will not change and reman same as before.
Can anyone please help what is wrong with this code?
%% close loop responce
clc
clear all
close all
%%
% Input signal
h=0.002;
% h = 1
t=0:h:100;
t=t';
for k=1:length(t)
r(k,1)=1;
rp(k,1)=4;
end
Kp=0.3;
Ki=0.2;
Kd=0.01;
%%
N=10; % hidden neuron
dim=1;
eta=0.28; % learning rate
alfa=0.04; % momentom
W_IH=0.05*rand(N,dim);
W_HO=0.05*rand(3,N);
dW_HO(:,:,2)=zeros(3,N);
dW_IH(:,:,2)=zeros(N,dim);
%%
% IAQ model
% Initializing y(k)
y(1,1)=0;
y(2,1)=0;
ey(1,1)=r(1)-y(1);
ey(2,1)=r(2)-y(2);
u(1,1)=0;
u(2,1)=0;
for k=3:length(t)
a(k,1)=1.4*(1-0.9*exp(-0.3*k));
y(k,1)=a(k)/(1+y(k-1)^2)+u(k-1)+0.2*u(k-2); %plant
ey(k)=r(k)-y(k);
eyc(k,1)=ey(k)-ey(k-1);
u(k)=u(k-1)+Kp*(ey(k)-ey(k-1))+Ki*(ey(k))+Kd*(ey(k)-2*ey(k-1)+ey(k-2)); %control signal
if t(k-1)==0.8
u(k,1)=u(k,1)+0.02;
elseif t(k-1)==1.6
u(k,1)=u(k,1)+0.01;
end
x=[ey(k)];
O1=x; %output of layer one
in2=W_IH*O1; %input of hidden layer
O2=tanh(in2); %output of hidden layer
in3=W_HO*O2; %input of layer3
O3=0.5*(1+tanh(in3));
%% learning algorithm of the weight update in output layer
for i=1:N
for l=1:3
g=0.5+0.5*tanh(in3(l));
gp=g*(1-g);
if l==1
delta3(l)=ey(k)*1.2*(ey(k)-ey(k-1))*gp;
elseif l==2
delta3(l)=ey(k)*1.2*(ey(k))*gp;
elseif l==3
delta3(l)=ey(k)*1.2*(ey(k)-2*ey(k-1)+ey(k-2))*gp;
end
dW_HO(l,i,k)=alfa* dW_HO(l,i,k-1)+eta*delta3(l)*O2(i);
W_HO_new(l,i)=W_HO(l,i)+dW_HO(l,i,k);
end
end
%% learning algorithm of the weight update in hidden layer
for i=1:N
for j=1:dim
f=tanh(in2(i));
fp=0.5-0.5*f*f;
for l=1:3
S(l)=delta3(l)*W_HO(l,i);
end
delta2(i)=fp*sum(S);
dW_IH(i,j,k)=alfa*dW_IH(i,j,k-1)+eta*delta2(i)*O1(j);
W_IH(i,j)=W_IH(i,j)+dW_IH(i,j,k);
end
end
W_HO=W_HO_new;
in2=W_IH*O1;
O2=tanh(in2);
in3=W_HO*O2;
O3new=0.5*(1+tanh(in3))./[3.33;3.33;20];
K(:,k)=O3new;
Kp=O3new(1);
Ki=O3new(2);
Kd=O3new(3);
K(1,k)=Kp;
K(2,k)=Ki;
K(3,k)=Kd;
% k= k+1;
end
%% plot
plot(t,y,'r','LineWidth',1.5)
axis([-0.05 100 -0 1.1])
xlabel('time(s)')
ylabel('y(k)')
title('Closed loop responce of system with PID controller')
legend('y(k)')
figure
plot(t,u,'b','LineWidth',1.5)
axis([-0.05 100 -0 0.3])
xlabel('time(s)')
ylabel('u(k)')
title('Closed loop responce of system with PID controller')
legend('u(k)')
figure
subplot(3,1,1)
plot(t,K(1,:),'r','LineWidth',1.5)
axis([-0.05 100 0.05 0.2])
xlabel('time(s)')
ylabel('Kp');
set(gca,'yTick',0.1:0.05:0.2)
subplot(3,1,2)
plot(t,K(2,:),'y','LineWidth',1.5)
axis([-0.05 100 0.05 0.2])
xlabel('time(s)')
ylabel('Ki')
set(gca,'yTick',0.05:0.05:0.15)
subplot(3,1,3)
plot(t,K(3,:),'g','LineWidth',1.5)
axis([-0.05 100 0.005 0.03])
xlabel('time(s)')
ylabel('Kd')
set(gca,'yTick',0.005:0.005:0.025)
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 PID Controller Tuning 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!