%% Find Tolerable Range of Gain and Phase Variations
% Consider a feedback loop with the following open-loop gain.

L = tf(3.5,[1 2 3 0]);

% Suppose that the system has gain uncertainty of 1.5 (gain can increase or decrease by a factor of 1.5) and phase uncertainty of ±30°.

DGM = getDGM(1.5,30,'tight');
F = umargin('F',DGM)

% Examine the robust stability of the closed-loop system.

T = feedback(L*F,1);
SM = robstab(T)

% robstab shows that the system can only tolerate 0.83 times the modeled uncertainty before going unstable. Scale the umargin block F by this amount to find the largest gain and phase variation that the system can tolerate.

factor = SM.LowerBound;
Fsafe = uscale(F,factor)

% The scaled uncertainty has smaller ranges of both gain variation and phase variation. Compare these ranges for the original modeled variation and the maximum tolerable variation.

DGM = F.GainChange;
DGMsafe = Fsafe.GainChange;
diskmarginplot([DGM;DGMsafe])
legend('original','safe')

%
% Copyright 2019 The MathWorks, Inc.