%TimeResponse_multiple.m
%Effect of pole position. 
%The time response is generated for different pole positions.
%First and second order systems
%Impulse and Step response
%Introduction to:
%commands for zero/pole map
%- pzmap
%commands for system simulation
%- impulse
%- step


clc
type_plot=input('Enter type of response (impulse/step): ','s');
numfig1=0;
Ni=5;
Nj=3;
for i_ind=1:Ni
   i_mod=i_ind-ceil(Ni/2);
   for j_ind=1:Nj
      j_mod=j_ind-1;
      if j_mod==0
         %real poles
         fprintf('\nREAL POLE: \n')
         if i_mod==0
            p1=0;
         else
            p1=sign(i_mod)*10^(abs(i_mod)-1);
         end
         fprintf('\np=%i\n',p1)
         if p1==0
            k=1;
         else
            k=abs(p1);
         end
         sys=tf([k],[1 -p1])
         tit=['REAL POLE: p1=',num2str(p1)];
      elseif i_mod==0
         %pure imaginary poles
         fprintf('\nPURE IMAGINARY POLES: \n')
         p1=(10^(abs(j_mod)-1))*i;
         p2=conj(p1);
         fprintf('\np1=+%ii\np2=-%ii\n',abs(imag(p1)),abs(imag(p1)))
         sys=tf(p1*p2,poly([p1 p2]))
         tit=['PURE IMAGINARY POLES: p1=',num2str(p1),' p2=',num2str(p2)];
      else
         %complex conjugate
         fprintf('\nCOMPLEX CONJUGATE POLES: \n')
         p1=sign(i_mod)*10^(abs(i_mod)-1)+(10^abs(j_mod-1))*i;
         p2=conj(p1);
         fprintf('\np1=%i+%ii\np2=%i-%ii\n',real(p1),abs(imag(p1)),real(p1),abs(imag(p1)))
         sys=tf(p1*p2,poly([p1 p2]))
         tit=['COMPLEX CONJUGATE POLES: p1=',num2str(p1),' p2=',num2str(p2)];
      end
      figure(numfig1+1)
      if strcmp(type_plot,'step')
         [y,t]=step(sys,5);
      end
      if strcmp(type_plot,'impulse')
      	[y,t]=impulse(sys,5);
      end   
      plot(t,y)
      title(tit)
      %if real(p1)<0
      %	axis([0 5 -1 2])
      %end   
      numfig1=gcf;
      figure(21)
      pzmap(sys)
      axis([-10 10 -10 10])
      fprintf('\nPress a key to continue ...\n')
      pause
   end
end

