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

  1. function  [yout,x] = dlsim(a, b, c, d, u, x0)
  2. %DLSIM    Simulation of discrete-time linear systems.
  3. %    DLSIM(A,B,C,D,U)  plots the time response of the discrete system:
  4. %
  5. %        x[n+1] = Ax[n] + Bu[n]
  6. %        y[n]   = Cx[n] + Du[n]
  7. %
  8. %    to input sequence U.  Matrix U must have as many columns as there
  9. %    are inputs, u.  Each row of U corresponds to a new time point.
  10. %    DLSIM(A,B,C,D,U,X0) can be used if initial conditions exist.
  11. %
  12. %    DLSIM(NUM,DEN,U) plots the time response of the transfer function
  13. %    description  G(z) = NUM(z)/DEN(z)  where NUM and DEN contain the 
  14. %    polynomial coefficients in descending powers of z.  If 
  15. %    LENGTH(NUM)=LENGTH(DEN) then DLSIM(NUM,DEN,U) is equivalent to 
  16. %    FILTER(NUM,DEN,U).  When invoked with left hand arguments,
  17. %        [Y,X] = DLSIM(A,B,C,D,U)
  18. %        [Y,X] = DLSIM(NUM,DEN,U)
  19. %    returns the output and state time history in the matrices Y and X.
  20. %    No plot is drawn on the screen.  Y has as many columns as there 
  21. %    are outputs and LENGTH(U) rows.  X has as many columns as there 
  22. %    are states and LENGTH(U) rows. 
  23. %
  24. %    See also: DSTEP,DIMPULSE,DINITIAL and LSIM.
  25.  
  26. %    J.N. Little 4-21-85
  27. %    Revised 7-18-88 JNL
  28. %    Revised 7-31-90  Clay M. Thompson
  29. %    Copyright (c) 1986-93 by the MathWorks, Inc.
  30.  
  31. error(nargchk(3,6,nargin));
  32. if (nargin==4), error('Wrong number of input arguments.'); end
  33.  
  34. nargs = nargin;
  35.  
  36. if (nargs == 3)        % transfer function description 
  37.   [num,den] = tfchk(a,b);
  38.   u = c(:);
  39.   [m,n] = size(num);
  40.   if ((m == 1)&(nargout == 1))    % Use filter, it's more efficient
  41.     y = filter(num,den,u);
  42.     yout = y; 
  43.     return
  44.   else              % Convert to state space
  45.     [a,b,c,d] = tf2ss(num,den);
  46.     nargs = 5;
  47.   end
  48. end
  49.  
  50. [ny,nu] = size(d);
  51. if ny*ny==0, x = []; if nargout~=0, yout = []; end, return, end
  52.  
  53. [ns,nx] = size(a);
  54. if (nargs == 5)
  55.   x0 = zeros(1,ns);
  56. end
  57. error(abcdchk(a,b,c,d));
  58. if min(size(u)) == 1
  59.   u = u(:);    % Make sure u is a column vector
  60. end
  61.  
  62. % Make sure u is has the right number of columns
  63. [n,mu]=size(u);
  64. if mu~=nu, error('U must have same number of columns as inputs.'); end
  65.  
  66. if isempty(a),
  67.   x = [];
  68.   y = u * d.';
  69. else
  70.   x = ltitr(a,b,u,x0);
  71.   y = x * c.' + u * d.';
  72. end
  73.  
  74. if nargout==0,    % With no output arguments, plot graph
  75.   t = [0:length(y)-1];
  76.   stairs(t,y)
  77.   xlabel('No. of Samples'), ylabel('Amplitude')
  78.  
  79.   return  % Suppress output
  80. end
  81.  
  82. yout = y; 
  83.  
  84.