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