annotate scripts/control/base/lsim.m @ 4174:aabb8307021f

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