home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / control / lsim.m < prev    next >
Text File  |  1999-03-05  |  3KB  |  96 lines

  1. # Copyright (C) 1996 A. Scottedward Hodel 
  2. #
  3. # This file is part of Octave. 
  4. #
  5. # Octave is free software; you can redistribute it and/or modify it 
  6. # under the terms of the GNU General Public License as published by the 
  7. # Free Software Foundation; either version 2, or (at your option) any 
  8. # later version. 
  9. # Octave is distributed in the hope that it will be useful, but WITHOUT 
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  11. # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
  12. # for more details.
  13. # You should have received a copy of the GNU General Public License 
  14. # along with Octave; see the file COPYING.  If not, write to the Free 
  15. # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  16.  
  17. function [y,x] = lsim(sys,u,t,x0)
  18. # lsim: Produce output for a linear simulation of a system
  19. #
  20. # lsim(sys,u,t,[x0])
  21. # Produces a plot for the output of the system, sys.
  22. #
  23. # U is an array that contains the system's inputs.  Each column in u 
  24. # corresponds to a different time step.  Each row in u corresponds to a 
  25. # different input.  T is an array that contains the time index of the 
  26. # system.  T should be regularly spaced.  If initial conditions are required
  27. # on the system, the x0 vector should be added to the argument list.
  28. #
  29. # When the lsim function is invoked with output parameters:
  30. # [y,x] = lsim(sys,u,t,[x0])
  31. # a plot is not displayed, however, the data is returned in y = system output
  32. # and x = system states.
  33.  
  34. # Written by David Clem, A. S. Hodel July 1995
  35. # modified by John Ingram for system format August 1996
  36.  
  37.  
  38.   if((nargin < 3)||(nargin > 4))
  39.     usage("[y,x] = lsim(sys,u,t[,x0])");
  40.   endif
  41.  
  42.   if(!is_struct(sys))
  43.     error("sys must be in system data structure");
  44.   endif
  45.  
  46.   sys = sysupdate(sys,"ss");
  47.  
  48.   [ncstates, ndstates, nin, nout] = sysdimensions(sys);
  49.   [a,b,c,d] = sys2ss(sys);
  50.   
  51.   if (nargin == 3)     x0 = zeros(columns(a),1);        endif
  52.  
  53.   if(rows(u) ~= length(t))
  54.     error("lsim: There should be an input value (row) for each time instant");
  55.   endif
  56.   if(columns(u) ~= columns(d))
  57.     error("lsim: U and d should have the same number of inputs");
  58.   endif
  59.   if(columns(x0) > 1)
  60.     error("lsim: Initial condition vector should have only one column");
  61.   endif
  62.   if(rows(x0) > rows(a))
  63.     error("lsim: Initial condition vector is too large");
  64.   endif
  65.  
  66.   Ts = 0;
  67.   t(2)-t(1);
  68.   u=u';
  69.   n = max(size(t));
  70.   for ii = 1:(n-1)
  71.  
  72.     # check if step size changed
  73.     if (t(ii+1) - t(ii) != Ts)
  74.       Ts = t(ii+1) - t(ii);
  75.       # [F,G] = c2d(a,b,Ts);
  76.       dsys = c2d(sys, Ts);
  77.       [F,G] = sys2ss(dsys);
  78.     endif
  79.  
  80.     x(:,ii) = x0;
  81.     x0 = F*x0 + G*u(:,ii);
  82.   endfor
  83.  
  84.   # pick up last point
  85.   x(:,n) = x0;
  86.  
  87.   y = c*x + d*u;
  88.   if(nargout == 0)
  89.    plot(t,y);
  90.    y=[];
  91.    x=[];
  92.   endif
  93. endfunction
  94.