aminit;

%Bode plot of first order system
g='1./(s+1)';
w=logspace(-2,2,500);
w1=logspace(-2,0,20);
w2=logspace(0,2,20);
one=ones(size(w1));
s=i*w;
gv=eval(g);
gain=abs(gv);
ga=min(1,1./w);
phase=180/pi*unwrap(angle(gv));
pa=-min(max(0,45+100000*log(w)),90);

% Generate an initial bode plot that we can draw on top of
figure(1); clf; axes('position', [0.13 0.4096 0.3347 0.5]);
[magh, phaseh] = ambode(tf([1], [1 1]));

subplot(magh); hold on;
loglog(w1, one, 'r--', w2, 1./w2, 'r--', 'LineWidth', AM_thick_linewidth);
loglog(w, gain,'b-', 'Linewidth', AM_ref_linewidth);

amaxis([0.01 100 0.005 200])
set(gca, 'XMinorTick', 'on');
set(gca,'Ytick',[0.01 0.1 1 10 100]);
set(gca,'YtickLabel', {'1m2' '' '1e0' '' '1e2'});

lgh = legend('Exact', 'Approx');
legend(lgh, 'boxoff');

% Now draw the phase
subplot(phaseh); hold on;
semilogx([0.01 0.1 10 100], [0 0 -90 -90], 'r--', ...
  'LineWidth', AM_thick_linewidth);
semilogx(w,phase,'b-', 'Linewidth', AM_ref_linewidth);

amaxis([0.01 100 -180-30 0+30])
set(gca,'Xtick',[0.01 0.1 1 10 100])
set(gca,'XtickLabel', {'a/100' 'a/10' 'a' '10a' '100a'});
set(gca,'Ytick',[-180 -135 -90 -45 0])
set(gca,'YtickLabel', {'-180' '' '-90' '' '0'});

amprint('bode-firstord.eps');

%%
%% Zero
%%

%Bode plot of first order system
g='(s+1)';
w=logspace(-2,2,500);
w1=logspace(-2,0,20);
w2=logspace(0,2,20);
one=ones(size(w1));
s=i*w;
gv=eval(g);
gain=abs(gv);
ga=min(1,1./w);
phase=180/pi*unwrap(angle(gv));
pa=-min(max(0,45+100000*log(w)),90);

% Generate an initial bode plot that we can draw on top of
figure(2); clf; axes('position', [0.13 0.4096 0.3347 0.5]);
[magh, phaseh] = ambode(tf([1 1], [1]));

subplot(magh); hold on;
loglog(w1, one, 'r--', w2, w2, 'r--', 'LineWidth', AM_thick_linewidth);
loglog(w, gain,'b-', 'Linewidth', AM_ref_linewidth);

amaxis([0.01 100 0.005 200])
set(gca, 'XMinorTick', 'on');
set(gca,'Ytick',[0.01 0.1 1 10 100]);
set(gca,'YtickLabel', {'1m2' '' '1e0' '' '1e2'});

lgh = legend('Exact', 'Approx', 'Location', 'southeast');
legend(lgh, 'boxoff');

% Now draw the phase
subplot(phaseh); hold on;
semilogx([0.01 0.1 10 100], [0 0 90 90], 'r--', ...
  'LineWidth', AM_thick_linewidth);
semilogx(w,phase,'b-', 'Linewidth', AM_ref_linewidth);

amaxis([0.01 100 -30 180+30])
set(gca,'Xtick',[0.01 0.1 1 10 100])
set(gca,'XtickLabel', {'a/100' 'a/10' 'a' '10a' '100a'});
set(gca,'Ytick',[0 45 90 135 180])
set(gca,'YtickLabel', {'0' '' '90' '' '180'});

amprint('bode-firstord-zero.eps');
