]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/@lti/mtimes.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / @lti / mtimes.m
1 ## Copyright (C) 2009, 2011   Lukas F. Reichlin
2 ##
3 ## This file is part of LTI Syncope.
4 ##
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.
9 ##
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.
14 ##
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/>.
17
18 ## -*- texinfo -*-
19 ## Matrix multiplication of LTI objects.  If necessary, object conversion
20 ## is done by sys_group.  Used by Octave for "sys1 * sys2".
21
22 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
23 ## Created: September 2009
24 ## Version: 0.2
25
26 function sys = mtimes (sys2, sys1)
27
28   if (nargin != 2)  # prevent sys = mtimes (sys1, sys2, sys3, ...)
29     error ("lti: mtimes: this is a binary operator");
30   endif
31
32   [p1, m1] = size (sys1);
33   [p2, m2] = size (sys2);
34
35   if (m2 != p1)
36     error ("lti: mtimes: system dimensions incompatible: (%dx%d) * (%dx%d)",
37             p2, m2, p1, m1);
38   endif
39
40   M22 = zeros (m2, p2);
41   M21 = eye (m2, p1);
42   M12 = zeros (m1, p2);
43   M11 = zeros (m1, p1);
44
45   M = [M22, M21;
46        M12, M11];
47
48   out_idx = 1 : p2;
49   in_idx = m2 + (1 : m1);
50
51   sys = __sys_group__ (sys2, sys1);
52   sys = __sys_connect__ (sys, M);
53   sys = __sys_prune__ (sys, out_idx, in_idx);
54
55 endfunction
56
57
58 ## Alternative code: consistency vs. compatibility
59 #{
60   M11 = zeros (m1, p1);
61   M12 = zeros (m1, p2);
62   M21 = eye (m2, p1);
63   M22 = zeros (m2, p2);
64   
65
66   M = [M11, M12;
67        M21, M22];
68
69   out_idx = p1 + (1 : p2);
70   in_idx = 1 : m1;
71
72   sys = __sys_group__ (sys1, sys2);
73 #}
74 ## Don't forget to adapt @tf/__sys_connect__.m draft code
75
76
77 ## mtimes
78 %!shared sysmat, sysmat_exp
79 %! sys1 = ss ([0, 1; -3, -2], [0; 1], [-5, 1], [2]);
80 %! sys2 = ss ([-10], [1], [-40], [5]);
81 %! sys3 = sys2 * sys1;
82 %! [A, B, C, D] = ssdata (sys3);
83 %! sysmat = [A, B; C, D];
84 %! A_exp = [ -10   -5    1
85 %!             0    0    1
86 %!             0   -3   -2 ];
87 %! B_exp = [   2
88 %!             0
89 %!             1 ];
90 %! C_exp = [ -40  -25    5 ];
91 %! D_exp = [  10 ];
92 %! sysmat_exp = [A_exp, B_exp; C_exp, D_exp];
93 %!assert (sysmat, sysmat_exp)
94
95
96 ## Cascade inter-connection of two systems in state-space form
97 ## Test from SLICOT AB05MD
98 ## TODO: order of united state vector: consistency vs. compatibility?
99 #%!shared M, Me
100 #%! A1 = [ 1.0   0.0  -1.0
101 #%!        0.0  -1.0   1.0
102 #%!        1.0   1.0   2.0 ];
103 #%!
104 #%! B1 = [ 1.0   1.0   0.0
105 #%!        2.0   0.0   1.0 ].';
106 #%!
107 #%! C1 = [ 3.0  -2.0   1.0
108 #%!        0.0   1.0   0.0 ];
109 #%!
110 #%! D1 = [ 1.0   0.0
111 #%!        0.0   1.0 ];
112 #%!
113 #%! A2 = [-3.0   0.0   0.0
114 #%!        1.0   0.0   1.0
115 #%!        0.0  -1.0   2.0 ];
116 #%!
117 #%! B2 = [ 0.0  -1.0   0.0
118 #%!        1.0   0.0   2.0 ].';
119 #%!
120 #%! C2 = [ 1.0   1.0   0.0
121 #%!        1.0   1.0  -1.0 ];
122 #%!
123 #%! D2 = [ 1.0   1.0
124 #%!        0.0   1.0 ];
125 #%!
126 #%! sys1 = ss (A1, B1, C1, D1);
127 #%! sys2 = ss (A2, B2, C2, D2);
128 #%! sys = sys2 * sys1;
129 #%! [A, B, C, D] = ssdata (sys);
130 #%! M = [A, B; C, D];
131 #%!
132 #%! Ae = [ 1.0000   0.0000  -1.0000   0.0000   0.0000   0.0000
133 #%!        0.0000  -1.0000   1.0000   0.0000   0.0000   0.0000
134 #%!        1.0000   1.0000   2.0000   0.0000   0.0000   0.0000
135 #%!        0.0000   1.0000   0.0000  -3.0000   0.0000   0.0000
136 #%!       -3.0000   2.0000  -1.0000   1.0000   0.0000   1.0000
137 #%!        0.0000   2.0000   0.0000   0.0000  -1.0000   2.0000 ];
138 #%!
139 #%! Be = [ 1.0000   2.0000
140 #%!        1.0000   0.0000
141 #%!        0.0000   1.0000
142 #%!        0.0000   1.0000
143 #%!       -1.0000   0.0000
144 #%!        0.0000   2.0000 ];
145 #%!
146 #%! Ce = [ 3.0000  -1.0000   1.0000   1.0000   1.0000   0.0000
147 #%!        0.0000   1.0000   0.0000   1.0000   1.0000  -1.0000 ];
148 #%!
149 #%! De = [ 1.0000   1.0000
150 #%!        0.0000   1.0000 ];
151 #%!
152 #%! Me = [Ae, Be; Ce, De];
153 #%!
154 #%!assert (M, Me, 1e-4);