% nyqcondstab.m - plots for nyquest conditional stability
% RMM, 2 Sep 06 (based on KJA, 26 Aug 04)

aminit;					% initialize macros

%Nyquist curve for conditionally stable system
w=0.3:0.01:100;
s=i*w;
k=3;
g='k*(s+6).^2./(s.*(s+1).^2)';
gv=eval(g);
re=real(gv);
im=imag(gv);
a1=angle(gv(1));
R=abs(gv(1));
fi=a1:0.02:-a1;
x=R*cos(fi);
y=R*sin(fi);
ax=-380:10:380;
null=zeros(size(ax));

figure(1); clf; subplot(221);
ampolezero(tf([1], [1]), [-400 400 -400 400]);
axis square; axis tight; hold on;

% Add a box around the area we are going to expand
plot(6*[-15 2 2 -15 -15], 30*[-0.5 -0.5 0.5 0.5 -0.5], 'r-', ...
  'LineWidth', AM_ref_linewidth);

pl = plot(real(gv), imag(gv), 'b-', real(gv), -imag(gv), 'b--', x, y, 'b-.');
set(pl, 'LineWidth', AM_bode_linewidth);

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

mid = 15;
arrow([real(gv(mid+1)) -imag(gv(mid+1))], [real(gv(mid)) -imag(gv(mid))], ...
  AM_normal_arrowsize, 'FaceColor', color, 'EdgeColor', color);
arrow([real(gv(mid)) imag(gv(mid))], [real(gv(mid+1)) imag(gv(mid+1))], ...
  AM_normal_arrowsize, 'FaceColor', color, 'EdgeColor', color);

arrow([R*cos(-pi/4) R*sin(-pi/4)], [R*cos(-pi/4-0.1) R*sin(-pi/4-0.1)], ...
  AM_normal_arrowsize, 'FaceColor', color, 'EdgeColor', color);
arrow([R*cos(pi/4+0.1) R*sin(pi/4+0.1)], [R*cos(pi/4) R*sin(pi/4)], ...
  AM_normal_arrowsize, 'FaceColor', color, 'EdgeColor', color);

plot(-200, 0, 'k+', 'MarkerSize', 6, 'LineWidth', 0.5);
amtext(-260, -75, '-200');

amprint('nyqcondstab1.eps');

%Nyquist plot for conditionally stable system
%rund nyqcondstb first
%kja 040826

figure(2); clf; subplot(222);
ampolezero(tf([1], [1]), [-15 2 -0.5 0.5]);
axis square; axis tight; hold on;
pl = plot(real(gv),imag(gv),'b-',real(gv),-imag(gv),'b--',x,y,'b-.')

mid = 375;
arrow([real(gv(mid)) imag(gv(mid))], [real(gv(mid+1)) imag(gv(mid+1))], ...
  AM_normal_arrowsize, 'FaceColor', color, 'EdgeColor', color);
arrow([real(gv(mid+1)) -imag(gv(mid+1))], [real(gv(mid)) -imag(gv(mid))], ...
  AM_normal_arrowsize, 'FaceColor', color, 'EdgeColor', color);

mid = 160;
arrow([real(gv(mid)) imag(gv(mid))], [real(gv(mid+1)) imag(gv(mid+1))], ...
  AM_normal_arrowsize, 'FaceColor', color, 'EdgeColor', color);
arrow([real(gv(mid+1)) -imag(gv(mid+1))], [real(gv(mid)) -imag(gv(mid))], ...
  AM_normal_arrowsize, 'FaceColor', color, 'EdgeColor', color);

% Add -1 point
plot(-1, 0, 'k+', 'Linewidth', AM_tick_linewidth);
amtext(-1.7, -0.08, '-1');

amprint('nyqcondstab2.eps');
