%% Guaranteed stability margins of LQ-optimal state feedback control

n = 5;                              % order of the plant
G = rss(n,n,1); 
G.c = eye(n,n); 
G.d = zeros(n,1);                   % random system, states at the output
Q = 100*eye(n,n);                   % weighting matrices
R = 1;

%

[K,S,e] = lqr(G,Q,R);               % LQ design
L = K*G;                            % open-loop transfer function

margin(L)
grid on
nyquist(L)