%Basic Nyquist plot
%kja 070515
aminit;

figure(1); clf; xsubplot(221);
ampolezero(tf([1], [1]), [-1.5 1.5 -1.5 1.5]); hold on;
axis square;

% Plot the Nyquist plot itself
L='0.2*exp(-s)./(s.*(s+1).^2)';
w=logspace(-0.9,1,200);s=i*w;lv=eval(L);
pl=plot(real(lv),imag(lv)); hold on
set(pl,'Linewidth', AM_data_linewidth)

% Compute the location of the delay margin
[xx,kk]=min(abs(lv+1));
plot(real(lv(kk)), imag(lv(kk)), 'ro', 'MarkerSize', AM_bode_markersize);

zm=[real(lv(kk)),imag(lv(kk))];
plot([-1 real(lv(kk))], [0 imag(lv(kk))], 'r-', 'LineWidth', AM_ref_linewidth);
amtext(-0.8,-0.25,'d');

% Compute the phase margin with an arc showing angle
[xx,kk]=min(abs(abs(lv)-1));
plot([1.5*real(lv(kk)) 0], [1.5*imag(lv(kk)) 0], 'k-', ...
  'LineWidth', AM_ref_linewidth);

plot(real(lv(kk)),imag(lv(kk)), 'ro', 'MarkerSize', AM_bode_markersize)
fim=atan2(imag(lv(kk)), real(lv(kk)));
fi2=-pi:pi/100:fim;
plot(cos(fi2), sin(fi2), 'r-', 'LineWidth', AM_ref_linewidth);

% set up the color
color = 'r';
if (AMPRINT_FLAG==2) color = 'k'; end;

len = length(fi2);
arrow([cos(fi2(len-1)), sin(fi2(len-1))], [cos(fi2(len)), sin(fi2(len))], ...
  AM_normal_arrowsize, 'FaceColor', color, 'EdgeColor', color);

% Complet the unit radius circle around the origin
fi=fim:0.01:pi;
plot(cos(fi),sin(fi),'k--', 'LineWidth', AM_ref_linewidth);
amtext(-1.2,0.2,'p');
amtext(-1.05,-0.55,'fi')

[xx,kk]=min(abs(angle(lv)+pi));
plot(lv(kk),0,'ro', 'MarkerSize', AM_bode_markersize);
amtext(-0.35,0.2,'c')

amprint('nyqmargin.eps');
