aminit;

%Design of PI controller by Loop shaping
K=[];TI=[];PHM=[];WGC=[];P='1./(s+1).^4';str='b-.';
for wgc=0.1:0.1:0.5,
s=i*wgc;pgc=eval(P);
Ti=0.5/wgc;k=wgc*Ti/sqrt(1+wgc^2*Ti^2)/abs(pgc);K=[K,k];TI=[TI,Ti];
C='k.*(1+1./(s*Ti))';
w=logspace(-2,1,100);s=i*w;one=ones(size(w));
pv=eval(P);cv=eval(C);L=pv.*cv;
[xx,kk]=min(abs(abs(L)-1));WGC=[WGC,xx];
phm=180+180*angle(L(kk))/pi;PHM=[PHM,phm];
%Bode plot
subplot(2,1,1)
loglog(w,abs(L),str,w,abs(pv),'r--',w,one,'b-');hold on
axis([0.01 2 0.1 100]);ylabel('g');
subplot(2,1,2)
semilogx(w,180*unwrap(angle(L))/pi,str,w,180*unwrap(angle(pv))/pi,'r--');hold on;
axis([0.01 2 -200 0]);xlabel('w');ylabel('p');
%Nyquist plot
%subplot(2,2,1)
%pl=plot(real(L),imag(L),str);hold on
%set(pl,'Linewidth',1.2);
%axis([-1.5 0.5 -1.5 0.5]);axis square;
str='b-';
end
%Bode plot
subplot(2,1,1);loglog(w,one,'b-')
subplot(2,1,2);semilogx(w,-180*one,'b-');
%Nyquist plot
%fi=-pi:pi/100:pi;xl=-2:0.1:2;null=zeros(size(xl));
%plot(cos(fi),sin(fi),'r-',xl,null,'b-',null,xl,'b-');


amprint('procctrl-pibode.eps');
