%% Worst-Case Disk Margins for Uncertain MIMO Feedback Loop
% Use wcdiskmargin to compute worst-case loop-at-a-time and multiloop disk margins. This example illustrates that loop-at-a-time margins can give an overly optimistic assessment of the true robustness of MIMO feedback loops. Margins of individual loops can be sensitive to small perturbations within other loops.
% Consider the closed-loop system of the following illustration.
%
% P is a two-input, two-output second-order plant and C is a 2x2 static gain. Construct P in state-space form, assuming that it has an uncertain parameter and some dynamic uncertainty. Compute the worst-case disk margins at the plant output (to compute the margins at the plant input, use L = C*Pu).

p = ureal('p',10,'Percentage',10);
a = [-0.2 p;-p -0.2]; 
b = eye(2); 
c = [1 p;-p 1];
d = zeros(2,2);
P = ss(a,b,c,0);
DEL = ultidyn('DEL',[2 2],'Bound',0.1);
Pu = P*(eye(2)+DEL);

C = [1 -2;0 1];
L = Pu*C;

[wcDM,wcu] = wcdiskmargin(L,'siso');

%
% Examine the worst-case loop-at-a-time disk margins, returned in the structure array wcDM. Each entry in this structure array contains the worst-case stability margins of the corresponding channel.

wcDM(1)  

% The result in wcDM(1) gives guaranteed stability margins for the specified uncertainty range. As long as the open-loop gain of the first channel changes by a factor between 0.53 and 1.88, the closed loop remains stable for all (p,DEL) values within the specified range. Similarly, the closed loop remains stable as long as the phase variation does not exceed 34° in absolute value.
% Similarly, wcDM(2) shows that in the second feedback channel, the gain can vary by any factor between  0.52 and 1.93 or the phase can vary by up to 35°, and the system remains stable for such variations and the (p,DEL) uncertainty.

wcDM(2)

%
% The lower bound returned by wcdiskmargin is a theoretical minimum guaranteed worst-case disk margin. The upper bound corresponds to an actual perturbation in the specified uncertainty range that approaches the lower-bound prediction. The output wcu contains the values of that perturbation for each feedback channel. For example, wcu(2) is the worst combination of (alpha,DEL) for the second channel, and the disk margins for this worst combination are close to wcDM(2). In particular, DM(2).UpperBound and wcDM(1).UpperBound match.

wcL = usubs(L,wcu(2));
DM = diskmargin(wcL);
DM(2)

%
% In practice, gain and phase variations affect both channels simultaneously. To estimate the stability margins with respect to such independent and concurrent variations, examine the worst-case multiloop disk margins.

wcMM = wcdiskmargin(L,'mimo')

% The multiloop margins are much weaker than when considering one loop at a time. This is because it takes a smaller amount of gain (or phase) variation to destabilize the feedback loop when both channels are subject to variations.
%
% You can visualize how uncertainty affects the margins with wcdiskmarginplot. This plots the (disk-based) gain and phase margins as a function of frequency for the nominal and worst-case values of (alpha,DEL) as well as 20 random samples of this uncertainty. The plot shows that uncertainty weakens the margins most near DC.

wcdiskmarginplot(L,{1e-1,1e1})
legend('location','NorthWest')

%
% Finally, compute the multiloop margin for simultaneous variations in gain (or phase) at both the plant inputs and plant outputs. When you allow the gain (or phase) to vary in more places, it becomes easier to destabilize the feedback loop, so the margins get even smaller. Thus, the multiloop I/O margin provide the most comprehensive and most conservative assessment of robust stability in the face of gain or phase variations and (alpha,DEL) uncertainty.

wcMMIO = wcdiskmargin(Pu,C)

%
% Copyright 2018 The MathWorks, Inc.
%
%
%