3432
|
1 ## Copyright (C) 1996 Auburn University. All rights reserved. |
|
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 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 ## for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301 USA. |
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 |
|
43 if((nargin < 3)||(nargin > 4)) |
|
44 usage("[y,x] = lsim(sys,u,t[,x0])"); |
|
45 endif |
|
46 |
4030
|
47 if(!isstruct(sys)) |
3432
|
48 error("sys must be in system data structure"); |
|
49 endif |
|
50 |
|
51 sys = sysupdate(sys,"ss"); |
|
52 |
|
53 [ncstates, ndstates, nin, nout] = sysdimensions(sys); |
|
54 [a,b,c,d] = sys2ss(sys); |
|
55 |
|
56 if (nargin == 3) x0 = zeros(columns(a),1); endif |
|
57 |
|
58 if(rows(u) ~= length(t)) |
|
59 error("lsim: There should be an input value (row) for each time instant"); |
|
60 endif |
|
61 if(columns(u) ~= columns(d)) |
|
62 error("lsim: U and d should have the same number of inputs"); |
|
63 endif |
|
64 if(columns(x0) > 1) |
|
65 error("lsim: Initial condition vector should have only one column"); |
|
66 endif |
|
67 if(rows(x0) > rows(a)) |
|
68 error("lsim: Initial condition vector is too large"); |
|
69 endif |
|
70 |
|
71 Ts = 0; |
|
72 t(2)-t(1); |
|
73 u=u'; |
|
74 n = max(size(t)); |
4174
|
75 |
3432
|
76 for ii = 1:(n-1) |
|
77 |
|
78 ## check if step size changed |
4174
|
79 ## XXX FIXME XXX -- this is probably not the best test, but it is |
|
80 ## better than a test for exact equality. |
|
81 if (abs (t(ii+1) - t(ii) - Ts) > 10 * eps) |
3432
|
82 Ts = t(ii+1) - t(ii); |
|
83 ## [F,G] = c2d(a,b,Ts); |
|
84 dsys = c2d(sys, Ts); |
|
85 [F,G] = sys2ss(dsys); |
|
86 endif |
|
87 |
|
88 x(:,ii) = x0; |
|
89 x0 = F*x0 + G*u(:,ii); |
|
90 endfor |
|
91 |
|
92 ## pick up last point |
|
93 x(:,n) = x0; |
|
94 |
|
95 y = c*x + d*u; |
|
96 if(nargout == 0) |
|
97 plot(t,y); |
|
98 y=[]; |
|
99 x=[]; |
|
100 endif |
|
101 endfunction |