DT=0.1;
DT1=0.8;
DT2=0.3;

t=0:DT:10;		%Time (high resolution)
f=(sin(t).*t+1).*(t<4.1);
t1=0:DT1:10;	%Time (low resolution)
f1=(sin(t1).*t1+1).*(t1<4.1);
t2=0:DT2:10;	%Time (medium resolution)
f2=(sin(t2).*t2+1).*(t2<4.1);

sys=tf(1,[1 1 1]);

plot(t,f,'b');
xlabel('Time');
ylabel('Input');
title('Input vs Time');
pause;

hold on;
bar(t1,f1,1,'c');
ax=axis;		%Add axis
plot([ax(1) ax(2)],[0 0],'k:')
plot([0 0],[ax(3) ax(4)],'k:')
axis(ax)
bar(t1,f1,1,'c');
title('Input vs Time and discrete approximation');
plot(t,f,'b');
hold off
pause;

plot(t,f,'b');
xlabel('Time');
ylabel('Input');

hold on;
stem(t1,f1,'g');
title('Input vs Time and approximation with impulses');
hold off
pause;

impulse(sys,t);
title('Impulse response of system');
xlabel('Time');
ylabel('Output');
pause

h=impulse(sys,t);		%impulse response of system.
colors='ymcrgb';

imp=zeros(length(t),length(t1));
for i=1:length(t1),
   offset=(i-1)*round(DT1/DT);
   x=h*f1(i)*DT1;
   imp(offset+1:length(t),i)=x(1:length(t)-offset);
   plot(t,imp(:,i),colors(mod(i-1,6)+1));
   hold on;
end
ttl=sprintf('Scaled and Delayed Impulse Response DT=%f',DT1);
title(ttl);
xlabel('Time');
ylabel('Response');
legend('Delay 1','Delay 2','Delay 3','Delay 4','Delay 5','Delay 6')
pause;
approx=sum(imp,2);
plot(t,approx,'k');
gtext('Sum of impulses is shown in black');
pause;
hold off;

imp2=zeros(length(t),length(t1));
for i=1:length(t1),
   offset=(i-1)*round(DT2/DT);
   x=h*f2(i)*DT2;
   imp2(offset+1 :length(t),i)=x(1:length(t)-offset,:);
   plot(t,imp2(:,i),colors(mod(i-1,6)+1));
   hold on;
end
hold off;
ttl=sprintf('Scaled and Delayed Impulse Response DT=%f',DT2);
title(ttl);
xlabel('Time');
ylabel('Response');
pause;

approx2=sum(imp2,2);
soln=lsim(sys,f,t);
plot(t,soln,t,approx,t,approx2);
title('Sum of impulse responses');
xlabel('Time');
ylabel('Response');
legend('Exact (lsim)',sprintf('DT=%f',DT1),sprintf('DT=%f',DT2));
pause;

soln2=conv(f,h)*DT;
plot(t,soln,t,soln2(1:length(t)));
title('Exact response, and using Matlab''s ''conv'' function');
xlabel('Time');
ylabel('Response');
legend('Exact (lsim)','Using ''conv''');

