%% Mixed-Sensitivity H2 Loop Shaping
% Shape the singular value plots of the sensitivity S = (I+ GK)^{-1} and complementary sensitivity T = GK(I+GK)^{-1}.
% To do so, find a stabilizing controller K that minimizes the H_2 norm of:
%
% Assume the following plant and weights:
% G(s) = \frac{{s - 1}}{{s - 2}},{\rm{ }}{W_1} = \frac{{0.1}}{{100s + 1}},{\rm{ }}{W_2} = 0.1{\rm{, }}{W_3} = 0.
% Using those values, construct the augmented plant P, as illustrated in the mixsyn reference page.

s = zpk('s');
G = 10*(s-1)/(s+1)^2;
G.u = 'u2';
G.y = 'y';

W1 = 0.1/(100*s+1); 
W1.u = 'y2';
W1.y = 'y11';

W2 = tf(0.1); 
W2.u = 'u2';
W2.y = 'y12';

S = sumblk('y2 = u1 - y');
 
P = connect(G,S,W1,W2,{'u1','u2'},{'y11','y12','y2'});

%
% Use h2syn to generate the controller. This system has one measurement signal and one control signal, which are the last output and input of P, respectively.

[K,CL,gamma] = h2syn(P,1,1);

%
% Examine the resulting loop shapes.

L = G*K; 
S = inv(1+L); 
T = 1-S;
sigmaplot(L,'k-.',S,'r',T,'g')
legend('open-loop','sensitivity','closed-loop')

% Copyright 2015 The MathWorks, Inc.