%pulses.m Simulation of pulse responses
% kja 060812

aminit;

k0=0.1;k1=0.1;k2=0.5;b0=1.5;
A=[-k0-k1 k1;k2 -k2];B=[b0;0];C=[0 1];
sys1=ss(A,B,C,0);
sys2=ss(A,B,C,0);
t=0:0.01:40;

% Clear previous figures, if they exist
figure(1); clf; figure(2); clf;

for Td=[0.8 2.5 5];
  set(sys2,'InputDelay',Td);
  [y1,t]=step(sys1,t);
  [y2,t]=step(sys2,t);
  ys=1000*(Td-t);
  ys=max(ys,0);ys=min(ys,1/Td);

  figure(1); subplot(321); hold on;
  plot([0; t], [0; ys], 'r-', 'Linewidth', AM_ref_linewidth);

  figure(2); subplot(321); hold on;
  pulh = plot(t, (y1-y2)/Td, 'r--', 'Linewidth', AM_ref_linewidth);
  hold on;
end

% Now plot the actual impulse response
figure(1); subplot(321); hold on;

% set up the color
color = 'b';
if (AMPRINT_FLAG==2) color = 'k'; end;

amaxis([-1 10 0 1.5]);
arrow([0 0], [0 1], AM_normal_arrowsize, 'Width', AM_data_linewidth, ...
  'FaceColor', color, 'EdgeColor', color);
xlabel('t'); ylabel('u', 'Rotation', 0); 
box on;

figure(2); subplot(321); hold on;
yimp=impulse(sys1,t);
imph = plot(t, yimp, 'b-', 'Linewidth', AM_data_linewidth);
amaxis([0 40 0 1]);
lgh = legend([pulh, imph], 'Pulse responses', 'Impulse response');
legend(lgh, 'boxoff');
xlabel('t'); ylabel('y', 'Rotation', 0); 
box on;

figure(1); amprint('pulses.eps');
figure(2); amprint('pulses-response.eps');
