%Skteching Bode and Nyquist curves
%   kja 070515
aminit;

% Set up the transfer function along with limits
L = tf([1], [1 2 1]) * tf([1], [1 0.0001]);

% w=logspace(-0.7,2);s=i*w;
% L=1./s./(s+1).^2;
% phi=angle(L(1))
% R=abs(L(1))

%
% Nyquist plot
%
wmax = 10; wmin = 0.2;			% frequency range to use
R = abs(freqresp(L, wmin));		% max radius

figure(1); clf; axes('position', [0 0.5 0.41 0.41]);

% Generate the axes that we want
axis([-1.1*R+1 1.1*R+1 -1.3*R 1.3*R]); daspect(daspect); hold on;

% Generate a plane Nyquist plot without any annotation
AM_nyquist_plain = 1;
amnyquist(L, logspace(log10(wmin), log10(wmax), 100))
clear AM_nyquist_plain;

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

% Add arrows to the portion that we just drew
wmid = 0.3; H1 = freqresp(L, wmid); H2 = freqresp(L, wmid+0.01);
arrow([real(H1) imag(H1)], [real(H2) imag(H2)], AM_normal_arrowsize, ...
  'FaceColor', color, 'EdgeColor', color);
arrow([real(H2) -imag(H2)], [real(H1) -imag(H1)], AM_normal_arrowsize, ...
  'FaceColor', color, 'EdgeColor', color);

% Add an arc around the top
phi = angle(freqresp(L, wmin));
theta = phi:0.01:-phi;
plot(R*cos(theta), R*sin(theta), 'b-.');

% Add arrows to the arc
arrow([R*cos(-pi/3) R*sin(-pi/3)], [R*cos(-pi/3-0.1) R*sin(-pi/3-0.1)], ...
  AM_normal_arrowsize, 'FaceColor', color, 'EdgeColor', color);
arrow([R*cos(pi/3+0.1) R*sin(pi/3+0.1)], [R*cos(pi/3) R*sin(pi/3)], ...
  AM_normal_arrowsize, 'FaceColor', color, 'EdgeColor', color);

% Add axis labels
text(-2, -0.75, '-1');

amprint('sketch-nyquist.eps');

%
% Bode plot
%

figure(2); clf; subplot(221);

% Numerical version for use in constructing crossover points
w = logspace(-1,1,1000); s=i*w;
L = 1./s./(s+1).^2;
gain = abs(L);
phase=180*unwrap(angle(L))/pi;

% Generate the basic bode plot
[magh, phaseh] = ambode(tf([1], [1 0]) * tf([1], [1 1])^2, w);

%
% Annotate the magnitude plot
%
axes(magh); hold on;

% Figure out the gain crossover point
% [xxx, iii] = min(abs(phase+180));
% plot(w(iii), gain(iii), 'ro', 'MarkerSize', AM_bode_markersize);

% Add unity gain line
plot([0.1 10], [1 1], 'k-', 'LineWidth', AM_ref_linewidth);

% Reset the axis information
axis([0.1 10 0.01 10]);
ylabel('g')

% 
% Annotate the phase plot
%
axes(phaseh); hold on;

% Figure out the phase crossover point
% plot(w(iii), phase(iii), 'ro', 'MarkerSize', AM_bode_markersize);

% Reset the axis inforamtion
axis([0.1 10 -270-15 -90+15]);
plot([0.1 10], [-180 -180], 'k-', 'LineWidth', AM_ref_linewidth);
xlabel('w'); ylabel('p');

amprint('sketch-bode.eps');
