%% Worst-Case Performance of Closed-Loop Uncertain System
% To examine the worst-case performance of an uncertain control system, create a plant with nominal model of an integrator, and include additive unmodeled dynamics uncertainty of a level of 0.4 (this corresponds to 100% model uncertainty at 2.5 rad/s). Then design a proportional controller K1 that puts the nominal closed-loop bandwidth at 0.8 rad/s. Roll off K1 at a frequency 25 times the nominal closed-loop bandwidth. Repeat the design for a controller K2 that puts the nominal closed-loop bandwidth at 2.0 rad/s. In each case, form the closed-loop sensitivity function.

P = tf(1,[1 0]) + ultidyn('delta',[1 1],'bound',0.4); 
BW1 = 0.8; 
K1 = tf(BW1,[1/(25*BW1) 1]); 
S1 = feedback(1,P*K1); 
BW2 = 2.0; 
K2 = tf(BW2,[1/(25*BW2) 1]); 
S2 = feedback(1,P*K2);

%
% Assess the worst-case gain of the closed-loop sensitivity functions.

[maxgain1,wcunc1] = wcgain(S1); 
[maxgain2,wcunc2] = wcgain(S2); 

maxgain1, maxgain2

%
% The maxgain variables indicate that controller K1 achieves better worst-case performance than K2. Plot Bode magnitude plots of the nominal closed-loop sensitivity functions, as well as the worst instances, using usubs to replace the uncertain element with the worst value returned by wcgain.

bodemag(S1.Nom,'r--',usubs(S1,wcunc1),'r',S2.Nom,'b--',...
        usubs(S2,wcunc2),'b')
legend('Nominal S1','Worst Case S1','Nominal S2','Worst Case S2',...
        'Location','SouthEast')

% Although the nominal closed-loop sensitivity resulting from K2 is superior to that with K1, the worst-case behavior is much worse.
% Copyright 2015 The MathWorks, Inc.