%System analysis for loop-shaping examples
%It is assumed that the plant is G=g, disturbance TFM is Gd=gd and 
%controller is K=c.
%It produce the system matrices, L=l, S=s, T=t,
%step response y=Tr, and step disturbance response, yd=SGdr.
%It also gives:Mt,Ms,Gm,Pm,Wc,W180,tr:y(tr)=0.9,Ov=max(y),
%Ymax=max(yd) and Y3=yd(3).
%
l = mmult(g,c);
s = minv(madd(1,l));
t=msub(1,s); 
time=0:0.01:3;
[A,B,C,D]=unpck(mmult(s,gd));yd=step(A,B,C,D,1,time);
Ymax=max(yd);Y3=yd(length(yd));yd=vpck(yd,time);
[A,B,C,D]=unpck(t);[y,x,time]=step(A,B,C,D);k=length(y);
Ov=max([y;1]);[x,x,x,tr]=margin(y(2:k)/0.9,y(2:k),time(2:k));
mt=hinfnorm(t);Mt=mt(1);
ms=hinfnorm(s);Ms=ms(1);
[A,B,C,D]=unpck(l);[Gm,Pm,W180,Wc]=margin(A,B,C,D);

