%% Combined Gain and Phase Variation
% The disk-based gain margins returned by diskmargin assume no phase variation, and the phase margins assume no gain variation. In practice, systems experience both gain and phase variation at the same time. Disk-margin analysis accounts for such simultaneous gain and phase variation.
% Compute the disk-based gain and phase margins of a system. Then, use diskmarginplot to visualize the corresponding allowed ranges of simultaneous gain and phase variation.

L = tf(25,[1 10 10 10]);
DM = diskmargin(L);
DGM = DM.GainMargin
DPM = DM.PhaseMargin

%

diskmarginplot(DGM)

% The shaded region in the plot shows the range of simultaneous gain and phase variations that preserve the stability of the closed-loop system feedback(L,1). When there is no phase variation, the system can tolerate the full gain-variation range DGM, from –4 dB to 4 dB. If the phase is allowed to vary by about ±20 degrees, the allowable gain-variation range drops to about –2.5 dB to 2 dB. At the full phase-variation range of ±26 degrees, the system can tolerate no gain variation.
%
% To visualize multiple ranges on the same plot, combine them into a two-column vector. For instance, compute the disk margins of L with positive and negative skew and plot all three ranges of variation together.

DMn = diskmargin(L,-2);
DGMn = DMn.GainMargin;
DMp = diskmargin(L,2);
DGMp = DMp.GainMargin;
DGMall = [DGMn;DGM;DGMp];
diskmarginplot(DGMall)

% This plot shows that the feedback loop can tolerate larger gain and phase variations when the gain decreases. In other words, the loop stability is more sensitive to gain increase. For more information about how varying skew affects disk-based gain and phase margin estimates, see Stability Analysis Using Disk Margins.
%
% Copyright 2019 The MathWorks, Inc.