%PI controller example
%Z-N tuning rule
%Z-N and C-C tuning rules
%step test
%test process one--first order plus delay model
delay=20; 
num=0.5;
%test process one
den1=[30 1];
den2=conv([30 1],[30 1]);
den=conv(den1,den2);
%%%%%%


d=36;
tauM=(164-d);
Kss=0.5;
%Z-N tuning rule (PI)
Kc=0.9*tauM/(Kss*d);
tauI=3*d;
%modify 
Kc=0.5*Kc;
w=logspace(-3,3,1500);
Go=freqs(num,den,w);
numc=Kc*[tauI 1];
denc=[tauI 0];
j=sqrt(-1);
C=freqs(numc,denc,w);
Ny_w=Go.*C;
Ny_w1=Ny_w.*exp(-j*w*delay);
%C-C tuning rule (PI)
Kc=tauM/(Kss*d)*(0.9+d/(12*tauM));
Kc=0.5*Kc;
tauI=d*(30*tauM+3*d)/(9*tauM+20*d);
numc=Kc*[tauI 1];
denc=[tauI 0];
C=freqs(numc,denc,w);
Ny_w=Go.*C;
Ny_w2=Ny_w.*exp(-j*w*delay);
%Wang and Cluett tuning rule (PI)
L=tauM/d;
Kc_hat=0.13+0.51*L;
tauI_hat=(0.25+0.96*L)/(0.93+0.03*L);
Kc=Kc_hat/Kss;
tauI=d*tauI_hat;
numc=Kc*[tauI 1];
denc=[tauI 0];
C=freqs(numc,denc,w);
Ny_w=Go.*C;
Ny_w3=Ny_w.*exp(-j*w*delay);
figure(1)
plot(real(Ny_w1),imag(Ny_w1),'g')
hold on
plot(real(Ny_w2),imag(Ny_w2),'r')
plot(real(Ny_w3),imag(Ny_w3),'b')
grid on
legend('1','2','3')
 axis([-4,4,-4,4])
 plot(circle(0,1),'k--')
  set(gca,'FontSize',14,'FontName','helvetica'); 
 xlabel('Real')
 ylabel('Imag')
 
 figure(3)
 plot(w, abs(Ny_w1),'g')
 hold on
 plot(w, abs(Ny_w2),'r')
 plot(w, abs(Ny_w3),'b')
 plot(w,ones(1,length(w)),'-.')
 legend('1','2','3')
 set(gca,'FontSize',14,'FontName','helvetica'); 
 xlabel('Frequency (rad/sec)')
 ylabel('Mag')
 ylim([0 2])
 xlim([0 0.1])