1 ## Copyright (C) 2009, 2011 Lukas F. Reichlin
3 ## This file is part of LTI Syncope.
5 ## LTI Syncope is free software: you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation, either version 3 of the License, or
8 ## (at your option) any later version.
10 ## LTI Syncope is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with LTI Syncope. If not, see <http://www.gnu.org/licenses/>.
19 ## @deftypefn {Function File} {@var{retsys} =} __sys_connect__ (@var{sys}, @var{M})
20 ## This function is part of the Model Abstraction Layer. No argument checking.
21 ## For internal use only.
24 ## Problem: Solve the system equations of
26 ## E(s) = U(s) + M Y(s)
30 ## Y(s) = G(s) [I - M G(s)]^-1 U(s)
31 ## = [I - G(s) M]^-1 G(s) U(s)
36 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
37 ## Created: October 2009
40 function sys = __sys_connect__ (sys, M)
47 ## TODO: Implementation for MIMO models. There are three possibilities:
48 ## 1. An _algebraic_ solution of the inversion problem in order
49 ## to not introduce unwanted zero/pole pairs. Difficult.
50 ## 2. A numeric solution of the inversion problem. Afterwards,
51 ## elimination of _all_ zero/pole pairs by minreal. Bad.
52 ## 3. Conversion to state-space, solving the problem there and
53 ## converting back to transfer function. Easier, but obviously,
54 ## this way needs MIMO __sys2ss__ and __sys2tf__ implementations
55 ## as described in Thomas Kailath's classic "Linear Systems".
56 ## Possibly this is the way to go, but it works for proper systems
57 ## only unless descriptor state-space models are implemented.
59 ## WARNING: The code below is a cheap hack to quickly enable SISO TF connections.
61 ## TODO: Check for den = 0, e.g. in feedback (tf (1), tf (-1))
63 if (p == 2 && m == 2 && num{1,2} == 0 && num{2,1} == 0 \
64 && M(1,1) == 0 && M(2,2) == 0)
66 sys.num(1,1) = num{1,1} * den{2,2};
67 sys.num(1,2) = M(1,2) * num{1,1} * num{2,2};
68 sys.num(2,1) = M(2,1) * num{1,1} * num{2,2};
69 sys.num(2,2) = num{2,2} * den{1,1};
71 sys.den(:) = den{1,1} * den{2,2} - M(1,2) * M(2,1) * num{1,1} * num{2,2};
73 elseif (p == 3 && m == 4 && num{1,3} == 0 && num{1,4} == 0 \
74 && num{2,1} == 0 && num{2,2} == 0 && num{2,4} == 0 \
75 && num{3,1} == 0 && num{3,2} == 0 && num{3,3} == 0 \
76 && M == [0, 1, 0; 0, 0, 1; 0, 0, 0; 0, 0, 0])
77 ## horzcat [sys1, sys2], plus, minus
78 sys.num(:) = tfpoly (0);
79 sys.den(:) = tfpoly (1);
81 sys.num(1,1) = num{1,1};
82 sys.num(1,2) = num{1,2};
83 sys.num(1,3) = num{1,1} * num{2,3};
84 sys.num(1,4) = num{1,2} * num{3,4};
85 sys.num(2,3) = num{2,3};
86 sys.num(3,4) = num{3,4};
88 sys.den(1,3) = den{2,3};
89 sys.den(1,4) = den{3,4};
90 sys.den(2,3) = den{2,3};
91 sys.den(3,4) = den{3,4};
93 elseif (p == 3 && m == 3 && num{1,3} == 0 \
94 && num{2,1} == 0 && num{2,2} == 0 && num{2,3} == 1 \
95 && num{3,1} == 0 && num{3,2} == 0 && num{3,3} == 1 \
96 && M == [0, 1, 0; 0, 0, 1; 0, 0, 0])
98 sys.num(1,3) = num{1,1} * den{1,2} + num{1,2} * den{1,1};
99 sys.den(1,3) = den{1,1} * den{1,2};
101 elseif (p == 4 && m == 3 && num{1,2} == 0 && num{1,3} == 0 \
102 && num{2,1} == 0 && num{2,3} == 0 \
103 && num{3,1} == 0 && num{3,2} == 0 && num{3,3} == 1 \
104 && num{4,1} == 0 && num{4,2} == 0 && num{4,3} == 1 \
105 && M == [0, 0, 1, 0; 0, 0, 0, 1; 0, 0, 0, 0])
106 ## vertcat [sys1; sys2]
107 sys.num(1,3) = num{1,1};
108 sys.num(2,3) = num{2,2};
110 sys.den(1,3) = den{1,1};
111 sys.den(2,3) = den{2,2};
114 ## MIMO case, convert to state-space and back.
115 warning ("tf: converting to minimal state-space for MIMO TF interconnections");
116 sys = tf (__sys_connect__ (ss (sys), M));