% DADOS  DO SISTEMA
A = [0 1 0;0 0 1; -20 -14 -4];
H = [0 0 1]';
E = [2 1 0]; 
J = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nx = size(A,1);
nw = size(H,2);
nz = size(E,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
opt = 2; % 1 - para norma H2
         % 2 - para norma Hoo

switch opt
    case 1
%%%%%%%%%%%%%%%%%%%%%%%% NORMA H2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

% Descreve a LMI a ser projetada
setlmis([])

% declaracao de variveis
%===============
P = lmivar(1,[nx 1]);
%===============

% descricao das LMIs
%===============
ct = 1;
lmiterm([ct,1,1,P],A',1,'s');
lmiterm([ct,2,1,0],E);
lmiterm([ct,2,2,0],-1);

ct = ct+1;
lmiterm([-ct,1,1,P],1,1);

lmisys = getlmis;
%===============

% Declaracao funcao objetivo
options = [1e-4,2000,0,200,0];
%===============

np = decnbr(lmisys);

c = zeros(np,1);
for i=1:np
    Pi = defcx(lmisys,i,P);
    c(i) = trace(H'*Pi*H);
end

[copt,xopt] = mincx(lmisys,c,options);

if (isempty(copt))
    Ps = [];
    return
end

Ps = dec2mat(lmisys,xopt,P);
NormaH2 = trace(H'*Ps*H) % Norma ao quadrado

    case 2
%%%%%%%%%%%%%%%%%%%%%%%% NORMA Hoo %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%% 
% Descreve a LMI a ser projetada
setlmis([])

% declaracao de variaveis
%===============
P = lmivar(1,[nx 1]);
rho = lmivar(1,[1 0]);
%===============

% descricao das LMIs
%===============
ct = 1;
lmiterm([ct,1,1,P],A',1,'s');
lmiterm([ct,1,1,0],E'*E);
lmiterm([ct,2,1,P],H',1);
lmiterm([ct,2,1,0],J'*E);
lmiterm([ct,2,2,0],J'*J);
lmiterm([ct,2,2,rho],-1,1);

ct = ct+1;
lmiterm([-ct,1,1,P],1,1);

ct = ct+1
lmiterm([-ct,1,1,rho],1,1);

lmisys = getlmis;
%===============

% Declaracao funcao objetivo
options = [1e-4,2000,1e9,200,0];
%===============

np = decnbr(lmisys);

c = zeros(np,1);
c(decinfo(lmisys,rho)) = 1;

[copt,xopt] = mincx(lmisys,c,options);

if (isempty(copt))
    Ps = [];
    rhos = [];
return
end

Ps = dec2mat(lmisys,xopt,P);
rhos = dec2mat(lmisys,xopt,rho);
NormaHoo = rhos % Norma ao quadrado
end
