function [K,P,P1,P2] = lqrc(A,B,QRN,aretype)
%
% [K,P,P1,P2] = LQRC(A,B,QRN,ARETYPE) solves the Riccati equation and 
%     produces the optimal LQR feedback gain K such that the feedback law
%     u = -Kx minimizes the cost function:
%
%       J = 1/2 Integral { [x' u'] | Q  N | |x| } dt ; ( QRN := |Q  N| )
%                                  | N' R | |u|        (        |N' R| )
%                                           .
%     subject to the constraint equation: x = Ax + Bu.
%     Also returned is P, the steady-state solution to the ARE:
%				      -1
%		0 = A'P + PA - (PB+N)R  (B'P+N') + Q
%
%     In addition, it will calculate the residual of the ARE. If the 
%     problem is ill-posed such that the residual is large or there 
%     exists jw-axis closed loop poles, a warning message will be displyed.
%
%     aretype = 'eigen' --- solve Riccati via eigenstructure (default)
%     aretype = 'Schur' --- solve Riccati via Schur method.
%

% R. Y. Chiang & M. G. Safonov 8/85
% Copyright (c) 1988 by the MathWorks, Inc.
% All Rights Reserved.
% ------------------------------------------------------------------------
if nargin == 3
   aretype = 'eigen';
end
%
[n,n] = size(A);
[Q,N,NT,R] = sys2ss(QRN,n);
Q = Q - N/R*NT;
A = A - B/R*NT;
RR = B/R*B';
[P1,P2,lamp,Perr,wellposed,P] = aresolv(A,Q,RR,aretype);
K = R\(NT + B'*P);
%
% ------ End of LQRC.M ---- RYC/MGS %
