%% Plot Worst-Case Disk Margins of Uncertain System
% Plot the worst-case disk-based gain and phase margins of the following system:
% L_{\textrm{unc}} =\frac{1}{s^2 +a+10}\left(1+0\ldotp 1\Delta \right),
% where a is an uncertain real parameter with a nominal value of 1 and a range of 0.2–2, and Δ is a gain-bounded dynamic uncertainty.

a = ureal('a',1,'Range',[.2 2]);
Delta = ultidyn('Delta',1);
Lunc = tf(1,[1 a 10]) * (1+0.1*Delta);

wcdiskmarginplot(Lunc)
legend('location','SouthEast')

% The Worst perturbation curve corresponds to the combination of uncertain elements that yields the smallest disk margin across frequency. This perturbation corresponds to the wcu output of wcdiskmargin.
% The Worst-case margin curves show the lower and upper bounds on the worst-case margins at each frequency. For any perturbation within the specified uncertainty range, the disk-based gain or phase margins of the perturbed system lie below the Worst-case margin (upper bound) curve. In other words, this curve is the envelope produced by finding the smallest margins within the uncertainty at each frequency. For this system, the lower and upper bounds are close enough to appear identical on the plot. (See wcdiskmargin for more information about these bounds.)
%
% Focus the plot on the region between 0.5 and 10 rad/s.

w = {0.5,10};
wcdiskmarginplot(Lunc,w)

%
% Examine the effect on the worst-case margins of increasing the uncertainty range. To do this without changing the uncertainty specified in Lunc, use the ULevel option of wcOptions. This option scales the normalized uncertainty by the factor you specify. For example, examine the worst-case margins for a 50% greater uncertainty range.

opts = wcOptions('ULevel',1.5);
wcdiskmarginplot(Lunc,w,opts)

% In this case, the gain and phase margins for the worst perturbation reach zero, and the worst margin at any frequency is also zero. This result means that extending the uncertainty range this far encompasses some perturbations that drive the closed-loop system feedback(Lunc,1) unstable.
%
% Copyright 2019 The MathWorks, Inc.