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