7017
|
1 ## Copyright (C) 1996, 2000, 2002, 2004, 2005, 2006, 2007 |
|
2 ## Auburn University. All rights reserved. |
3432
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
7016
|
7 ## under the terms of the GNU General Public License as published by |
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
3432
|
10 ## |
7016
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
3432
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
3432
|
19 |
|
20 ## -*- texinfo -*- |
5016
|
21 ## @deftypefn {Function File} {[@var{y}, @var{x}] =} lsim (@var{sys}, @var{u}, @var{t}, @var{x0}) |
|
22 ## Produce output for a linear simulation of a system; produces |
|
23 ## a plot for the output of the system, @var{sys}. |
3432
|
24 ## |
5016
|
25 ## @var{u} is an array that contains the system's inputs. Each row in @var{u} |
|
26 ## corresponds to a different time step. Each column in @var{u} corresponds to a |
|
27 ## different input. @var{t} is an array that contains the time index of the |
|
28 ## system; @var{t} should be regularly spaced. If initial conditions are required |
|
29 ## on the system, the @var{x0} vector should be added to the argument list. |
3432
|
30 ## |
5016
|
31 ## When the lsim function is invoked a plot is not displayed; |
|
32 ## however, the data is returned in @var{y} (system output) |
|
33 ## and @var{x} (system states). |
3432
|
34 ## @end deftypefn |
|
35 |
|
36 ## Author: David Clem |
|
37 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu> |
|
38 ## Created: July 1995 |
|
39 ## modified by John Ingram for system format August 1996 |
|
40 |
|
41 function [y, x] = lsim (sys, u, t, x0) |
|
42 |
7126
|
43 if (nargin < 3 || nargin > 4) |
6046
|
44 print_usage (); |
3432
|
45 endif |
|
46 |
7126
|
47 if (! isstruct (sys)) |
|
48 error ("sys must be in system data structure"); |
3432
|
49 endif |
|
50 |
7126
|
51 sys = sysupdate (sys,"ss"); |
3432
|
52 |
7126
|
53 [ncstates, ndstates, nin, nout] = sysdimensions (sys); |
|
54 [a, b, c, d] = sys2ss (sys); |
3432
|
55 |
7126
|
56 if (nargin == 3) |
|
57 x0 = zeros (columns (a), 1); |
|
58 endif |
3432
|
59 |
7126
|
60 if (rows (u) != length (t)) |
|
61 error ("lsim: There should be an input value (row) for each time instant"); |
3432
|
62 endif |
7126
|
63 if (columns (u) != columns (d)) |
|
64 error ("lsim: U and d should have the same number of inputs"); |
3432
|
65 endif |
7126
|
66 if (columns (x0) > 1) |
|
67 error ("lsim: Initial condition vector should have only one column"); |
3432
|
68 endif |
7126
|
69 if (rows (x0) > rows p(a)) |
|
70 error ("lsim: Initial condition vector is too large"); |
3432
|
71 endif |
|
72 |
|
73 Ts = 0; |
|
74 t(2)-t(1); |
|
75 u=u'; |
7126
|
76 n = max (size (t)); |
4174
|
77 |
3432
|
78 for ii = 1:(n-1) |
|
79 |
|
80 ## check if step size changed |
5775
|
81 ## FIXME -- this is probably not the best test, but it is |
4174
|
82 ## better than a test for exact equality. |
|
83 if (abs (t(ii+1) - t(ii) - Ts) > 10 * eps) |
3432
|
84 Ts = t(ii+1) - t(ii); |
|
85 ## [F,G] = c2d(a,b,Ts); |
7126
|
86 dsys = c2d (sys, Ts); |
|
87 [F, G] = sys2ss (dsys); |
3432
|
88 endif |
|
89 |
|
90 x(:,ii) = x0; |
|
91 x0 = F*x0 + G*u(:,ii); |
|
92 endfor |
|
93 |
|
94 ## pick up last point |
|
95 x(:,n) = x0; |
|
96 |
|
97 y = c*x + d*u; |
7126
|
98 if (nargout == 0) |
|
99 plot (t, y); |
|
100 y = []; |
|
101 x = []; |
3432
|
102 endif |
7126
|
103 |
3432
|
104 endfunction |