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 |
|
17 ## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. |
|
18 |
|
19 ## -*- texinfo -*- |
3500
|
20 ## @deftypefn {Function File} {} lsim (@var{sys}, @var{u}, @var{t}, @var{x0}) |
3432
|
21 ## Produce output for a linear simulation of a system |
|
22 ## |
|
23 ## Produces a plot for the output of the system, sys. |
|
24 ## |
4150
|
25 ## U is an array that contains the system's inputs. Each row in u |
|
26 ## corresponds to a different time step. Each column in u corresponds to a |
3432
|
27 ## different input. T is an array that contains the time index of the |
|
28 ## system. T should be regularly spaced. If initial conditions are required |
|
29 ## on the system, the x0 vector should be added to the argument list. |
|
30 ## |
|
31 ## When the lsim function is invoked with output parameters: |
|
32 ## [y,x] = lsim(sys,u,t,[x0]) |
|
33 ## a plot is not displayed, however, the data is returned in y = system output |
|
34 ## and x = system states. |
|
35 ## @end deftypefn |
|
36 |
|
37 ## Author: David Clem |
|
38 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu> |
|
39 ## Created: July 1995 |
|
40 ## modified by John Ingram for system format August 1996 |
|
41 |
|
42 function [y, x] = lsim (sys, u, t, x0) |
|
43 |
|
44 if((nargin < 3)||(nargin > 4)) |
|
45 usage("[y,x] = lsim(sys,u,t[,x0])"); |
|
46 endif |
|
47 |
4030
|
48 if(!isstruct(sys)) |
3432
|
49 error("sys must be in system data structure"); |
|
50 endif |
|
51 |
|
52 sys = sysupdate(sys,"ss"); |
|
53 |
|
54 [ncstates, ndstates, nin, nout] = sysdimensions(sys); |
|
55 [a,b,c,d] = sys2ss(sys); |
|
56 |
|
57 if (nargin == 3) x0 = zeros(columns(a),1); endif |
|
58 |
|
59 if(rows(u) ~= length(t)) |
|
60 error("lsim: There should be an input value (row) for each time instant"); |
|
61 endif |
|
62 if(columns(u) ~= columns(d)) |
|
63 error("lsim: U and d should have the same number of inputs"); |
|
64 endif |
|
65 if(columns(x0) > 1) |
|
66 error("lsim: Initial condition vector should have only one column"); |
|
67 endif |
|
68 if(rows(x0) > rows(a)) |
|
69 error("lsim: Initial condition vector is too large"); |
|
70 endif |
|
71 |
|
72 Ts = 0; |
|
73 t(2)-t(1); |
|
74 u=u'; |
|
75 n = max(size(t)); |
4174
|
76 |
3432
|
77 for ii = 1:(n-1) |
|
78 |
|
79 ## check if step size changed |
4174
|
80 ## XXX FIXME XXX -- this is probably not the best test, but it is |
|
81 ## better than a test for exact equality. |
|
82 if (abs (t(ii+1) - t(ii) - Ts) > 10 * eps) |
3432
|
83 Ts = t(ii+1) - t(ii); |
|
84 ## [F,G] = c2d(a,b,Ts); |
|
85 dsys = c2d(sys, Ts); |
|
86 [F,G] = sys2ss(dsys); |
|
87 endif |
|
88 |
|
89 x(:,ii) = x0; |
|
90 x0 = F*x0 + G*u(:,ii); |
|
91 endfor |
|
92 |
|
93 ## pick up last point |
|
94 x(:,n) = x0; |
|
95 |
|
96 y = c*x + d*u; |
|
97 if(nargout == 0) |
|
98 plot(t,y); |
|
99 y=[]; |
|
100 x=[]; |
|
101 endif |
|
102 endfunction |