%PID_PFC
%compare  PID/PFC
clear 
close
tf= 900;
w=1:1:tf;
u=zeros(1,tf);
mvpfc=u;mvpid=u;
spid =u;spfc=u;
sm=u;intg=u;
tech=1;
tau= 17; am=exp(-tech/tau); bm=1-am;
trbf= 8;% desired closed loop time response
lh=1-exp(-tech*3/trbf);%only tuning parameter !
r=50;%delay
k=1;%gain
pert=0;
for ii= r+2:1:tf;
   if ii>500;pert=20;end
   spfc(ii)=spfc(ii-1)*am+bm*(mvpfc(ii-1-r)*k+pert);%process
   sm(ii)=sm(ii-1)*am+bm*mvpfc(ii-1)*k;%model
   spred= spfc(ii)+sm(ii)-sm(ii-r);
   d= (100-spred)*lh+sm(ii)*bm;
   mvpfc(ii)=d/(k*bm);%action
   if mvpfc(ii)>120; mvpfc(ii)=120;end
end
figure(1)
plot(w,spfc,'r',w,mvpfc,'b')
grid
zoom
axis([0 tf 0 130])
title   ( 'PFC  MV b CV  r  constraint MV =120 ')
pert=0;
for ii= r+2:1:tf;
   spid(ii)=spid(ii-1)*am+bm*mvpid(ii-1-r)*k;
   spidn(ii)=spid(ii);
   er= 100-spidn(ii);
   intg(ii)=intg(ii-1)+er;
   mvpid (ii)=0.45*er+0.017*intg(ii)+8*(spidn(ii)-spidn(ii-1));
  if mvpid(ii)>120; mvpid(ii)=120;end
end
figure(2)
plot(w,spid,'r',w,mvpid,'b')
grid
zoom
axis([0 tf 0 130])
title   ( 'PI  MV b CV  r  constraint MV =120  ')