function [magh, phaseh] = freqplot(mag, phase, omega, magloc, freqloc)
%FREQPLOT   Frequency response plot for a linear system
%
% FREQPLOT(SYS) plots the frequency response of a linear system.  This
% is basically a reimplementation of the Bode plot command, but using
% AM05 unit choices.

% Written by Richard Murray, Fall 2005
%
global AM_bode_linewidth

if (nargin < 4) magloc = 321; end;
if (nargin < 5) freqloc = 323; end;

% Magnitude portion
if (freqloc ~= 0) 
  subplot(magloc);
  magh = loglog(omega, squeeze(mag));
  set(magh, 'LineWidth', AM_bode_linewidth);
  axis([min(omega), max(omega), min(mag), max(mag)]); grid on;

  ylabel('Gain'); xlabel('Frequency [rad/s])');
end

% Phase portion
phase
if (freqloc ~= 0)
  subplot(freqloc);
  phaseh = semilogx(omega, squeeze(phase));
  ylabel('Phase [deg]');
  xlabel('Frequency [rad/s]');
  set(phaseh, 'LineWidth', AM_bode_linewidth);

  % Set the axis limits to something sensible
  phmin = min(phase); phmax = max(phase);
  if (phmax < 0) phmax = 0; end;
  if (phmin > 0) phmin = 0; end;
  if (phmin == phmax) 
    phmax = phmax + 45;
    phmin = phmin - 45;
  end

  axis([min(omega), max(omega), phmin, phmax]); grid on;
end;
