%% Mixed-Sensitivity Loop-Shaping Controller Design
% Load a plant model for a mixed-sensitivityH_{\infty } controller design. This two-input, two-output, six-state model is described in the example Loop Shaping of HIMAT Pitch Axis Controller.

load mixsynExampleData G
size(G)

%
% To design a controller for performance and robustness, shape the sensitivity and complementary sensitivity functions. Choose weights that are the inverse of the desired shapes.
% To achieve good reference-tracking and disturbance-rejection performance, shape S to be small inside the control bandwidth, which means choosing W1 large at low frequency, rolling off at high frequency. For this example, specify W1 with:
% Low-frequency gain of about 30 dB (33 in absolute units)
% High-frequency gain of about –6 dB (0.5 in absolute units)
% 0 dB crossover at about 5 rad/s.

W1 = makeweight(33,5,0.5);

% For robustness and noise attenuation, shape T to be small outside the control bandwidth, which means choosing W3 large at high frequency.

W3 = makeweight(0.5,20,20);

% Examine both weighting functions. Their inverses are the target shapes for S and T.

bodemag(W1,W3)
yline(0,'--');
legend('W1','W3','0 dB')
grid on

%
% Because S + T = I,  mixsyn cannot make both S and T small (less than 0 dB) in the same frequency range. Therefore, when you specify weights, there must be a frequency band in which both W1 and W3 are below 0 dB.
% Use mixsyn to compute the optimal mixed-sensitivity controller with these weights. For this example, impose no penalty on controller effort by setting W2 to [].

[K,CL,gamma] = mixsyn(G,W1,[],W3);
gamma

%
% The resulting gamma, which is the peak singular value across all frequencies, is well below 1, indicating that the closed-loop system meets the design requirements. Examine the resulting system responses. First, compare the resulting sensitivity S and complementary sensitivity T to the corresponding weighting functions W1 and W3.

L = G*K;
I = eye(size(L));
S = feedback(I,L); 
T= I-S;

sigma(S,'b',W1,'b--',T,'r',W3,'r--',{0.1,1000})
legend('S','W1','T','W3')

% The plot shows that S and T achieve the desired loop shape, where S is large inside the control bandwidth and a is small outside the control bandwidth.
%
% To see how mixed-sensitivity loop-shaping achieves the goals of classic loop shaping, compare the open-loop response L to the weighting functions. L ~ W1 where W1 is large, and L ~ 1/W3 where W3 is large.

sigma(L,'b',W1,'r--',1/W3,'g--',{0.1,1000})
legend('L','W1','1/W3')

%
% Copyright 2019 The MathWorks, Inc.