function zgrid(zeta,wn,s)
%ZGRID  Generate z-plane grid lines for a root locus or pole-zero map.
%   ZGRID generates a grid over an existing discrete z-plane root 
%   locus or pole-zero map.  Lines of constant damping factor (zeta)
%   and natural frequency (Wn) are drawn in within the unit Z-plane 
%   circle.
%
%   If the current figure is empty, ZGRID plots the z-plane grid  
%   and sets the HOLD ON so that the root locus or pole-
%   zero map can be plotted over the grid.
%
%   ZGRID(Z,Wn) plots constant damping and frequency lines for the 
%   damping ratios in the vector Z and the natural frequencies in the
%   vector Wn.  ZGRID(Z,Wn,'new') clears the screen first.
%
%   See also: RLOCUS, SGRID, and PZMAP.

% Old help
%   ZGRID('new') is still supported but is obsolete
%
%   ZGRID('new') clears the graphics screen before plotting the 
%   z-plane grid and sets the HOLD ON so that the root locus or pole-
%   zero map can be plotted over the grid using (for example):
%
%       zgrid('new')
%       rlocus(num,den) or pzmap(num,den)

%   Marc Ullman   May 27, 1987
%   Revised JNL 7-10-87, CMT 7-13-90, ACWG 6-21-92
%   Copyright (c) 1986-97 by The MathWorks, Inc.
%   $Revision: 1.3 $  $Date: 1997/04/09 00:46:09 $

ni = nargin;
status = ishold;
ax = gca;

%---Check if the current axis is empty
%---if so, act as if sgrid('new') was invoked
kids = get(ax,'children');
if ( isempty(kids) ) & ( ni==0 ),
   ni = 1;
end

if ni==0,   % Plot on existing graph
    zeta = 0:.1:.9;
    wn = 0:pi/10:pi;
    axis(axis)
    hold on

elseif ni==1, % Clear screen then plot standard grid
    zeta = 0:.1:.9;
    wn = 0:pi/10:pi;
    hold off

elseif ni==2, % Use zeta and wn specified
    hold on

elseif ni==3, % Clear screen and use zeta and wn specified
    hold off
end

Color = get(ax,'XColor');

% Plot Unit circle
t=0:.1:6.3;
plot(sin(t),cos(t),'LineStyle','-','Color',Color)
hold on

% Plot damping lines
NanMat = NaN;
I = sqrt(-1);
if ~isempty(zeta),
    m = tan(asin(zeta)) +sqrt(-1);
        Ones = ones(1,length(m));
    zz = [exp((0:pi/20:pi)'*(-m)); NanMat(Ones)];
        zz = zz(:);
        rzz = real(zz);
        izz = imag(zz);
    plot([rzz; rzz],[izz; -izz],'LineStyle',':','Color',Color);
end

% Plot natural frequency lines
if ~isempty(wn),
    e_itheta = exp(sqrt(-1)*(pi/2:pi/20:pi)');
    e_r = exp(wn);
        Ones = ones(1,length(e_r));        
    zz = [(ones(length(e_itheta),1)*e_r).^(e_itheta*Ones); NanMat(Ones)];
        zz = zz(:);
        rzz = real(zz);
        izz = imag(zz);
    plot([rzz; rzz],[izz; -izz],'LineStyle',':','Color',Color);
end

% Return hold to previous status
if ( (ni==0) | (ni==2) ) & ~status, 
   hold off
end

% end zgrid
