%Nyquist curve and sensitivity
aminit;

den=[1 1];den=conv(den,den);den=conv(den,den);
den=conv(den,[1 0]);
num=[0.78 0.39];
L=tf(num,den);

%   The Nyquist plot
w=0.1:0.005:2;
[re,im]=nyquist(L,w);re=re(:);im=im(:);

figure(1); clf; xsubplot(221);
ampolezero(tf([1], [1]), [-2.7 0.3 -1.8 1.2]); axis square
plot(re, im, 'Linewidth', AM_bode_linewidth);

dphi=pi/100;phi=-pi:dphi:pi;
x=cos(phi)-1; y=sin(phi);
uc=plot(x, y, 'k--', 'Linewidth', AM_bode_linewidth)

% x=-2.2:0.1:2;null=zeros(size(x));plot(x,null);plot(null,x);
re=re(1:100); im=im(1:100);
[v,ii]=min(abs(sqrt((re+1).^2+im.^2)-1));%ws
plot(re(ii),im(ii),'ro', 'MarkerSize', AM_data_markersize);
text(re(ii)+0.1,im(ii)-0.1,'ws')

[v,ii]=min(abs(sqrt(re+1).^2+im.^2));%wms
plot(re(ii), im(ii), 'ro', 'MarkerSize', AM_data_markersize);
text(re(ii)+0.1,im(ii),'wms');
text(re(ii)-0.4,0.85*im(ii),'ms');

% Critical point
plot(-1, 0, 'b+', 'MarkerSize', AM_data_markersize);
text(-1.1,0.15,'-1')

s=0:0.01:1;
xx=s*(re(ii)+1);yy=s*im(ii);
plot(xx-1, yy, 'r-', 'LineWidth', AM_ref_linewidth);

amprint('nyquist-rob2.eps');

figure(2); clf; subplot(221);
ampolezero(tf([1], [1]), [-1.3 0.5 -1.3 0.5]); axis square

% Plot the Nyquist curve
[re,im]=nyquist(L,w);re=re(:);im=im(:);
plot(re, im, 'Linewidth', AM_bode_linewidth)

% Unit circle
dphi=pi/100;phi=-pi:dphi:pi;
r=0.5;ind=60;
x=re(ind)+r*cos(phi);y=im(ind)+r*sin(phi);uc=plot(x,y,'k--');
set(uc,'Linewidth', AM_ref_linewidth)

% Mark the frequency we are interested in
plot(re(ind), im(ind), 'ro', 'MarkerSize', AM_data_markersize);

% Add the critical point
plot(-1,0, '+', 'MarkerSize', AM_data_markersize);
text(-1,0.15,'-1');

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

% Indicate the radius for the uncertainty
arrow([re(ind), im(ind)], [re(ind)+r, im(ind)], AM_small_arrowsize, ...
  'FaceColor', color, 'EdgeColor', color);
text(re(ind)+0.2,im(ind)-0.1,'cdp')

% Draw a line to the critocal point
plot([-1 re(ind)], [0,im(ind)], 'r-')
text(-1.0,0.7*im(ind),'a')

amprint('nyquist-rob3.eps');
