%% Quantifying Unmodeled Dynamics and Robust Controller Design
%% Create the plant

bw = ureal('bw',5,'Percentage',10);
H = tf(1,[1/bw 1]);

Gnom = H;
W = makeweight(.05,9,10);
Delta = ultidyn('Delta',[1 1]);
G = Gnom*(1+W*Delta)

%
%% Plot frequency response of plant

bode(G,{1e-1 1e2})

%
%% Design two controllers

xi = 0.707;
wn = 3;
K1 = tf([(2*xi*wn/5-1) wn*wn/5],[1 0]);
wn = 7.5;
K2 = tf([(2*xi*wn/5-1) wn*wn/5],[1 0]);

%
%% Compute closed-loop systems

T1 = feedback(G*K1,1);
T2 = feedback(G*K2,1);

%
%% Plot closed-loop responses

tfinal = 3;
stepplot(T1,'b',T2,'r',tfinal)

%
%% Check robust stability

opt = robOptions('Display','on');
stabmarg1 = robstab(T1,opt);

%

stabmarg2 = robstab(T2,opt);

%
%% Examine worst-case sensitivity

S1 = feedback(1,G*K1);
S2 = feedback(1,G*K2);

[maxgain1,wcu1] = wcgain(S1);
maxgain1

[maxgain2,wcu2] = wcgain(S2);
maxgain2

%
%% Compare nominal and worst-case behavior

bodemag(S1.NominalValue,'b',usubs(S1,wcu1),'b');
hold on
bodemag(S2.NominalValue,'r',usubs(S2,wcu2),'r');
hold off

%
% Copyright 2012 The MathWorks, Inc.