annotate scripts/control/base/lsim.m @ 7126:4a375de63f66

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