%% Unstructured Robust Controller Synthesis
% Synthesize a stabilizing robust controller K for the system in the following illustration, where the plant G includes some dynamic uncertainty. The controller must also reject disturbances injected at the plant output.
%
% The nominal plant model G0 is an unstable first-order system.

G0 = tf(1,[1 -1]);

%
% The uncertainty in G0 is as follows:
% At low frequency, below 2 rad/s, the plant can vary up to 25% from its nominal value.
% Around 2 rad/s, the percentage variation starts to increase, reaching 400% at approximately 32 rad/s.
% Represent the frequency-dependent model uncertainty with the weight Wu and the uncertain LTI dynamic uncertainty InputUnc, an ultidyn control design block.

Wu = 0.25*tf([1/2 1],[1/32 1]); 
InputUnc = ultidyn('InputUnc',[1 1]);
G = G0*(1+InputUnc*Wu);

%
% musyn seeks a controller that optimizes robust performance from inputs to outputs. To set up this problem for musyn, then, insert a weighting function Wp that captures the disturbance rejection goal.
%
% When you provide this augmented plant P to musyn, the function designs a controller that drives the transfer function from d to e below 1 at all frequencies. That transfer function is Wp/S, where S = 1 – GK is the sensitivity function. Thus, choose Wp to be the inverse of the desired sensitivity. For this example, choose Wp with:
% Low-frequency gain of 100 (40 dB)
% 0 dB crossover at 0.5 rad/s
% High-frequency gain of 0.25 (–12 dB)

Wp = makeweight(100,[1 0.5],0.25);
bodemag(Wp)

%
% You can now construct the plant as shown in the block diagram by naming the signals, defining a sum block, and using connect. Construct the plant so that the control input is the last input, and the measurement output is the last output.

G.InputName = 'u';
G.OutputName = 'y1';
Wp.InputName = 'y';
Wp.OutputName = 'e';
SumD = sumblk('y = y1 + d');

inputs = {'d','u'};
outputs = {'e','y'};
P = connect(G,Wp,SumD,inputs,outputs);

%
% Use musyn to design a controller K for this uncertain system.

nmeas = 1;
ncont = 1;
[K,CLperf,info] = musyn(P,nmeas,ncont); 

% The display shows that the best achieved robust performance is about 0.65. This result means that the gain from d to e remains below 0.65 for up to 1/0.65 times the uncertainty specified in the plant. Thus, the controller achieves the robust performance objectives for the full range of modeled uncertainty. (For more information on interpreting musyn results, see Robust Controller Design Using Mu Synthesis.)
%
% You can examine the robust performance using analysis commands such as robgain and wcgainplot. For instance, examine the worst-case gain of the closed-loop system.

CL = lft(P,K);
wcg = wcgain(CL)

% This result confirms that the actual worst-case gain over the modeled uncertainty is about 0.53, which is within the robust performance of 0.65 guaranteed by musyn.
%
% For this problem, the controller returned by musyn is fairly high order.

size(K)

% You can try reducing the controller order with model-reduction commands such as balred or reduce to see whether you can maintain robust performance. (For an example, see the musynperf reference page.) Or, you can try specifying a lower order controller structure and use musyn to tune it.  See Robust Tuning of Fixed-Structure Controller.
%
% Copyright 2019 The MathWorks, Inc.