]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/ltimodels.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / ltimodels.m
1 ## Copyright (C) 2009 Luca Favatella
2 ## Copyright (C) 2009, 2010, 2011   Lukas Reichlin
3 ##
4 ## This file is part of LTI Syncope.
5 ##
6 ## LTI Syncope is free software: you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation, either version 3 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## LTI Syncope is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with LTI Syncope.  If not, see <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} test ltimodels
21 ## @deftypefnx {Function File} ltimodels
22 ## @deftypefnx {Function File} ltimodels (@var{systype})
23 ## Test suite and help for LTI models.
24 ## @end deftypefn
25
26 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
27 ## Created: November 2009
28 ## Version: 0.2
29
30 function ltimodels (systype = "general")
31
32   %if (nargin > 1)
33     print_usage ();
34   %endif
35
36   ## TODO: write documentation
37
38   if (! ischar (systype))
39     error ("ltimodels: argument must be a string");
40   endif
41
42   systype = lower (systype);
43
44   switch (systype)
45     case "ss"
46       str = {"State Space (SS) Models"\
47              "-----------------------"\
48              ""};
49
50     case "tf"
51       str = {"Transfer Function (TF) Models"\
52              "-----------------------------"\
53              ""};
54
55     otherwise  # general
56       str = {"Linear Time Invariant (LTI) Models"\
57              "----------------------------------"\
58              ""};
59
60   endswitch
61
62   disp ("");
63   disp (char (str));
64
65 endfunction
66
67
68
69 ## ==============================================================================
70 ## LTI Tests
71 ## ==============================================================================
72
73 ## isct, isdt
74 %!shared ltisys
75 %! ltisys = tf (12);
76 %!assert (ltisys.ts, -2);
77 %!assert (isct (ltisys));
78 %!assert (isdt (ltisys));
79
80 %!shared ltisys
81 %! ltisys = ss (17);
82 %!assert (ltisys.ts, -2);
83 %!assert (isct (ltisys));
84 %!assert (isdt (ltisys));
85
86 %!shared ltisys
87 %! ltisys = tf (1, [1 1]);
88 %!assert (ltisys.ts, 0);
89 %!assert (isct (ltisys));
90 %!assert (! isdt (ltisys));
91
92 %!shared ltisys, ts
93 %! ts = 0.1;
94 %! ltisys = ss (-1, 1, 1, 0, ts);
95 %!assert (ltisys.ts, ts);
96 %!assert (! isct (ltisys));
97 %!assert (isdt (ltisys));
98
99
100
101 ## ==============================================================================
102 ## TF Tests
103 ## ==============================================================================
104
105
106
107 ## ==============================================================================
108 ## SS Tests
109 ## ==============================================================================
110
111
112 ## staircase (SLICOT AB01OD)
113 %!shared Ac, Bc, Ace, Bce
114 %! A = [ 17.0   24.0    1.0    8.0   15.0
115 %!       23.0    5.0    7.0   14.0   16.0
116 %!        4.0    6.0   13.0   20.0   22.0
117 %!       10.0   12.0   19.0   21.0    3.0
118 %!       11.0   18.0   25.0    2.0    9.0 ];
119 %!
120 %! B = [ -1.0   -4.0
121 %!        4.0    9.0
122 %!       -9.0  -16.0
123 %!       16.0   25.0
124 %!      -25.0  -36.0 ];
125 %!
126 %! tol = 0;
127 %!
128 %! A = A.';  # There's a little mistake in the example
129 %!           # program of routine AB01OD in SLICOT 5.0
130 %!
131 %! [Ac, Bc, U, ncont] = slab01od (A, B, tol);
132 %!
133 %! Ace = [ 12.8848   3.2345  11.8211   3.3758  -0.8982
134 %!          4.4741 -12.5544   5.3509   5.9403   1.4360
135 %!         14.4576   7.6855  23.1452  26.3872 -29.9557
136 %!          0.0000   1.4805  27.4668  22.6564  -0.0072
137 %!          0.0000   0.0000 -30.4822   0.6745  18.8680 ];
138 %!
139 %! Bce = [ 31.1199  47.6865
140 %!          3.2480   0.0000
141 %!          0.0000   0.0000
142 %!          0.0000   0.0000
143 %!          0.0000   0.0000 ];
144 %!
145 %!assert (Ac, Ace, 1e-4);
146 %!assert (Bc, Bce, 1e-4);
147
148
149 ## controllability staircase form of descriptor state-space models (SLICOT TG01HD)
150 %!shared ac, ec, bc, cc, q, z, ncont, ac_e, ec_e, bc_e, cc_e, q_e, z_e, ncont_e
151 %!
152 %! a = [ 2     0     2     0    -1     3     1
153 %!       0     1     0     0     1     0     0
154 %!       0     0     0     1     0     0     1
155 %!       0     0     2     0    -1     3     1
156 %!       0     0     0     1     0     0     1
157 %!       0     1     0     0     1     0     0
158 %!       0     0     0     1     0     0     1 ];
159 %!
160 %! e = [ 0     0     1     0     0     0     0
161 %!       0     0     0     0     0     1     0
162 %!       0     0     0     0     0     0     1
163 %!       0     0     0     0     0     0     1
164 %!       0     0     0     1     0     0     0
165 %!       0     0     1     0    -1     0     0
166 %!       1     3     0     2     0     0     0 ];
167 %!
168 %! b = [ 2     1     0
169 %!       0     0     0
170 %!       0     0     0
171 %!       0     0     0
172 %!       0     0     0
173 %!       0     0     0
174 %!       1     2     3 ];
175 %!
176 %! c = [ 1     0     0     1     0     0     1
177 %!       0    -1     1     0    -1     1     0 ];
178 %!
179 %! tol = 0;
180 %!
181 %! [ac, ec, bc, cc, q, z, ncont] = sltg01hd (a, e, b, c, tol);
182 %!
183 %! ncont_e = 3;
184 %!
185 %! ac_e = [  0.0000   0.0000   0.0000   0.0000  -1.2627   0.4334   0.4666
186 %!           0.0000   2.0000   0.0000  -3.7417  -0.8520   0.2924  -0.4342
187 %!           0.0000   0.0000   1.7862   0.3780  -0.2651  -0.7723   0.0000
188 %!           0.0000   0.0000   0.0000   3.7417   0.8520  -0.2924   0.4342
189 %!           0.0000   0.0000   0.0000   0.0000  -1.5540   0.5334   0.5742
190 %!           0.0000   0.0000   0.0000   0.0000  -0.6533   0.2242   0.2414
191 %!           0.0000   0.0000   0.0000   0.0000  -0.5892   0.2022   0.2177 ];
192 %!
193 %! ec_e = [ -1.8325   1.0000   2.3752   0.0000  -0.8214   0.2819   1.8016
194 %!           0.4887   0.0000   0.3770  -0.5345   0.1874   0.5461   0.0000
195 %!          -0.1728   0.0000  -0.1333  -1.1339   0.1325   0.3861   0.0000
196 %!           0.0000   0.0000   0.0000   0.0000   0.8520  -0.2924   0.4342
197 %!           0.0000   0.0000   0.0000   0.0000  -1.0260  -0.1496   0.0000
198 %!           0.0000   0.0000   0.0000   0.0000   0.0000   1.1937   0.0000
199 %!           0.0000   0.0000   0.0000   0.0000   0.0000   0.0000   1.0000 ];
200 %!
201 %! bc_e = [  1.0000   2.0000   3.0000
202 %!           2.0000   1.0000   0.0000
203 %!           0.0000   0.0000   0.0000
204 %!           0.0000   0.0000   0.0000
205 %!           0.0000   0.0000   0.0000
206 %!           0.0000   0.0000   0.0000
207 %!           0.0000   0.0000   0.0000 ];
208 %!
209 %! cc_e = [  0.0000   1.0000   0.0000   0.0000  -1.2627   0.4334   0.4666
210 %!           0.3665   0.0000  -0.9803  -1.6036   0.1874   0.5461   0.0000 ];
211 %!
212 %! q_e = [   0.0000   1.0000   0.0000   0.0000   0.0000   0.0000   0.0000
213 %!           0.0000   0.0000   0.7071   0.0000   0.2740  -0.6519   0.0000
214 %!           0.0000   0.0000   0.0000   0.0000   0.8304   0.3491  -0.4342
215 %!           0.0000   0.0000   0.0000  -1.0000   0.0000   0.0000   0.0000
216 %!           0.0000   0.0000   0.0000   0.0000   0.4003   0.1683   0.9008
217 %!           0.0000   0.0000   0.7071   0.0000  -0.2740   0.6519   0.0000
218 %!           1.0000   0.0000   0.0000   0.0000   0.0000   0.0000   0.0000 ];
219 %!
220 %! z_e = [   0.0000   1.0000   0.0000   0.0000   0.0000   0.0000   0.0000
221 %!          -0.6108   0.0000   0.7917   0.0000   0.0000   0.0000   0.0000
222 %!           0.4887   0.0000   0.3770  -0.5345   0.1874   0.5461   0.0000
223 %!           0.0000   0.0000   0.0000   0.0000  -0.4107   0.1410   0.9008
224 %!           0.6108   0.0000   0.4713   0.2673  -0.1874  -0.5461   0.0000
225 %!          -0.1222   0.0000  -0.0943  -0.8018  -0.1874  -0.5461   0.0000
226 %!           0.0000   0.0000   0.0000   0.0000  -0.8520   0.2924  -0.4342 ];
227 %!
228 %!assert (ac, ac_e, 1e-4);
229 %!assert (ec, ec_e, 1e-4);
230 %!assert (bc, bc_e, 1e-4);
231 %!assert (cc, cc_e, 1e-4);
232 %!assert (q, q_e, 1e-4);
233 %!assert (z, z_e, 1e-4);
234 %!assert (ncont, ncont_e);
235
236
237 ## observability staircase form of descriptor state-space models (SLICOT TG01ID)
238 %!shared ao, eo, bo, co, q, z, nobsv, ao_e, eo_e, bo_e, co_e, q_e, z_e, nobsv_e
239 %!
240 %! a = [ 2     0     0     0     0     0     0
241 %!       0     1     0     0     0     1     0
242 %!       2     0     0     2     0     0     0
243 %!       0     0     1     0     1     0     1
244 %!      -1     1     0    -1     0     1     0
245 %!       3     0     0     3     0     0     0
246 %!       1     0     1     1     1     0     1 ];
247 %!
248 %! e = [ 0     0     0     0     0     0     1
249 %!       0     0     0     0     0     0     3
250 %!       1     0     0     0     0     1     0
251 %!       0     0     0     0     1     0     2
252 %!       0     0     0     0     0    -1     0
253 %!       0     1     0     0     0     0     0
254 %!       0     0     1     1     0     0     0 ];
255 %!
256 %! b = [ 1     0
257 %!       0    -1
258 %!       0     1
259 %!       1     0
260 %!       0    -1
261 %!       0     1
262 %!       1     0 ];
263 %!
264 %! c = [ 2     0     0     0     0     0     1
265 %!       1     0     0     0     0     0     2
266 %!       0     0     0     0     0     0     3 ];
267 %!
268 %! tol = 0;
269 %!
270 %! [ao, eo, bo, co, q, z, nobsv] = sltg01id (a, e, b, c, tol);
271 %!
272 %! nobsv_e = 3;
273 %!
274 %! ao_e = [  0.2177   0.2414   0.5742   0.4342   0.0000  -0.4342   0.4666
275 %!           0.2022   0.2242   0.5334  -0.2924  -0.7723   0.2924   0.4334
276 %!          -0.5892  -0.6533  -1.5540   0.8520  -0.2651  -0.8520  -1.2627
277 %!           0.0000   0.0000   0.0000   3.7417   0.3780  -3.7417   0.0000
278 %!           0.0000   0.0000   0.0000   0.0000   1.7862   0.0000   0.0000
279 %!           0.0000   0.0000   0.0000   0.0000   0.0000   2.0000   0.0000
280 %!           0.0000   0.0000   0.0000   0.0000   0.0000   0.0000   0.0000 ];
281 %!
282 %! eo_e = [  1.0000   0.0000   0.0000   0.4342   0.0000   0.0000   1.8016
283 %!           0.0000   1.1937  -0.1496  -0.2924   0.3861   0.5461   0.2819
284 %!           0.0000   0.0000  -1.0260   0.8520   0.1325   0.1874  -0.8214
285 %!           0.0000   0.0000   0.0000   0.0000  -1.1339  -0.5345   0.0000
286 %!           0.0000   0.0000   0.0000   0.0000  -0.1333   0.3770   2.3752
287 %!           0.0000   0.0000   0.0000   0.0000   0.0000   0.0000   1.0000
288 %!           0.0000   0.0000   0.0000   0.0000  -0.1728   0.4887  -1.8325 ];
289 %!
290 %! bo_e = [  0.4666   0.0000
291 %!           0.4334   0.5461
292 %!          -1.2627   0.1874
293 %!           0.0000  -1.6036
294 %!           0.0000  -0.9803
295 %!           1.0000   0.0000
296 %!           0.0000   0.3665 ];
297 %!
298 %! co_e = [  0.0000   0.0000   0.0000   0.0000   0.0000   2.0000   1.0000
299 %!           0.0000   0.0000   0.0000   0.0000   0.0000   1.0000   2.0000
300 %!           0.0000   0.0000   0.0000   0.0000   0.0000   0.0000   3.0000 ];
301 %!
302 %! q_e = [   0.0000   0.0000   0.0000   0.0000   0.0000   1.0000   0.0000
303 %!           0.0000   0.0000   0.0000   0.0000   0.7917   0.0000  -0.6108
304 %!           0.0000   0.5461   0.1874  -0.5345   0.3770   0.0000   0.4887
305 %!           0.9008   0.1410  -0.4107   0.0000   0.0000   0.0000   0.0000
306 %!           0.0000  -0.5461  -0.1874   0.2673   0.4713   0.0000   0.6108
307 %!           0.0000  -0.5461  -0.1874  -0.8018  -0.0943   0.0000  -0.1222
308 %!          -0.4342   0.2924  -0.8520   0.0000   0.0000   0.0000   0.0000 ];
309 %!
310 %! z_e = [   0.0000   0.0000   0.0000   0.0000   0.0000   1.0000   0.0000
311 %!           0.0000  -0.6519   0.2740   0.0000   0.7071   0.0000   0.0000
312 %!          -0.4342   0.3491   0.8304   0.0000   0.0000   0.0000   0.0000
313 %!           0.0000   0.0000   0.0000  -1.0000   0.0000   0.0000   0.0000
314 %!           0.9008   0.1683   0.4003   0.0000   0.0000   0.0000   0.0000
315 %!           0.0000   0.6519  -0.2740   0.0000   0.7071   0.0000   0.0000
316 %!           0.0000   0.0000   0.0000   0.0000   0.0000   0.0000   1.0000 ];
317 %!
318 %!assert (ao, ao_e, 1e-4);
319 %!assert (eo, eo_e, 1e-4);
320 %!assert (bo, bo_e, 1e-4);
321 %!assert (co, co_e, 1e-4);
322 %!assert (q, q_e, 1e-4);
323 %!assert (z, z_e, 1e-4);
324 %!assert (nobsv, nobsv_e);
325
326
327 ## ss2tf conversion by Slicot TB04BD
328 ## Test provided by Slicot
329 %!shared NUM, DEN, NUMe, DENe
330 %! A = [ -1.0   0.0   0.0
331 %!        0.0  -2.0   0.0
332 %!        0.0   0.0  -3.0 ];
333 %!
334 %! B = [  0.0   1.0  -1.0
335 %!        1.0   1.0   0.0 ].';
336 %!
337 %! C = [  0.0   1.0   1.0
338 %!        1.0   1.0   1.0 ];
339 %!
340 %! D = [  1.0   0.0
341 %!        0.0   1.0 ];
342 %!
343 %! [NUM, DEN] = tfdata (ss (A, B, C, D));
344 %!
345 %! NUMe = {[1, 5, 7], [1]; [1], [1, 5, 5]};
346 %!
347 %! DENe = {[1, 5, 6], [1, 2]; [1, 5, 6], [1, 3, 2]};
348 %!
349 %!assert (NUM, NUMe, 1e-4);
350 %!assert (DEN, DENe, 1e-4);
351
352
353 ## ss2tf conversion by Slicot TB04BD
354 ## Trivial test
355 %!shared NUM, DEN, NUMe, DENe
356 %! A = [  0 ];
357 %!
358 %! B = [  1 ];
359 %!
360 %! C = [  1 ];
361 %!
362 %! D = [  0 ];
363 %!
364 %! [NUM, DEN] = tfdata (ss (A, B, C, D));
365 %!
366 %! NUMe = {[1]};
367 %!
368 %! DENe = {[1, 0]};
369 %!
370 %!assert (NUM, NUMe, 1e-4);
371 %!assert (DEN, DENe, 1e-4);
372
373
374 ## transfer function to state-space conversion
375 ## test from SLICOT TD04AD
376 %!shared Mo, Me
377 %! INDEX =  [  3     3 ];
378 %!
379 %! DCOEFF = [  1.0   6.0  11.0   6.0
380 %!             1.0   6.0  11.0   6.0 ];
381 %!
382 %! UCOEFF = zeros (2, 2, 4);
383 %!
384 %! u11 = [ 1.0   6.0  12.0   7.0 ];
385 %! u12 = [ 0.0   1.0   4.0   3.0 ];
386 %! u21 = [ 0.0   0.0   1.0   1.0 ];
387 %! u22 = [ 1.0   8.0  20.0  15.0 ];
388 %!
389 %! UCOEFF(1,1,:) = u11;
390 %! UCOEFF(1,2,:) = u12;
391 %! UCOEFF(2,1,:) = u21;
392 %! UCOEFF(2,2,:) = u22;
393 %!
394 %! [Ao, Bo, Co, Do] = sltd04ad (UCOEFF, DCOEFF, INDEX, 0);
395 %!
396 %! Ae = [  0.5000  -0.8028   0.9387
397 %!         4.4047  -2.3380   2.5076
398 %!        -5.5541   1.6872  -4.1620 ];
399 %!
400 %! Be = [ -0.2000  -1.2500
401 %!         0.0000  -0.6097
402 %!         0.0000   2.2217 ];
403 %!
404 %! Ce = [  0.0000  -0.8679   0.2119
405 %!         0.0000   0.0000   0.9002 ];
406 %!
407 %! De = [  1.0000   0.0000
408 %!         0.0000   1.0000 ];
409 %!
410 %! Mo = [Ao, Bo; Co, Do];
411 %! Me = [Ae, Be; Ce, De];
412 %!
413 %!assert (Mo, Me, 1e-4);