%% Eigenvalues of the Hamiltonian matrix
% First generate some LTI system (a quadruple of matrices A, B, C and D) randomly. Create also the weighting matrices Q and R.

G = rss(3,2,2);
[A,B,C,D] = ssdata(G);
Q = diag([1 2 3]); 
R = diag([1 2]);

%
% Build the Hamiltonian matrix (both conventions)

H = [A, -B/R*B';
    -Q, -A']
%H = [A, B/R*B';
%     Q, -A']

%
% Compute the LQ-optimal state-feedback gain

[S,K,p] = icare(A,B,Q,R,[],[],[])

%
% Analyze the eigenvalues of the Hamiltonian matrix(ces)

h = complex(eig(H)) 
plot(h,'rp','MarkerSize',12)
hold on
plot(complex(eig(A-B*K)),'bo')     % no need to compute the eig() again, we have it in p already
xlabel('Real')
ylabel('Imaginary')
grid
hold off