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 -*- |
5016
|
20 ## @deftypefn {Function File} {@var{clsys} =} sysconnect (@var{sys}, @var{out_idx}, @var{in_idx}, @var{order}, @var{tol}) |
3430
|
21 ## Close the loop from specified outputs to respective specified inputs |
|
22 ## |
|
23 ## @strong{Inputs} |
|
24 ## @table @var |
|
25 ## @item sys |
5016
|
26 ## System data structure. |
3430
|
27 ## @item out_idx |
|
28 ## @itemx in_idx |
5016
|
29 ## Names or indices of signals to connect (see @code{sysidx}). |
3462
|
30 ## The output specified by @math{out_idx(ii)} is connected to the input |
|
31 ## specified by @math{in_idx(ii)}. |
3430
|
32 ## @item order |
|
33 ## logical flag (default = 0) |
|
34 ## @table @code |
|
35 ## @item 0 |
5016
|
36 ## Leave inputs and outputs in their original order. |
3430
|
37 ## @item 1 |
5016
|
38 ## Permute inputs and outputs to the order shown in the diagram below. |
3430
|
39 ## @end table |
|
40 ## @item tol |
5016
|
41 ## Tolerance for singularities in algebraic loops, default: 200@code{eps}. |
3430
|
42 ## @end table |
|
43 ## |
|
44 ## @strong{Outputs} |
5016
|
45 ## @table @var |
|
46 ## @item clsys |
|
47 ## Resulting closed loop system. |
|
48 ## @end table |
3430
|
49 ## |
|
50 ## @strong{Method} |
5016
|
51 ## |
3430
|
52 ## @code{sysconnect} internally permutes selected inputs, outputs as shown |
|
53 ## below, closes the loop, and then permutes inputs and outputs back to their |
|
54 ## original order |
|
55 ## @example |
|
56 ## @group |
5016
|
57 ## -------------------- |
3430
|
58 ## u_1 ----->| |----> y_1 |
|
59 ## | sys | |
|
60 ## old u_2 | | |
|
61 ## u_2* ---->(+)--->| |----->y_2 |
5016
|
62 ## (in_idx) ^ -------------------- | (out_idx) |
3430
|
63 ## | | |
|
64 ## ------------------------------- |
|
65 ## @end group |
|
66 ## @end example |
|
67 ## The input that has the summing junction added to it has an * added to |
|
68 ## the end of the input name. |
|
69 ## @end deftypefn |
|
70 |
|
71 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu> |
|
72 ## Created: August 1995 |
|
73 ## modified by John Ingram July 1996 |
|
74 |
|
75 function sys = sysconnect (sys, output_list, input_list, order, tol) |
|
76 |
|
77 if( (nargin < 3) | (nargin > 5) ) |
|
78 usage("retsys = sysconnect(sys,output_list,input_list[,order,tol])"); |
|
79 endif |
|
80 |
|
81 ## check order |
|
82 if(nargin <= 3) |
|
83 order = 0; |
|
84 elseif( (order != 0) & (order != 1) ) |
|
85 error("sysconnect: order must be either 0 or 1") |
|
86 endif |
|
87 |
|
88 if (nargin <= 4) |
|
89 tol = 200*eps; |
|
90 elseif( !is_sample(tol) ) |
|
91 error("sysconnect: tol must be a positive scalar"); |
|
92 elseif(tol > 1e2*sqrt(eps)) |
|
93 warning(["sysconnect: tol set to large value=",num2str(tol), ... |
|
94 ", eps=",num2str(eps)]) |
|
95 endif |
|
96 |
3462
|
97 ## convert signal names to indices |
|
98 if(is_signal_list(input_list) | isstr(input_list)) |
|
99 input_list = sysidx(sys,"in",input_list); |
|
100 endif |
|
101 if(is_signal_list(output_list) | isstr(output_list)) |
|
102 output_list = sysidx(sys,"out",output_list); |
|
103 endif |
|
104 |
3430
|
105 ## verify sizes,format of input, output lists |
|
106 if( min(size(output_list))*min(size(input_list)) != 1) |
|
107 error("output_list and input_list must be vectors"); |
|
108 else |
|
109 lo = length(output_list); |
|
110 li = length(input_list); |
|
111 if(lo != li) |
|
112 error("output_list and input_list must be of the same length") |
|
113 endif |
|
114 |
|
115 if(is_duplicate_entry(output_list) | is_duplicate_entry(input_list) ) |
|
116 error("duplicate entry in input_list and/or output_list"); |
|
117 endif |
|
118 endif |
|
119 |
|
120 [nc,nz,mm,pp] = sysdimensions(sys); |
|
121 nn = nc+nz; |
|
122 |
4030
|
123 if( !isstruct(sys)) |
3430
|
124 error("sys must be in structured system form") |
|
125 elseif(pp < li) |
|
126 error(["length(output_list)=",num2str(li),", sys has only ", ... |
|
127 num2str(pp),"system outputs"]) |
|
128 elseif(mm < li) |
|
129 error(["length(input_list)=",num2str(li),", sys has only ", ... |
|
130 num2str(mm),"system inputs"]) |
|
131 endif |
|
132 |
|
133 ## check that there are enough inputs/outputs in the system for the lists |
|
134 if(max(input_list) > mm) |
|
135 error("max(input_list) exceeds the number of inputs"); |
|
136 elseif(max(output_list) > pp) |
|
137 error("max(output_list) exceeds the number of outputs"); |
|
138 endif |
|
139 |
|
140 output_list = reshape(output_list,1,length(output_list)); |
|
141 |
|
142 ## make sure we're in state space form |
|
143 sys = sysupdate (sys, "ss"); |
|
144 |
|
145 ## permute rows and columns of B,C,D matrices into pseudo-dgkf form... |
|
146 all_inputs = sysreorder(mm,input_list); |
|
147 all_outputs = sysreorder(pp,output_list); |
|
148 |
|
149 [aa,bb,cc,dd] = sys2ss(sys); |
|
150 bb = bb(:,all_inputs); |
|
151 cc = cc(all_outputs,:); |
|
152 dd = dd(all_outputs,all_inputs); |
|
153 |
|
154 yd = sysgetsignals(sys,"yd"); |
|
155 yd = yd(all_outputs); |
|
156 |
|
157 ## m1, p1 = number of inputs, outputs that are not being connected |
|
158 m1 = mm-li; |
|
159 p1 = pp-li; |
|
160 |
|
161 ## m2, p2: 1st column, row of B, C that is being connected |
|
162 m2 = m1+1; |
|
163 p2 = p1+1; |
|
164 |
|
165 ## partition system into a DGKF-like form; the loop is closed around |
|
166 ## B2, C2 |
|
167 if(m1 > 0) |
|
168 B1 = bb(:,1:m1); |
|
169 D21= dd(p2:pp,1:m1); |
|
170 endif |
|
171 B2 = bb(:,m2:mm); |
|
172 if(p1 > 0) |
|
173 C1 = cc(1:p1,:); |
|
174 D12= dd(1:p1,m2:mm); |
|
175 endif |
|
176 C2 = cc(p2:pp,:); |
|
177 if(m1*p1 > 0) |
|
178 D11= dd(1:p1,1:m1); |
|
179 endif |
|
180 D22= dd(p2:pp,m2:mm); |
|
181 |
|
182 if(norm(D22)) |
|
183 warning("sysconnect: possible algebraic loop, D22 non-zero"); |
|
184 D22i = (eye(size(D22))-D22); |
|
185 C2h = D22i\C2; |
|
186 if(m1 > 0) |
|
187 D21h = D22i\D21; |
|
188 endif |
|
189 D22h = D22i\D22; |
|
190 else |
|
191 C2h = C2; |
|
192 if(m1 > 0) |
|
193 D21h = D21; |
|
194 endif |
|
195 D22h = D22; |
|
196 |
|
197 endif |
|
198 |
|
199 ## check cont state -> disc output -> cont state |
|
200 dyi = find(yd(p2:pp)); |
|
201 |
|
202 ## disp("sysconnect: dyi=") |
|
203 ## dyi |
|
204 ## nc |
|
205 ## disp("/sysconnect"); |
|
206 |
|
207 if( (nc > 0) & find(dyi > 0) ) |
|
208 B2con = B2(1:nc,dyi); # connection to cont states |
|
209 C2hd = C2h(dyi,1:nc); # cont states -> outputs |
|
210 else |
|
211 B2con = C2hd = []; |
|
212 endif |
|
213 |
|
214 if(max(size(B2con)) & max(size(C2hd)) ) |
|
215 if(norm(B2con*C2hd)) |
|
216 warning("sysconnect: cont-state -> disc output -> cont state derivative"); |
|
217 warning(" connection made; resulting system may not be meaningful"); |
|
218 endif |
|
219 endif |
|
220 |
|
221 Ac = aa+B2*C2h; |
|
222 if(m1 > 0) |
|
223 B1c = B1 + B2*D21h; |
|
224 endif |
|
225 B2c = B2*(eye(size(D22h)) + D22h); |
|
226 if(p1*m1 > 0) |
|
227 D11c = D11 + D12*D21h; |
|
228 endif |
|
229 if(p1 > 0) |
|
230 C1c = C1+D12*C2h; |
|
231 D12c = D12*(eye(size(D22h))+D22h); |
|
232 endif |
|
233 |
|
234 ## construct system data structure |
|
235 if(m1 > 0) |
|
236 Bc = [B1c, B2c]; |
|
237 else |
|
238 Bc = B2c; |
|
239 endif |
|
240 |
|
241 if(p1 > 0) |
|
242 Cc = [C1c;C2h]; |
|
243 else |
|
244 Cc = C2h; |
|
245 endif |
|
246 |
|
247 if(m1*p1 > 0) |
|
248 Dc = [D11c,D12c; D21h,D22h]; |
|
249 elseif(m1 > 0) |
|
250 Dc = [D21h, D22h]; |
|
251 elseif(p1 > 0) |
|
252 Dc = [D12c; D22h]; |
|
253 else |
|
254 Dc = D22h; |
|
255 endif |
|
256 |
|
257 ## permute rows and columns of Bc, Cc, Dc back into original order |
|
258 Im = eye(mm,mm); |
|
259 Pi = Im(:,all_inputs); |
|
260 back_inputs = Pi*[1:mm]'; |
|
261 |
|
262 Ip = eye(pp,pp); |
|
263 Po = Ip(:,all_outputs); |
|
264 back_outputs = Po*[1:pp]'; |
|
265 |
|
266 Bc = Bc(:,back_inputs); |
|
267 Cc = Cc(back_outputs,:); |
|
268 Dc = Dc(back_outputs,back_inputs); |
|
269 yd = yd(back_outputs); |
|
270 |
|
271 ## rebuild system |
|
272 Ts = sysgettsam(sys); |
|
273 [stnam,innam,outnam] = sysgetsignals(sys); |
4771
|
274 sys = ss(Ac,Bc,Cc,Dc,Ts,nc,nz,stnam,innam,outnam,find(yd)); |
3430
|
275 |
|
276 ## update connected input names |
|
277 for ii = 1:length(input_list) |
|
278 idx = input_list(ii); |
4771
|
279 tmpval = sysgetsignals(sys,"in",idx); |
|
280 strval = sprintf("%s*",tmpval{1} ); |
3430
|
281 sys = syssetsignals(sys,"in",strval,idx); |
|
282 endfor |
|
283 |
|
284 ## maintain original system type if it was SISO |
|
285 if (strcmp (sysgettype (sys), "tf")) |
|
286 sysupdate (sys, "tf"); |
|
287 elseif (strcmp (sysgettype (sys),"zp")) |
|
288 sysupdate (sys, "zp"); |
|
289 endif |
|
290 |
|
291 endfunction |