%% Disk Margins of MIMO Feedback Loop
% diskmargin computes both 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 in other loops, and loop-at-a-time margins ignore such loop interactions.
% Consider the two-channel MIMO feedback loop of the following illustration.
%
% The plant model P is drawn from MIMO Stability Margins for Spinning Satellite and C is the static output-feedback gain [1 -2;0 1].

a = [0 10;-10 0]; 
b = eye(2); 
c = [1 10;-10 1]; 
P = ss(a,b,c,0); 
C = [1 -2;0 1]; 

%
% Compute the disk-based margins at the plant output. The negative-feedback open-loop response at the plant output is Lo = P*C.

Lo = P*C;
[DMo,MMo] = diskmargin(Lo);

%
% Examine the loop-at-a-time disk margins returned in the structure array DM. Each entry in DM contains the stability margins of the corresponding feedback channel.

DMo(1)
DMo(2)

%
% The loop-at-a-time margins are excellent (infinite gain margin and 90° phase margin). Next examine the multiloop disk margins MMo. These consider independent and concurrent gain (phase) variations in both feedback loops. This is a more realistic assessment because plant uncertainty typically affects both channels simultaneously.

MMo

%
% The multiloop gain and phase margins are much weaker than their loop-at-a-time counterparts. Stability is only guaranteed when the gain in each loop varies by a factor less than 1.46, or when the phase of each loop varies by less than 21°. Use diskmarginplot to visualize the gain and phase margins as a function of frequency.

diskmarginplot(Lo)

%
% Typically, there is uncertainty in both the actuators (inputs) and sensors (outputs). Therefore, it is a good idea to compute the disk margins at the plant inputs as well as the outputs. Use Li = C*P to compute the margins at the plant inputs. For this system, the margins are the same at the plant inputs and outputs.

Li = C*P;
[DMi,MMi] = diskmargin(Li);
MMi

%
% Finally, you can also compute the multiloop disk margins for gain or phase variations at both the inputs and outputs of the plant. This approach is the most thorough assessment of stability margins, because it this considers independent and concurrent gain or phase variations in all input and output channels. As expected, of all three measures, this gives the smallest gain and phase margins.

MMio = diskmargin(P,C);
diskmarginplot(MMio.GainMargin)

%
% Stability is only guaranteed when the gain varies by a less than 2 dB or when the phase varies by less than 13°. However, these variations take place at the inputs and the outputs of P, so the total change in I/O gain or phase is twice that.
%
% Copyright 2018 The MathWorks, Inc.