home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 10.ddi / CONTROL.DI$ / DIMPULSE.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  2.7 KB  |  96 lines

  1. function [yout,x,n] = dimpulse(a,b,c,d,iu,n)
  2. %DIMPULSE Impulse response of discrete-time linear systems.
  3. %     DIMPULSE(A,B,C,D,IU)  plots the response of the discrete system:
  4. %
  5. %        x[n+1] = Ax[n] + Bu[n]
  6. %        y[n]   = Cx[n] + Du[n]
  7. %
  8. %    to an unit sample applied to the single input IU.  The number of
  9. %    points is determined automatically.
  10. %
  11. %    DIMPULSE(NUM,DEN)  plots the impulse response of the polynomial
  12. %    transfer function  G(z) = NUM(z)/DEN(z)  where NUM and DEN contain
  13. %    the polynomial coefficients in descending powers of z.
  14. %
  15. %    DIMPULSE(A,B,C,D,IU,N) or DIMPULSE(NUM,DEN,N) uses the user-
  16. %    supplied number of points, N.  When invoked with left hand 
  17. %    arguments,
  18. %        [Y,X] = DIMPULSE(A,B,C,D,...)
  19. %        [Y,X] = DIMPULSE(NUM,DEN,...)
  20. %    returns the output and state time history in the matrices Y and X.
  21. %    No plot is drawn on the screen.  Y has as many columns as there 
  22. %    are outputs and X has as many columns as there are states.
  23. %
  24. %    See also: DSTEP,DINITIAL,DLSIM and IMPULSE.
  25.  
  26. %    J.N. Little 4-21-85
  27. %    Revised CMT 7-31-90, ACWG 5-30-91 
  28. %    Copyright (c) 1986-93 by the MathWorks, Inc.
  29.  
  30. if nargin==0, eval('exresp(''dimpulse'');'), return,end
  31.  
  32. error(nargchk(2,6,nargin));
  33.  
  34. if (nargin==2),        % Transfer function without number of points
  35.     [num,den] = tfchk(a,b);
  36.     iu = 1;
  37.     [a,b,c,d] = tf2ss(num,den);
  38.  
  39. elseif (nargin==3),    % Transfer function with number of points
  40.     [num,den] = tfchk(a,b);
  41.     n = c;
  42.     iu = 1;
  43.     [a,b,c,d] = tf2ss(num,den);
  44.  
  45. elseif (nargin>=4)
  46.     error(abcdchk(a,b,c,d));
  47.  
  48. end
  49.  
  50. [ny,nu] = size(d);
  51. if (nu*ny==0), x = []; n = []; if nargout~=0, yout=[]; end, return, end
  52.  
  53. % Work out number of points if not supplied.
  54. if nargin==4 | nargin==5 | nargin==2,
  55.     if isempty(a),
  56.         n = 3;
  57.     else
  58.         % The next line controls the number of samples in the plot if N not specified
  59.         st=0.001; % Set settling time bound  = 0.1%
  60.         [n,m]=size(b);
  61.         x0=(eye(n,n)-a)\(b*ones(m,1));
  62.  
  63.         % Cater for pure integrator case
  64.         infind=find(~finite(x0));
  65.         x0(infind)=ones(length(infind),1);
  66.         n=dtimvec(a,b,c,x0,st);
  67.     end
  68.     if nargin==4            % Multivariable systems
  69.         [iu,nargin,y]=mulresp('dimpulse',a,b,c,d,n,nargout,0);
  70.         if ~iu, if nargout, yout = y; end, return, end
  71.     end
  72. end
  73.  
  74. if (nargin <= 3)&(nargout <= 1),    % Transfer function description
  75.     y = dlsim(num,den,[1;zeros(n-1,1)]);    % More efficient: uses FILTER
  76. else
  77.     if ~isempty(b), b=b(:,iu); end, d = d(:,iu);
  78.     [y,x] = dlsim(a,b,c,d,[1;zeros(n-1,1)]);
  79. end
  80.  
  81. if nargout==0,        % With no output arguments, plot graph
  82.     status = ishold;
  83.  
  84.     stairs([0:n-1],y)
  85.     hold on
  86.     plot([0,n-1],[0;0],':')
  87.  
  88.     xlabel('No. of Samples')
  89.     ylabel('Amplitude')
  90.  
  91.     if ~status, hold off, end    % Return hold to previous status
  92.     return % Suppress output
  93. end
  94. yout = y;
  95.  
  96.