3431
|
1 ## Copyright (C) 1998 Kai P. Mueller |
|
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. |
3431
|
19 |
|
20 ## -*- texinfo -*- |
3502
|
21 ## @deftypefn {Function File} {} buildssic (@var{clst}, @var{ulst}, @var{olst}, @var{ilst}, @var{s1}, @var{s2}, @var{s3}, @var{s4}, @var{s5}, @var{s6}, @var{s7}, @var{s8}) |
3431
|
22 ## |
|
23 ## Form an arbitrary complex (open or closed loop) system in |
5016
|
24 ## state-space form from several systems. @command{buildssic} can |
|
25 ## easily (despite its cryptic syntax) integrate transfer functions |
3431
|
26 ## from a complex block diagram into a single system with one call. |
|
27 ## This function is especially useful for building open loop |
5016
|
28 ## interconnections for |
|
29 ## @iftex |
|
30 ## @tex |
|
31 ## $ { \cal H }_\infty $ and $ { \cal H }_2 $ |
|
32 ## @end tex |
|
33 ## @end iftex |
|
34 ## @ifinfo |
|
35 ## H-infinity and H-2 |
|
36 ## @end ifinfo |
|
37 ## designs or for closing loops with these controllers. |
3431
|
38 ## |
5016
|
39 ## Although this function is general purpose, the use of @command{sysgroup} |
|
40 ## @command{sysmult}, @command{sysconnect} and the like is recommended for |
3431
|
41 ## standard operations since they can handle mixed discrete and continuous |
|
42 ## systems and also the names of inputs, outputs, and states. |
|
43 ## |
|
44 ## The parameters consist of 4 lists that describe the connections |
5016
|
45 ## outputs and inputs and up to 8 systems @var{s1}--@var{s8}. |
3431
|
46 ## Format of the lists: |
|
47 ## @table @var |
3502
|
48 ## @item clst |
3431
|
49 ## connection list, describes the input signal of |
|
50 ## each system. The maximum number of rows of Clst is |
|
51 ## equal to the sum of all inputs of s1-s8. |
|
52 ## |
|
53 ## Example: |
5016
|
54 ## @code{[1 2 -1; 2 1 0]} means that: new input 1 is old input 1 |
|
55 ## + output 2 - output 1, and new input 2 is old input 2 |
3431
|
56 ## + output 1. The order of rows is arbitrary. |
|
57 ## |
5016
|
58 ## @item ulst |
|
59 ## if not empty the old inputs in vector @var{ulst} will |
3431
|
60 ## be appended to the outputs. You need this if you |
5016
|
61 ## want to ``pull out'' the input of a system. Elements |
|
62 ## are input numbers of @var{s1}--@var{s8}. |
3431
|
63 ## |
5016
|
64 ## @item olst |
3431
|
65 ## output list, specifiy the outputs of the resulting |
5016
|
66 ## systems. Elements are output numbers of @var{s1}--@var{s8}. |
|
67 ## The numbers are allowed to be negative and may |
3431
|
68 ## appear in any order. An empty matrix means |
|
69 ## all outputs. |
|
70 ## |
5016
|
71 ## @item ilst |
3431
|
72 ## input list, specifiy the inputs of the resulting |
5016
|
73 ## systems. Elements are input numbers of @var{s1}--@var{s8}. |
|
74 ## The numbers are allowed to be negative and may |
3431
|
75 ## appear in any order. An empty matrix means |
|
76 ## all inputs. |
|
77 ## @end table |
|
78 ## |
|
79 ## Example: Very simple closed loop system. |
|
80 ## @example |
|
81 ## @group |
|
82 ## w e +-----+ u +-----+ |
|
83 ## --->o--*-->| K |--*-->| G |--*---> y |
|
84 ## ^ | +-----+ | +-----+ | |
|
85 ## - | | | | |
|
86 ## | | +----------------> u |
|
87 ## | | | |
|
88 ## | +-------------------------|---> e |
|
89 ## | | |
|
90 ## +----------------------------+ |
|
91 ## @end group |
|
92 ## @end example |
|
93 ## |
5016
|
94 ## The closed loop system @var{GW} can be optained by |
3431
|
95 ## @example |
|
96 ## GW = buildssic([1 2; 2 -1], 2, [1 2 3], 2, G, K); |
|
97 ## @end example |
|
98 ## @table @var |
3502
|
99 ## @item clst |
5016
|
100 ## 1st row: connect input 1 (@var{G}) with output 2 (@var{K}). |
|
101 ## |
|
102 ## 2nd row: connect input 2 (@var{K}) with negative output 1 (@var{G}). |
3502
|
103 ## @item ulst |
5016
|
104 ## Append input of 2 (@var{K}) to the number of outputs. |
3502
|
105 ## @item olst |
5016
|
106 ## Outputs are output of 1 (@var{G}), 2 (@var{K}) and |
|
107 ## appended output 3 (from @var{ulst}). |
3502
|
108 ## @item ilst |
5016
|
109 ## The only input is 2 (@var{K}). |
3431
|
110 ## @end table |
|
111 ## |
|
112 ## Here is a real example: |
|
113 ## @example |
|
114 ## @group |
|
115 ## +----+ |
|
116 ## -------------------->| W1 |---> v1 |
|
117 ## z | +----+ |
5016
|
118 ## ----|-------------+ |
|
119 ## | | |
3431
|
120 ## | +---+ v +----+ |
|
121 ## *--->| G |--->O--*-->| W2 |---> v2 |
|
122 ## | +---+ | +----+ |
|
123 ## | | |
|
124 ## | v |
|
125 ## u y |
|
126 ## @end group |
|
127 ## @end example |
5016
|
128 ## @iftex |
|
129 ## @tex |
|
130 ## $$ { \rm min } \Vert GW_{vz} \Vert _\infty $$ |
|
131 ## @end tex |
|
132 ## @end iftex |
|
133 ## @ifinfo |
|
134 ## @example |
|
135 ## min || GW || |
|
136 ## vz infty |
|
137 ## @end example |
|
138 ## @end ifinfo |
3431
|
139 ## |
5016
|
140 ## The closed loop system @var{GW} |
|
141 ## @iftex |
|
142 ## @tex |
|
143 ## from $ [z, u]^T $ to $ [v_1, v_2, y]^T $ |
|
144 ## @end tex |
|
145 ## @end iftex |
|
146 ## @ifinfo |
|
147 ## from [z, u]' to [v1, v2, y]' |
|
148 ## @end ifinfo |
|
149 ## can be obtained by (all @acronym{SISO} systems): |
3431
|
150 ## @example |
|
151 ## GW = buildssic([1, 4; 2, 4; 3, 1], 3, [2, 3, 5], |
|
152 ## [3, 4], G, W1, W2, One); |
|
153 ## @end example |
5016
|
154 ## where ``One'' is a unity gain (auxillary) function with order 0. |
3431
|
155 ## (e.g. @code{One = ugain(1);}) |
|
156 ## @end deftypefn |
|
157 |
|
158 ## Author: Kai P. Mueller <mueller@ifr.ing.tu-bs.de> |
|
159 ## Created: April 1998 |
|
160 |
|
161 function sys = buildssic (Clst, Ulst, Olst, Ilst, s1, s2, s3, s4, s5, s6, s7, s8) |
|
162 |
|
163 if((nargin < 5) || (nargin > 12)) |
|
164 usage("sys = buildssic(Clst,Ulst,Olst,Ilst,s1,s2,s3,s4,s5,s6,s7,s8)"); |
|
165 endif |
|
166 if (nargin >= 5) |
4030
|
167 if (!isstruct(s1)) |
3431
|
168 error("---> s1 must be a structed system."); |
|
169 endif |
|
170 s1 = sysupdate(s1, "ss"); |
|
171 [n, nz, m, p] = sysdimensions(s1); |
|
172 if (!n && !nz) |
|
173 error("---> pure static system must not be the first in list."); |
|
174 endif |
|
175 if (n && nz) |
|
176 error("---> cannot handle mixed continuous and discrete systems."); |
|
177 endif |
|
178 D_SYS = (nz > 0); |
|
179 [A,B,C,D,tsam] = sys2ss(s1); |
|
180 nt = n + nz; |
|
181 endif |
|
182 for ii = 6:nargin |
4771
|
183 eval(["mysys = s", num2str(ii-4), ";"]); |
|
184 if (!isstruct(mysys)) |
3431
|
185 error("---> Parameter must be a structed system."); |
|
186 endif |
4771
|
187 mysys = sysupdate(mysys, "ss"); |
|
188 [n1, nz1, m1, p1] = sysdimensions(mysys); |
3431
|
189 if (n1 && nz1) |
|
190 error("---> cannot handle mixed continuous and discrete systems."); |
|
191 endif |
|
192 if (D_SYS) |
|
193 if (n1) |
|
194 error("---> cannot handle mixed cont. and discr. systems."); |
|
195 endif |
4771
|
196 if (tsam != sysgettsam(mysys)) |
3431
|
197 error("---> sampling time of all systems must match."); |
|
198 endif |
|
199 endif |
4771
|
200 [as,bs,cs,ds] = sys2ss(mysys); |
3431
|
201 nt1 = n1 + nz1; |
|
202 if (!nt1) |
|
203 ## pure gain (pad B, C with zeros) |
|
204 B = [B, zeros(nt,m1)]; |
|
205 C = [C; zeros(p1,nt)]; |
|
206 else |
|
207 A = [A, zeros(nt,nt1); zeros(nt1,nt), as]; |
|
208 B = [B, zeros(nt,m1); zeros(nt1,m), bs]; |
|
209 C = [C, zeros(p,nt1); zeros(p1,nt), cs]; |
|
210 endif |
|
211 D = [D, zeros(p,m1); zeros(p1,m), ds]; |
|
212 n = n + n1; |
|
213 nz = nz + nz1; |
|
214 nt = nt + nt1; |
|
215 m = m + m1; |
|
216 p = p + p1; |
|
217 endfor |
|
218 |
|
219 ## check maximum dimensions |
|
220 [nx, mx] = size(Clst); |
|
221 if (nx > m) |
|
222 error("---> more rows in Clst than total number of inputs."); |
|
223 endif |
|
224 if (mx > p+1) |
|
225 error("---> more cols in Clst than total number of outputs."); |
|
226 endif |
|
227 ## empty vector Ulst is OK |
|
228 lul = length(Ulst); |
|
229 if (lul) |
4030
|
230 if (!isvector(Ulst)) |
3431
|
231 error("---> Input u list Ulst must be a vector."); |
|
232 endif |
|
233 if (lul > m) |
|
234 error("---> more values in Ulst than number of inputs."); |
|
235 endif |
|
236 endif |
|
237 if (!length(Olst)) Olst = [1:(p+lul)]; endif |
|
238 if (!length(Ilst)) Ilst = [1:m]; endif |
4030
|
239 if (!isvector(Olst)) |
3431
|
240 error("---> Output list Olst must be a vector."); |
|
241 endif |
4030
|
242 if (!isvector(Ilst)) |
3431
|
243 error("---> Input list Ilst must be a vector."); |
|
244 endif |
|
245 |
|
246 ## build the feedback "K" from the interconnection data Clst |
|
247 K = zeros(m, p); |
|
248 inp_used = zeros(m,1); |
|
249 for ii = 1:nx |
|
250 xx = Clst(ii,:); |
|
251 iu = xx(1); |
|
252 if ((iu < 1) || (iu > m)) |
|
253 error("---> invalid value in first col of Clst."); |
|
254 endif |
|
255 if (inp_used(iu)) |
|
256 error("---> Input specified more than once."); |
|
257 endif |
|
258 inp_used(iu) = 1; |
|
259 for kk = 2:mx |
|
260 it = xx(kk); |
|
261 if (abs(it) > p) |
|
262 error("---> invalid row value in Clst."); |
|
263 elseif (it) |
|
264 K(iu,abs(it)) = sign(it); |
|
265 endif |
|
266 endfor |
|
267 endfor |
|
268 |
|
269 ## form the "closed loop", i.e replace u in |
|
270 ## . |
|
271 ## x = Ax + Bu |
|
272 ## ~ |
|
273 ## y = Cx + Du by u = K*y+u |
|
274 ## |
|
275 ## -1 |
|
276 ## R = (I-D*K) must exist. |
|
277 |
|
278 R = eye(p) - D*K; |
|
279 if (rank(R) < p) |
|
280 error("---> singularity in algebraic loop."); |
|
281 else |
|
282 R = inv(R); |
|
283 endif |
|
284 A = A + B*K*R*C; |
|
285 B = B + B*K*R*D; |
|
286 C = R*C; |
|
287 D = R*D; |
|
288 |
|
289 ## append old inputs u to the outputs (if lul > 0) |
|
290 kc = K*C; |
|
291 kdi = eye(m) + K*D; |
|
292 for ii = 1:lul |
|
293 it = Ulst(ii); |
|
294 if ((it < 1) || (it > m)) |
|
295 error("---> invalid value in Ulst."); |
|
296 endif |
|
297 C = [C; kc(it,:)]; |
|
298 D = [D; kdi(it,:)]; |
|
299 endfor |
|
300 |
|
301 ## select and rearrange outputs |
|
302 nn = length(A); |
|
303 lol = length(Olst); |
|
304 Cnew = zeros(lol,nn); |
|
305 Dnew = zeros(lol,m); |
|
306 for ii = 1:lol |
|
307 iu = Olst(ii); |
|
308 if (!iu || (abs(iu) > p+lul)) |
|
309 error("---> invalid value in Olst."); |
|
310 endif |
|
311 Cnew(ii,:) = sign(iu)*C(abs(iu),:); |
|
312 Dnew(ii,:) = sign(iu)*D(abs(iu),:); |
|
313 endfor |
|
314 C = Cnew; |
|
315 D = Dnew; |
|
316 lil = length(Ilst); |
|
317 Bnew = zeros(nn,lil); |
|
318 Dnew = zeros(lol,lil); |
|
319 for ii = 1:lil |
|
320 iu = Ilst(ii); |
|
321 if (!iu || (abs(iu) > m)) |
|
322 error("---> invalid value in Ilst."); |
|
323 endif |
|
324 Bnew(:,ii) = sign(iu)*B(:,abs(iu)); |
|
325 Dnew(:,ii) = sign(iu)*D(:,abs(iu)); |
|
326 endfor |
|
327 |
4771
|
328 sys = ss(A, Bnew, C, Dnew, tsam, n, nz); |
3431
|
329 |
|
330 endfunction |