%% Gain and Phase Variation in SISO Loop
% Create a model of a SISO control loop, with gain uncertainty of ±6 dB and phase uncertainty of ±30°. Use open-loop transfer function
% L=\frac{3\ldotp 5}{s^3 +2s^2 +3s}.

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

%
% To model the uncertainty, first use getDGM to convert the gain and phase variation into a disk-based gain-margin range. Because the gain can increase or decrease by the same amount, you can use the 'balanced' option to model a disk of uncertainty that is symmetric around the nominal value.

GM = db2mag(6);
PM = 30;
DGM = getDGM(GM,PM,'balanced')

% DGM defines a disk of uncertainty with gain variations in the range given by DGM, and phase variations determined by the geometry of the disk. Use DGM to create a umargin block.

F = umargin('F',DGM)

%
% F represents the smallest uncertainty disk that can capture both the target gain and phase variation. The actual phase variation modeled by F is a little bigger than the target range of ±30°. To visualize the full range of gain and phase variations represented by F, including simultaneous gain and phase variations, use plot.

plot(F)

% The right plot shows the range of values in the complex plane that the multiplicative factor F can take. The size of the disk determines the amount of variation. The shaded region on the left plot shows the simultaneous variations of gain and phase encompassed in F. For more details about this uncertainty model, see Stability Analysis Using Disk Margins.
%
% To incorporate the gain and phase uncertainty into a model of the closed-loop system, insert it into the feedback loop as a multiplicative factor on the open-loop response.
%

T = feedback(L*F,1)

% The result is an uncertain state-space (uss) model with one control design block, F. Examine the effect of the modeled gain and phase uncertainty on the step response of the closed-loop system.

figure;
rng default  % for reproducibility
step(T)

% You can perform addition analysis with this model, such as analyzing the system robustness against the modeled gain and phase variation with robstab. Or, you can use musyn to design a robust controller for the uncertain system L*F. For examples, see
% Model Gain and Phase Uncertainty in Feedback Loops
% Robust Controller for Spinning Satellite
%
% Copyright 2019 The MathWorks, Inc.