% ioresp.m - generate sample step and frequency responses
% RMM, 25 Aug 05

aminit;				% initialize MATLAB

%
% This section uses a single linear system to generate all of the plots
% used in the lecture.  This is basically a damped, second order oscillator
% with a first order lag.
%
% Note that some MATLAB commands can take the A, B, C, D matrices directly,
% both others require us to pass a "system" as the argument.  You can create
% a system by using the "ss" function, as shown below.
%

% System definition
A = [-0.2 2 0; -0.5 -0.2 4; 0 0 -10];
B = [0; 0; 1]; C = [2.6 0 0]; D = [0];
sys = ss(A,B,C,D);			% state space object

% Step response (applied at t = 5 sec)
figure(1); clf; subplot(221);
t = 0:0.1:30;
u = [zeros(size(0:0.1:4.9)) ones(size(5:0.1:30))];
[y, x] = lsim(sys, u, t);

% Generate the plot and labels
xy = plot(t, u, '--', t, y, '-');
set(xy, 'LineWidth', AM_data_linewidth);

amaxis;
xlabel('Time'); ylabel('Input, output','Rotation', 90);
lgh = legend('Input', 'Output');
legend(lgh, 'boxoff');

amprint('iostep.eps');

% Frequency response (using AM05 plotting format)
figure(2); clf; subplot(221);
[mh, ph] = ambode(sys);

% Redo the formating on the gain
axes(mh); hold on;
amaxis([0.1 100 1e-5 10]); set(gca, 'Ytick', [1e-4 1e-2 1]);

% Redo the formatting on the phase
axes(ph); hold on;
amaxis([0.1 100 -270 0]);
set(gca, 'Ytick', [-270 -180 -90 0]);
xlabel('Frequency');

amprint('iofreq.eps');
