7017
|
1 ## Copyright (C) 1996, 1998, 2004, 2005, 2006, 2007 |
|
2 ## Auburn University. All rights reserved. |
4779
|
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. |
4779
|
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. |
4779
|
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/>. |
4779
|
19 |
|
20 ## -*- texinfo -*- |
5016
|
21 ## @deftypefn {Function File} {@var{outsys} =} ss (@var{a}, @var{b}, @var{c}, @var{d}, @var{tsam}, @var{n}, @var{nz}, @var{stname}, @var{inname}, @var{outname}, @var{outlist}) |
7001
|
22 ## Create system structure from state-space data. May be continuous, |
5016
|
23 ## discrete, or mixed (sampled data) |
4779
|
24 ## |
|
25 ## @strong{Inputs} |
|
26 ## @table @var |
|
27 ## @item a |
|
28 ## @itemx b |
|
29 ## @itemx c |
|
30 ## @itemx d |
|
31 ## usual state space matrices. |
|
32 ## |
|
33 ## default: @var{d} = zero matrix |
|
34 ## |
|
35 ## @item tsam |
|
36 ## sampling rate. Default: @math{tsam = 0} (continuous system) |
|
37 ## |
|
38 ## @item n |
|
39 ## @itemx nz |
|
40 ## number of continuous, discrete states in the system |
|
41 ## |
|
42 ## If @var{tsam} is 0, @math{n = @code{rows}(@var{a})}, @math{nz = 0}. |
|
43 ## |
|
44 ## If @var{tsam} is greater than zero, @math{n = 0}, |
|
45 ## @math{nz = @code{rows}(@var{a})} |
|
46 ## |
|
47 ## see below for system partitioning |
|
48 ## |
|
49 ## @item stname |
4844
|
50 ## cell array of strings of state signal names |
4779
|
51 ## |
|
52 ## default (@var{stname}=[] on input): @code{x_n} for continuous states, |
|
53 ## @code{xd_n} for discrete states |
|
54 ## |
|
55 ## @item inname |
4844
|
56 ## cell array of strings of input signal names |
4779
|
57 ## |
|
58 ## default (@var{inname} = [] on input): @code{u_n} |
|
59 ## |
|
60 ## @item outname |
5573
|
61 ## cell array of strings of output signal names |
4779
|
62 ## |
|
63 ## default (@var{outname} = [] on input): @code{y_n} |
|
64 ## |
|
65 ## @item outlist |
|
66 ## |
|
67 ## list of indices of outputs y that are sampled |
|
68 ## |
|
69 ## If @var{tsam} is 0, @math{outlist = []}. |
|
70 ## |
|
71 ## If @var{tsam} is greater than 0, @math{outlist = 1:@code{rows}(@var{c})}. |
|
72 ## @end table |
|
73 ## |
7001
|
74 ## Unlike states, discrete/continuous outputs may appear in any order. |
4779
|
75 ## |
4946
|
76 ## @code{sys2ss} returns a vector @var{yd} where |
4779
|
77 ## @var{yd}(@var{outlist}) = 1; all other entries of @var{yd} are 0. |
|
78 ## |
5016
|
79 ## @strong{Output} |
|
80 ## @table @var |
|
81 ## @item outsys |
|
82 ## system data structure |
|
83 ## @end table |
4779
|
84 ## |
|
85 ## @strong{System partitioning} |
|
86 ## |
|
87 ## Suppose for simplicity that outlist specified |
|
88 ## that the first several outputs were continuous and the remaining outputs |
|
89 ## were discrete. Then the system is partitioned as |
|
90 ## @example |
|
91 ## @group |
|
92 ## x = [ xc ] (n x 1) |
|
93 ## [ xd ] (nz x 1 discrete states) |
|
94 ## a = [ acc acd ] b = [ bc ] |
|
95 ## [ adc add ] [ bd ] |
|
96 ## c = [ ccc ccd ] d = [ dc ] |
|
97 ## [ cdc cdd ] [ dd ] |
|
98 ## |
|
99 ## (cdc = c(outlist,1:n), etc.) |
|
100 ## @end group |
|
101 ## @end example |
|
102 ## with dynamic equations: |
|
103 ## @ifinfo |
|
104 ## @math{d/dt xc(t) = acc*xc(t) + acd*xd(k*tsam) + bc*u(t)} |
|
105 ## |
|
106 ## @math{xd((k+1)*tsam) = adc*xc(k*tsam) + add*xd(k*tsam) + bd*u(k*tsam)} |
|
107 ## |
|
108 ## @math{yc(t) = ccc*xc(t) + ccd*xd(k*tsam) + dc*u(t)} |
|
109 ## |
|
110 ## @math{yd(k*tsam) = cdc*xc(k*tsam) + cdd*xd(k*tsam) + dd*u(k*tsam)} |
|
111 ## @end ifinfo |
|
112 ## @iftex |
|
113 ## @tex |
|
114 ## $$\eqalign{ |
|
115 ## {d \over dt} x_c(t) |
|
116 ## & = a_{cc} x_c(t) + a_{cd} x_d(k*t_{sam}) + bc*u(t) \cr |
|
117 ## x_d((k+1)*t_{sam}) |
|
118 ## & = a_{dc} x_c(k t_{sam}) + a_{dd} x_d(k t_{sam}) + b_d u(k t_{sam}) \cr |
|
119 ## y_c(t) |
|
120 ## & = c_{cc} x_c(t) + c_{cd} x_d(k t_{sam}) + d_c u(t) \cr |
|
121 ## y_d(k t_{sam}) |
|
122 ## & = c_{dc} x_c(k t_{sam}) + c_{dd} x_d(k t_{sam}) + d_d u(k t_{sam}) |
|
123 ## }$$ |
|
124 ## @end tex |
|
125 ## @end iftex |
|
126 ## |
|
127 ## @strong{Signal partitions} |
|
128 ## @example |
|
129 ## @group |
|
130 ## | continuous | discrete | |
|
131 ## ---------------------------------------------------- |
|
132 ## states | stname(1:n,:) | stname((n+1):(n+nz),:) | |
|
133 ## ---------------------------------------------------- |
|
134 ## outputs | outname(cout,:) | outname(outlist,:) | |
|
135 ## ---------------------------------------------------- |
|
136 ## @end group |
|
137 ## @end example |
|
138 ## where @math{cout} is the list of in 1:@code{rows}(@var{p}) |
|
139 ## that are not contained in outlist. (Discrete/continuous outputs |
|
140 ## may be entered in any order desired by the user.) |
|
141 ## |
|
142 ## @strong{Example} |
|
143 ## @example |
|
144 ## octave:1> a = [1 2 3; 4 5 6; 7 8 10]; |
|
145 ## octave:2> b = [0 0 ; 0 1 ; 1 0]; |
4855
|
146 ## octave:3> c = eye (3); |
7031
|
147 ## octave:4> sys = ss (a, b, c, [], 0, 3, 0, ... |
|
148 ## > @{"volts", "amps", "joules"@}); |
4779
|
149 ## octave:5> sysout(sys); |
|
150 ## Input(s) |
|
151 ## 1: u_1 |
|
152 ## 2: u_2 |
|
153 ## |
|
154 ## Output(s): |
|
155 ## 1: y_1 |
|
156 ## 2: y_2 |
|
157 ## 3: y_3 |
|
158 ## |
|
159 ## state-space form: |
|
160 ## 3 continuous states, 0 discrete states |
|
161 ## State(s): |
|
162 ## 1: volts |
|
163 ## 2: amps |
|
164 ## 3: joules |
|
165 ## |
|
166 ## A matrix: 3 x 3 |
|
167 ## 1 2 3 |
|
168 ## 4 5 6 |
|
169 ## 7 8 10 |
|
170 ## B matrix: 3 x 2 |
|
171 ## 0 0 |
|
172 ## 0 1 |
|
173 ## 1 0 |
|
174 ## C matrix: 3 x 3 |
|
175 ## 1 0 0 |
|
176 ## 0 1 0 |
|
177 ## 0 0 1 |
|
178 ## D matrix: 3 x 3 |
|
179 ## 0 0 |
|
180 ## 0 0 |
|
181 ## 0 0 |
|
182 ## @end example |
|
183 ## Notice that the @math{D} matrix is constructed by default to the |
|
184 ## correct dimensions. Default input and output signals names were assigned |
|
185 ## since none were given. |
|
186 ## @end deftypefn |
|
187 |
|
188 ## Author: John Ingram <ingraje@eng.auburn.edu> |
|
189 ## Created: July 20, 1996 |
|
190 |
|
191 function retsys = ss (a, b, c, d, tsam, n, nz, stname, inname, outname, outlist) |
|
192 |
|
193 ## Test for correct number of inputs |
7135
|
194 if (nargin < 3 || nargin > 11) |
6046
|
195 print_usage (); |
4779
|
196 endif |
|
197 |
|
198 ## verify A, B, C, D arguments |
|
199 ## If D is not specified, set it to a zero matrix of appriate dimension. |
7135
|
200 if (nargin == 3) |
|
201 d = zeros (rows (c), columns (b)); |
|
202 elseif (isempty (d)) |
|
203 d = zeros (rows (c), columns (b)); |
|
204 endif |
4779
|
205 |
|
206 ## Check the dimensions |
7135
|
207 [na, m, p] = abcddim (a, b, c, d); |
4779
|
208 |
|
209 ## If dimensions are wrong, exit function |
|
210 if (m == -1) |
7135
|
211 error ("a(%dx%d), b(%dx%d), c(%dx%d), d(%dx%d); incompatible", |
|
212 rows (a), columns (a), rows (b), columns (b), |
|
213 rows (c), columns (c), rows (d), columns (d)); |
4779
|
214 endif |
|
215 |
|
216 ## check for tsam input |
7137
|
217 if (nargin < 5) |
|
218 tsam = 0; |
|
219 elseif (! (is_sample (tsam) || tsam == 0)) |
7135
|
220 error ("tsam must be a nonnegative real scalar"); |
4779
|
221 endif |
|
222 |
|
223 ## check for continuous states |
7135
|
224 if (nargin < 6 && tsam == 0) |
|
225 n = na; |
|
226 elseif (nargin < 6) |
|
227 n = 0; |
|
228 elseif (! ismatrix (n) || ischar (n)) |
|
229 error ("Parameter n is not a numerical value."); |
|
230 elseif (! isscalar(n) || n < 0 || n != round (n)) |
|
231 if (isscalar (n)) |
|
232 error ("invalid value of n=%d,%e", n, n); |
|
233 else |
|
234 error ("invalid value of n=(%dx%d)", rows (n), columns (n)); |
|
235 endif |
4779
|
236 endif |
|
237 |
|
238 ## check for num discrete states |
7135
|
239 if (nargin < 7 && tsam == 0) |
|
240 nz = 0; |
|
241 elseif (nargin < 7) |
|
242 nz = na - n; |
|
243 elseif (! ismatrix(nz) || ischar (nz)) |
|
244 error ("Parameter nz is not a numerical value."); |
|
245 elseif (! isscalar(nz) || nz < 0 || nz != round(nz)) |
|
246 if (isscalar (nz)) |
|
247 error ("invalid value of nz=%d", nz); |
4779
|
248 else |
7135
|
249 error ("invalid value of nz=(%d,%d)", rows (nz), columns (nz)); |
4779
|
250 endif |
|
251 endif |
|
252 |
|
253 ## check for total number of states |
7135
|
254 if ((n + nz) != na) |
|
255 error ("invalid: a is %dx%d, n=%d, nz=%d", na, na, n, nz); |
4779
|
256 endif |
|
257 |
|
258 ## construct system with default names |
|
259 retsys.a = a; |
|
260 retsys.b = b; |
|
261 retsys.c = c; |
|
262 retsys.d = d; |
|
263 |
|
264 retsys.n = n; |
|
265 retsys.nz = nz; |
|
266 retsys.tsam = tsam; |
7135
|
267 retsys.yd = zeros (1, p); # default value entered below |
4779
|
268 |
|
269 ## Set the system vector: active = 2(ss), updated = [0 0 1]; |
|
270 retsys.sys = [2, 0, 0, 1]; |
|
271 |
|
272 retsys.stname = __sysdefstname__ (n, nz); |
|
273 retsys.inname = __sysdefioname__ (m, "u"); |
|
274 retsys.outname = __sysdefioname__ (p, "y"); |
|
275 |
|
276 ## check for state names |
7135
|
277 if (nargin >= 8) |
|
278 if (! isempty (stname)) |
|
279 retsys = syssetsignals (retsys, "st", stname); |
|
280 endif |
4779
|
281 endif |
|
282 |
|
283 ## check for input names |
7135
|
284 if (nargin >= 9) |
|
285 if (! isempty (inname)) |
|
286 retsys = syssetsignals (retsys, "in", inname); |
|
287 endif |
4779
|
288 endif |
|
289 |
|
290 ## check for output names |
7135
|
291 if (nargin >= 10) |
|
292 if (! isempty (outname)) |
|
293 retsys = syssetsignals (retsys, "out", outname); |
|
294 endif |
4779
|
295 endif |
|
296 |
|
297 ## set up yd |
7135
|
298 if (nargin < 11) |
|
299 retsys = syssetsignals (retsys, "yd", ones(1,p)*(tsam > 0)); |
4779
|
300 else |
7135
|
301 if (! isempty (outlist)) |
|
302 retsys = syssetsignals (retsys, "yd", ones (size (outlist)), outlist); |
4779
|
303 endif |
|
304 endif |
|
305 |
|
306 endfunction |