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