%Example 2.8, Loop-shiping design for Plant (2.54)
%Produce Figure 2.21 and Table 2.2
%

%Plant (2.54):  G(s)=200/(10s+1)(0.005s+1)^2,  Gd(s)=100/(10s+1)
Eq2_54

% Controllers for disturbance rejection, designed by loop-shaping

wc0=10;
c0 = nd2sys(conv([10 1],[0.1,1]),conv([200 1.e-3],[0.01,1]),wc0);
c1 = 0.5;
c2 = nd2sys([1 2],[1 1.e-4],0.5); 
c3 = mmult(c2,nd2sys([0.05 1],[0.005 1]));
omega=logspace(-2,2,41);

%Table 1.2
disp(sprintf('%6s%6s%6s%6s%6s%6s%11s%13s','','','','','','','Reference','Disturbance'));
disp(sprintf('%4s%6s%6s%6s%6s%6s%6s%6s%7s%7s','','GM','PM','Wc','Ms','Mt','tr','Ov','Ymax','Y(3)'));

% 0. Inverse -based controller.
c=c0;
sysanaly;
disp(sprintf('%4s%7.2f%6.1f%6.1f%6.2f%6.2f%6.2f%6.2f%6.2f%7.2f',...
    'K0',Gm,Pm,Wc,Ms,Mt,tr,Ov,Ymax,Y3));

% 1. P controller
c = c1;
sysanaly;
l_f1=frsp(l,omega);
y1=yd;
disp(sprintf('%4s%7.2f%6.1f%6.2f%6.2f%6.2f%6.2f%6.2f%6.2f%7.2f',...
    'K1',Gm,Pm,Wc,Ms,Mt,tr,Ov,Ymax,Y3));

% 2. PI controller
c = c2;
sysanaly;
l_f2=abs(frsp(l,omega));
y2=yd;
disp(sprintf('%4s%7.2f%6.1f%6.2f%6.2f%6.2f%6.2f%6.2f%6.2f%7.3f',...
    'K2',Gm,Pm,Wc,Ms,Mt,tr,Ov,Ymax,Y3));

% 3. PID controller 
c = c3;
sysanaly;
l_f3=abs(frsp(l,omega));
y3=yd;
disp(sprintf('%4s%7.1f%6.1f%6.2f%6.2f%6.2f%6.2f%6.2f%6.2f%7.3f',...
    'K3',Gm,Pm,Wc,Ms,Mt,tr,Ov,Ymax,Y3));

%Figure 2.21
clf;
subplot(221),vplot('liv,lm',l_f3,l_f1,l_f2,1,'w:')
axis([.01,100,.01,10000]);
text(0.02,30,'L1');text(0.18,1000,'L2, L3');
text(50,0.15,'L3');text(15,0.05,'L1, L2');
title('Loop gains');
subplot(222),vplot(y3,y1,y2,0,'w:')
axis([0,3,-0.2,1.5]);
xlabel('Time');ylabel('y');
text(0.3,0.2,'y2');text(0.53,0.5,'y3');text(1.5,0.85,'y1');
title('Disturbance responses');


