function [Gm,Pm,Ms,Mt]=marginss(numg,deng,Kc,taui,taud)
% This is the function marginss.m
% Simple routine made for computing stability margins for PID cascade controller using minbode
% Usage: [Gm,Pm,Ms,Mt]=marginss(numg,deng,Kc,taui,taud)
% Alternative (with delay): marginsminbode(numg,deng,Kc,taui,taud)

   num=conv(numg,conv([Kc Kc/taui],[taud 1])); 
   den=conv(deng,conv([1 0],[0.0001*taud 1])); 
    
gctf = tf(num,den); gc = ss(gctf);   

[A,B,C,D]=ssdata(gc); 
[Gm,Pm,W180,Wc]=margin(A,B,C,D); 
gcmu=pck(A,B,C,D);
smu=minv(madd(1,gcmu)); tmu = msub(1,smu);
hinfs=hinfnorm(smu); hinft=hinfnorm(tmu);
Ms=(hinfs(1)+hinfs(2))/2;
Mt=(hinft(1)+hinft(2))/2;
marginss=[Gm,Pm,Ms,Mt] 