%% Design Robust Controller With Specified Gain and Phase Margins
% The robust controller returned by musyn optimizes robust performance of uncertain feedback systems. When the uncertain plant contains umargin blocks, this requirement of robust stability is equivalent to enforcing disk-based gain and phase margins equal to the umargin uncertainty. In this example, design a robust controller for an uncertain plant, enforcing closed-loop stability against gain and phase variations at the plant inputs and outputs.
%
% Use the plant from the example "Loop Shaping with mixsyn" on the mixsyn reference page, introducing some uncertainty in the location of the system poles and zero.

a = ureal('a',1,'PlusMinus',[-0.1,0.1]);
s = zpk('s');
G = (s-a)/(s+a)^2;

%
% The goal is to enforce closed-loop stability against gain and phase variation at the plant inputs and outputs, over the full range of parameter variation modeled in the plant G. To do so, use the target gain and phase margins to create umargin uncertain blocks and attach them to the plant. For this example, suppose that you want stability against gain variations of a factor of 1.5 in either direction, or phase variations of ±20°.

DGM = getDGM(1.5,20,'tight');
Fin = umargin('Fin',DGM);
Fout = umargin('Fout',DGM);
Gmarg = Fout*G*Fin

%
% For tuning with musyn, you augment the plant with weighting functions that enforce your performance requirement such as reference tracking, disturbance rejection, and robustness. For this example, use the weighting functions described in the example on the on the mixsyn reference page.

W1 = makeweight(10,[1 0.1],0.01);
W2 = makeweight(0.1,[32 0.32],1);
W3 = makeweight(0.01,[1 0.1],10);

Gaug = augw(Gmarg,W1,W2,W3);

%
% Use musyn to design a controller.

[K,gam] = musyn(Gaug,1,1);

% musyn achieves a robust performance of about 1, which tells you that the closed-loop gain remains below 1 for the full range of uncertainty specified in the plant. To confirm that the resulting controller achieves the target gain and phase margins, use wcdiskmargin to examine the worst-case gain and phase margins of the system against simultaneous variations at the plant inputs and outputs. Use the plant G that contains the parameter uncertainty but not the gain and phase uncertainty.
%

MMIO = wcdiskmargin(G,K)

%
% The worst-case disk-based gain margin of [0.6 1.66] is slightly larger than the target margin of [0.66 1.5], and the worst-case phase margin of ±28° is likewise better than the required margin of ±20°. Thus, the controller K enforces the desired margins for the entire parameter-uncertainty range of the plant G.
% For an example that uses umargin blocks with musyn to enforce gain and phase margins in a MIMO control loop, see Robust Controller for Spinning Satellite.
%
% Copyright 2019 The MathWorks, Inc.