function varargout = gcare(H,varargin)
%GCARE  Generalized solver for continuous algebraic Riccati equations.
%
%   GCARE is obsolete, please use ICARE instead.
%
%   See also ICARE.

% Old help:
%GCARE  Generalized solver for continuous algebraic Riccati equations.
%
%   [X,L] = GCARE(H,J,NS) computes the unique stabilizing solution
%   X of the continuous-time algebraic Riccati equation associated 
%   with Hamiltonian pencils of the form
% 
%                  [  A     F     S1 ]       [  E   0   0 ]
%      H - t J  =  [  G    -A'   -S2 ]  - t  [  0   E'  0 ]
%                  [ S2'   S1'     R ]       [  0   0   0 ]
% 
%   where F,G,R are symmetric matrices. The optional input NS is 
%   the row size of the A matrix. Omitting NS amounts to setting 
%   R=[] and omitting J amounts to setting E=I. The output L is
%   the vector of closed-loop eigenvalues.
%
%   [X,L,REPORT] = GCARE(H,J,NS) suppresses errors when X fails
%   to exist. The diagnosis REPORT is set to:
%     * -1 when the Hamiltonian pencil has jw-axis eigenvalues
%     * -2 when there is no finite stabilizing solution X
%     * 0 when a finite stabilizing solution X exists.
%
%   [X1,X2,D,L] = GCARE(H,...,'factor') returns two matrices X1, X2 
%   and a diagonal scaling matrix D such that X = D*(X2/X1)*D.
%   The vector L contains the closed-loop eigenvalues.  All outputs 
%   are empty when the associated Hamiltonian matrix has eigenvalues 
%   on the imaginary axis.
%
%   [...] = GCARE(H,...,'nobalance') disables automatic scaling of 
%   the data.
%
%   See also CARE, GDARE.

%   Author(s): Pascal Gahinet
%   Copyright 1986-2009 The MathWorks, Inc.

% Flags
% String support: strcmp does not support cells for strings
idx1 = find(cellfun(@(x) strcmp(x,'factor'),varargin));
FactorFlag = (~isempty(idx1));
% String support: strcmp does not support cells for strings
idx2 = find(cellfun(@(x) strcmp(x,'nobalance'),varargin));
if isempty(idx2)
   NewFlags = {};
else   
   NewFlags = {'noscaling'};
end

% J and NS
varargin(:,[idx1,idx2]) = [];
nextra = length(varargin);
if nextra<1
   J = [];
else
   J = varargin{1};
end
if nextra<2
   n = size(H,1)/2;
   if n~=round(n)
       error(message('Control:foundation:ARE26'))
   end
else
   n = varargin{2};
end

% Error checks
n2 = 2*n;
m = size(H,1)-n2;
if m<0
   error(message('Control:foundation:ARE28','gcare'))
elseif ~isempty(J) && ~isequal(size(H),size(J))
   error(message('Control:foundation:ARE25','gcare'))
elseif isempty(J) && m>0
   % Must go to pencil because of compression step
   J = blkdiag(eye(n2),zeros(m));
end

% Check Hamiltonian structure: S*H should be symmetric,
% S*J should be skew-symmetric, and the last m columns of
% J should be zero
SH = [-H(n+1:n2,:) ; H(1:n,:) ; H(n2+1:n2+m,:)];
BadStruct = (abs(SH-SH') > 100*eps*abs(SH));
if ~isempty(J)
   SJ = [-J(n+1:n2,:) ; J(1:n,:) ; J(n2+1:n2+m,:)];
   BadStruct = BadStruct | (abs(SJ+SJ') > 100*eps*abs(SJ));
end
if any(BadStruct(:)) || norm(J(:,n2+1:end),1) > 0
   error(message('Control:foundation:ARE27','gcare','gcare'))
end

% Extract matrices
A = H(1:n,1:n);
if isempty(J)
   E = [];
else
   E = J(1:n,1:n);
   if isequal(E,eye(n))
      E = [];
   end
end
G = H(1:n,n+1:n2);
Q = -H(n+1:n2,1:n);
B = H(1:n,n2+1:n2+m);
S = -H(n+1:n2,n2+1:n2+m);
R = H(n2+1:n2+m,n2+1:n2+m);

% Call ICARE
try
   [X,~,L,INFO] = icare(A,B,Q,R,S,E,G,NewFlags{:});
catch ME
   throw(ME)
end

% Set REPORT flag
switch INFO.Report
   case 3
      Report = -1;  % Hamiltonian pencil has jw-axis eigenvalues
   case 2
      Report = -2;  % there is no finite stabilizing solution X
   otherwise
      Report = 0;
end
   
% Process results and build outputs
if FactorFlag
   % Factored form
   X1 = INFO.U;
   X2 = INFO.V;
   if isempty(X1)
      D = [];
   else
      Sx = INFO.Sx;
      D = diag(Sx);
      if ~isempty(E)
         Z = orth([Sx .* E * (Sx .\ X1); X2]);
         X1 = Z(1:n,:);   X2 = Z(n+1:n2,:);
      end
   end
   varargout = {X1 X2 D L Report};
else
   if nargout<3
      switch Report
         case -1
            error(message('Control:foundation:ARE21'))
         case -2
            error(message('Control:foundation:ARE20'))
      end
   end
   varargout = {X L Report};
end