% secord_freqresp.m - Frequency response for a second-order system
% RMM, 2008

aminit;					% initialize MATALB

% Generate the responses and plot the eigenvalues
figure(1); clf; subplot(221);

% Set up the axes
axis on; ampolezero(tf([1], [1]), [-2 1 -1.5 1.5]);
set(gca, 'DataAspectRatio', [1 1 1]);

% Compute the locations of the eigenvalues + frequency response
w=logspace(-1,1,1000);

z=0.0001;
  sys0 = tf(1, [1 2*z 1]);
  hold on; eigplot(pole(sys0), 'bx', 'MarkerSize', AM_pzmap_markersize);
  plot([0.125 0.5], [1.05 1.3], 'k-', 'LineWidth', AM_thin_linewidth);
  amtext(0.6, 1.4, 'z = 0'); 
  [a0,p0]=bode(sys0,w);
z=0.08;
  sys = tf(1, [1 2*z 1]);
  eigplot(pole(sys), 'bx', 'MarkerSize', AM_pzmap_markersize);
  plot([-0.125 -0.5], [1.1 1.45], 'k-', 'LineWidth', AM_thin_linewidth);
  amtext(-1.4, 1.6, 'z = 0.08'); 
  [a1,p1]=bode(sys,w);
z=0.2;
  sys = tf(1,[1 2*z 1]);
  eigplot(pole(sys), 'bx', 'MarkerSize', AM_pzmap_markersize);
  plot([-0.325 -0.8], [1 1.25], 'k-', 'LineWidth', AM_thin_linewidth);
  amtext(-1.6, 1.3, 'z = 0.2'); 
  [a2,p2]=bode(sys,w);
z=0.5;
  sys = tf(1,[1 2*z 1]);
  eigplot(pole(sys) + [0.015; -0.015], 'bx', 'MarkerSize', AM_pzmap_markersize);
  plot([-0.575 -1], [0.875 1], 'k-', 'LineWidth', AM_thin_linewidth);
  amtext(-1.8, 1, 'z = 0.5'); 
  [a3,p3]=bode(sys,w);
z=1;
  sys = tf(1,[1 2*z 1]);
  eigplot(pole(sys), 'bx', 'MarkerSize', AM_pzmap_markersize);
  plot([-1.1 -1.575], [0.1 0.35], 'k-', 'LineWidth', AM_thin_linewidth);
  amtext(-2.35, 0.4, 'z = 1'); 
  [a4,p4]=bode(sys,w);

% Plot the eigenvalues
amprint('secord-bodeeigs.eps');

% Plot the frequency response
figure(2); clf; subplot(221);

% Decide on the linewidth to use
linewidth = AM_ref_linewidth;

% Use the first system to generate a standard bode plot
% Reset linewidths to make things look better
old_linewidth = AM_bode_linewidth;
AM_bode_linewidth = linewidth;
[magh, phaseh] = ambode(sys0, w);

% Change linewidths back in case we are running interactive
AM_bode_linewidth = old_linewidth;

% Now add on the lines for the other systems
subplot(magh); hold on; loglog(w, squeeze(a1), 'LineWidth', linewidth);
subplot(phaseh); hold on; semilogx(w, squeeze(p1), 'LineWidth', linewidth);

subplot(magh); hold on; loglog(w, squeeze(a2), 'LineWidth', linewidth);
subplot(phaseh); hold on; semilogx(w, squeeze(p2), 'LineWidth', linewidth);

subplot(magh); hold on; loglog(w, squeeze(a3), 'LineWidth', linewidth);
subplot(phaseh); hold on; semilogx(w, squeeze(p3), 'LineWidth', linewidth);

subplot(magh); hold on; loglog(w, squeeze(a4), 'LineWidth', linewidth);
subplot(phaseh); hold on; semilogx(w, squeeze(p4), 'LineWidth', linewidth);

amprint('secord-bode.eps');
