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 |
|
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. |
|
200 if (nargin == 3) d = zeros(rows(c) , columns(b)); |
|
201 elseif (isempty(d)) d = zeros(rows(c) , columns(b)); endif |
|
202 |
|
203 ## Check the dimensions |
|
204 [na,m,p] = abcddim(a,b,c,d); |
|
205 |
|
206 ## If dimensions are wrong, exit function |
|
207 if (m == -1) |
|
208 error("a(%dx%d), b(%dx%d), c(%dx%d), d(%dx%d); incompatible", ... |
|
209 rows(a), columns(a), rows(b), columns(b), rows(c), columns(c), ... |
|
210 rows(d), columns(d)); |
|
211 endif |
|
212 |
|
213 ## check for tsam input |
|
214 if(nargin < 5) tsam = 0; |
|
215 elseif( !( is_sample(tsam) | (tsam == 0) ) ) |
|
216 error("tsam must be a nonnegative real scalar"); |
|
217 endif |
|
218 |
|
219 ## check for continuous states |
|
220 if( (nargin < 6) & (tsam == 0) ) n = na; |
|
221 elseif(nargin < 6) n = 0; |
5443
|
222 elseif((!ismatrix(n)) | ischar(n)) |
4779
|
223 error("Parameter n is not a numerical value."); |
|
224 elseif( (!isscalar(n)) | (n < 0 ) | (n != round(n)) ) |
|
225 if(isscalar(n)) error("invalid value of n=%d,%e",n,n); |
|
226 else error("invalid value of n=(%dx%d)", ... |
|
227 rows(n), columns(n)); endif |
|
228 endif |
|
229 |
|
230 ## check for num discrete states |
|
231 if( (nargin < 7) & (tsam == 0)) nz = 0; |
|
232 elseif(nargin < 7) nz = na - n; |
5443
|
233 elseif((!ismatrix(nz)) | ischar(nz)) |
4779
|
234 error("Parameter nz is not a numerical value."); |
|
235 elseif( (!isscalar(nz)) | (nz < 0 ) | (nz != round(nz)) ) |
|
236 if(isscalar(nz)) |
|
237 error(["invalid value of nz=",num2str(nz)]); |
|
238 else |
|
239 error(["invalid value of nz=(",num2str(rows(nz)),"x", ... |
|
240 num2str(columns(nz)),")"]); |
|
241 endif |
|
242 endif |
|
243 |
|
244 ## check for total number of states |
|
245 if( (n + nz) != na ) |
|
246 error(["invalid: a is ",num2str(na),"x",num2str(na),", n=", ... |
|
247 num2str(n),", nz=",num2str(nz)]); |
|
248 endif |
|
249 |
|
250 ## construct system with default names |
|
251 retsys.a = a; |
|
252 retsys.b = b; |
|
253 retsys.c = c; |
|
254 retsys.d = d; |
|
255 |
|
256 retsys.n = n; |
|
257 retsys.nz = nz; |
|
258 retsys.tsam = tsam; |
|
259 retsys.yd = zeros(1,p); # default value entered below |
|
260 |
|
261 ## Set the system vector: active = 2(ss), updated = [0 0 1]; |
|
262 retsys.sys = [2, 0, 0, 1]; |
|
263 |
|
264 retsys.stname = __sysdefstname__ (n, nz); |
|
265 retsys.inname = __sysdefioname__ (m, "u"); |
|
266 retsys.outname = __sysdefioname__ (p, "y"); |
|
267 |
|
268 ## check for state names |
|
269 if(nargin >= 8) |
|
270 if(!isempty(stname)) retsys = syssetsignals(retsys,"st",stname); endif |
|
271 endif |
|
272 |
|
273 ## check for input names |
|
274 if(nargin >= 9) |
|
275 if(!isempty(inname)) retsys = syssetsignals(retsys,"in",inname); endif |
|
276 endif |
|
277 |
|
278 ## check for output names |
|
279 if(nargin >= 10) |
|
280 if(!isempty(outname)) retsys = syssetsignals(retsys,"out",outname); endif |
|
281 endif |
|
282 |
|
283 ## set up yd |
|
284 if(nargin < 11) |
|
285 retsys = syssetsignals(retsys,"yd",ones(1,p)*(tsam > 0)); |
|
286 else |
|
287 if(!isempty(outlist)) |
|
288 retsys = syssetsignals(retsys,"yd",ones(size(outlist)),outlist); |
|
289 endif |
|
290 endif |
|
291 |
|
292 endfunction |