%% Loop Shaping with mixsyn
% Use mixsyn for sensitivity and complementary sensitivity loop shaping. Create a plant model and weighting functions that:
% Shape the sensitivity function for reference tracking and disturbance rejection (W1 = 1/S large inside the control bandwidth).
% Shape the complementary sensitivity for robustness and noise attenuation (W3 = 1/T large outside the control bandwidth).
% Limit the control effort (W2 = 1/KS large inside the control bandwidth).
% (For more information about choosing weighting functions, see Mixed-Sensitivity Loop Shaping.)

s = zpk('s');
G = (s-1)/(s+1)^2;

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

bodemag(W1,W2,W3)

%
% Design the controller.

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

%
% mixsyn shapes the singular values of the sensitivity function S, the complementary sensitivity function T, and the control effort R = K*S. Examine the results of the synthesis and the shapes of these transfer functions.

S = feedback(1,G*K);
KS = K*S;
T = 1-S;
sigma(S,'b',KS,'r',T,'g',gamma/W1,'b-.',ss(gamma/W2),'r-.',gamma/W3,'g-.',{1e-3,1e3})
legend('S','KS','T','GAM/W1','GAM/W2','GAM/W3','Location','SouthWest')
grid

%
% Copyright 2012 The MathWorks, Inc.