%% Anti-Stabilizing Solution of Continuous-Time Algebraic Riccati Equation
% To find the anti-stabilizing solution of the continuous-time algebraic Riccati equation A^T X+\textrm{XA}-\textrm{XB}B^T X+CC^T =0, consider the following matrices:
% A=
%   \left[ {\begin{array}{ccc}
%    1 & -2 & 3\\
%    -4 & 5 & 6 \\
%     7 & 8 & 9\\
%   \end{array} } \right]\;\;\;\;\;\;\;\;\;B=
%   \left[ {\begin{array}{c}
%    5\\
%    6\\
%     -7\\
%   \end{array} } \right]\;\;\;\;\;\;\;\;\;C=
%   \left[ {\begin{array}{ccc}
%    7 & -8 & 9\\
%   \end{array} } \right].
% For greater numerical accuracy, re-write the algebraic Riccati equation in the following way:
% A^T X+\textrm{XA}–\left\lbrack \textrm{XB},C^T \right\rbrack *\left\lbrack I,0;0,-I\right\rbrack \left\lbrack B^T X;C\right\rbrack =0.
% The above equation is the standard form of A^T X+\textrm{XA}-\left(\textrm{XB}+S\right)R^{-1} \left(B^T X+S^T \right)=0,
% where B=\left\lbrack B,0\right\rbrack ,\;S=\left\lbrack 0,C^T \right\rbrack ,\textrm{and}\;R=\left\lbrack I,0;0,-I\right\rbrack.
% Compute the anti-stabilizing solution using the 'anti' option.

A = [-1,2,3;4,5,-6;7,-8,9];
B = [5;6;-7];
C = [7,-8,9];
n = size(A,1);
m = size(B,2);
p = size(C,1);
R = blkdiag(eye(m),-eye(p));
BB = [B,zeros(n,p)];
S = [zeros(n,m),C'];
[X,K,L] = icare(A,BB,0,R,S,[],[],'anti')

% Here, X is the unique anti-stabilizing solution, K contains the state-feedback gain and L contains the closed-loop eigenvalues.
% Copyright 2018 The MathWorks, Inc.