%
%
% Function to call Robust Control Toolbox function HINFOPT
% with same syntax as HINFSYN in MU-TOLS.
%

function [K, clp, gamfin] = shinfopt(P, ny, nu,...
                 maxgam, mingam, tol, gamind )


   [A,B,C,D] = unpck(P);
   B1  = B(:,1:size(B,2)-nu);
   B2  = B(:,size(B,2)-nu+1:size(B,2)); 
   C1  = C(1:size(C,1)-ny,:);
   C2  = C(size(C,1)-ny+1:size(C,1),:);
   D11 = D(1:size(C,1)-ny,1:size(B,2)-nu);
   D12 = D(1:size(C,1)-ny,size(B,2)-nu+1:size(B,2));
   D21 = D(size(C,1)-ny+1:size(C,1),1:size(B,2)-nu);
   D22 = D(size(C,1)-ny+1:size(C,1),size(B,2)-nu+1:size(B,2));
   if nargin < 7
      gamind = 1:size(C1,1);
   end 

   aux = [tol 1/mingam 1/maxgam];

%   [A B; C D] - [A B1 B2; C1 D11 D12; C2 D21 D22]

   [gmi, Acp, Bcp, Ccp, Dcp, Acl, Bcl, Ccl, Dcl] = ...
       hinfopt(A,B1,B2,C1,C2,D11,D12,D21,D22,gamind,aux);

   gamfin = 1/gmi;

   disp(['  Gamma value achieved: ',num2str(gamfin)]);

   K   = pck(Acp, Bcp, Ccp, Dcp);
   clp = pck(Acl, Bcl, Ccl, Dcl);


