%
%  this routine allows the user to compare bode plots
%  
%  Inputs: data = vector of frequency (in Hz) and Magnitude (not in dB)
%             K       = static gain
%             omega_n = estimated natural frequency
%             zeta    = estiamted damping ratio
%
function fit_bode(data,K,omega_n,zeta);
%
%  Construct the transfer function
%
  G = tf(K,[1/omega_n^2 2*zeta/omega_n 1]);

%  extract the data
%
  f = data(:,1);
  M = data(:,2);
%
%  convert to dB and rad/sec
%
  MdB = 20*log10(M);  
  w = 2*pi*f;
%
  omega_low = min(w);
  omega_high = max(w);
  omega = logspace(log10(omega_low),log10(omega_high),1e3);
%
  [estimated_M,estimated_P,omega2] = bode(G,omega);
%
% Get the values for the estimated transfer functions
%
  eM = reshape(estimated_M, length(estimated_M),1);
%
%  convert to dB
%
  eMdB = 20*log10(eM);
%
%  Now plot

  semilogx(w,MdB,'d',omega,eMdB,'-');
%
%  change the axes to focus on our data
%
  min_mag = min(min(eMdB),min(MdB));
  max_mag = max(max(eMdB),max(MdB));
  axis([floor(min(w)) floor(max(w))+2 min_mag max_mag ]);
%
%  put on a legend
%
  legend('Measured','Estimated',2); grid; ylabel('Magnitude (dB)'); xlabel('Frequency (rad/sec)')
  title(['TF Fit To Bode Plot:  Gain (K) = ' num2str(K) ', \omega_n = ' num2str(omega_n) ', \zeta = ' num2str(zeta)]);
%
  return;