% nicholsgainloci.m
% Adds the loci of constant gain of the complementary sensitivity 
% function to a Nyquist plot
%   kja 071005

aminit;
figure(1); clf; subplot(221);

% Create patch vectors
M=1.1;cx=-M^2/(M^2-1);r=M/(M^2-1);
X1=[];Y1=[];
%for ff=-1.5*pi/2:pi/100:1.5*pi/2;
for ff=-1.9*pi/2:pi/100:1.9*pi/2;    
    X1=[X1 cx+r*cos(ff)];Y1=[Y1 r*sin(ff)];
end
M=1/M;cx=-M^2/(M^2-1);r=-M/(M^2-1);
X2=[];Y2=[];
%for ff=pi-3.0:0.01:pi+3.0;
for ff=0:pi/100:2*pi;
    X2=[X2 cx+r*cos(ff)];Y2=[Y2 r*sin(ff)];
end
X=[X1 X2];Y=[Y1 Y2];
Z=X+i*Y;LZ=log(Z);
patch(unwrap(imag(LZ)), real(LZ), [0.9,0.9,0.9], 'EdgeColor', 'none');
patch([-4 -4 4 4],[2.35 3 3 2.25],[0.9,0.9,0.9], 'EdgeColor', 'none');
hold on;

% Replot the axes that get wiped out by the patch
plot([-4 -4 0 0], [-1 3 3 -1], 'k-', 'LineWidth', AM_tick_linewidth);

MMt=[1.1 1.2 1.3 1.5 2 5];
s='b-';
%Gain curves
for ii=1:6,
M=MMt(ii);
cx=M^2/(M^2-1);cy=0;r=M/(M^2-1);
nicplot(-cx,cy,r,s);hold on;
M=1/M;
cx=-M^2/(M^2-1);cy=0;r=M/(M^2-1);
nicplot(cx,cy,r,s);hold on;
end
%Phase curves
PP=pi*[10 20 40 60 80]/180;s=':';
for jj=1:5,
  fi=PP(jj);
  cx=-0.5;cy=0.5/tan(fi);r=0.5/sin(fi);
  nicplot(cx,cy,r,s);
  nicplot(cx,-cy,r,s);
end  

amaxis([-4 0 -1 3])
axis square
line([-4 0],[0 0]);
line([-pi -pi],[-1 3]);
set(gca,'Xtick',[-4 -3 -2 -1 0]);
set(gca,'Ytick',[-1 0 1 2 3]);
xxx=logspace(-2,2);
z=-0.5+i*xxx;
plot(-unwrap(imag(log(z))),real(log(z)),'b--')
xlabel('x');ylabel('y');

amprint('nichols-loci.eps');
