%  Example 6.7.  Nyquist polt and stability margin generations for a
%    LQR control system.
clear
%  Define the plant model.
A=[0 1
   0 0];
B=[0
   1];
%  Define the cost function parameters.
Q=[1 0
   0 0];
R=0.1;
%  Find the steady-state LQR gain. 
K=lqr(A,B,Q,R)
%  Generate a Nyquist plot for the system.
set(0,'DefaultAxesFontName','times')
set(0,'DefaultAxesFontSize',16)
set(0,'DefaultTextFontName','times')
figure(1)
clf
[re,im]=nyquist(A,B,K,0,1);
plot(re,im,'r-',re,-im,'r-',-1,0,'r+',re(47),im(47),...
   'r<',re(47),-im(47),'r>')
axis([-2 2 -2 2])
axis('square')
xlabel('Real axis')
ylabel('Imaginary axis')
grid
%  Compute the gain and phase margins.  Note:  The gain margin generated 
%  by this command is the downside gain margin.  From the Nyquist graph 
%  it can be seen that the gain margin is infinite.
[Gm,Pm] = margin(A,B,K,0)
