]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/gram.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / gram.m
1 ## Copyright (C) 1996, 2000, 2003, 2004, 2005, 2007
2 ##               Auburn University. All rights reserved.
3 ##
4 ##
5 ## This program is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## This program is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{W} =} gram (@var{sys}, @var{mode})
21 ## @deftypefnx {Function File} {@var{Wc} =} gram (@var{a}, @var{b})
22 ## @code{gram (@var{sys}, "c")} returns the controllability gramian of
23 ## the (continuous- or discrete-time) system @var{sys}.
24 ## @code{gram (@var{sys}, "o")} returns the observability gramian of the
25 ## (continuous- or discrete-time) system @var{sys}.
26 ## @code{gram (@var{a}, @var{b})} returns the controllability gramian
27 ## @var{Wc} of the continuous-time system @math{dx/dt = a x + b u};
28 ## i.e., @var{Wc} satisfies @math{a Wc + m Wc' + b b' = 0}.
29 ##
30 ## @end deftypefn
31
32 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu>
33
34 ## Adapted-By: Lukas Reichlin <lukas.reichlin@gmail.com>
35 ## Date: October 2009
36 ## Version: 0.2
37
38 function W = gram (argin1, argin2)
39
40   if (nargin != 2)
41     print_usage ();
42   endif
43
44   if (ischar (argin2))     # the function was called as "gram (sys, mode)"
45     sys = argin1;
46
47     if (! isa (sys, "lti"))
48       error ("gram: first argument must be an LTI model");
49     endif
50
51     [a, b, c] = ssdata (sys);
52
53     if (strncmpi (argin2, "o", 1))
54       a = a.';
55       b = c.';
56     elseif (! strncmpi (argin2, "c", 1))
57       print_usage ();
58     endif
59   else                     # the function was called as "gram (a, b)"
60     a = argin1;
61     b = argin2;
62
63     ## assume that a and b are matrices of continuous-time system
64     ## values of b, c and d are irrelevant for system stability
65     sys = ss (a, b, zeros (1, columns (a)), zeros (1, columns (b)));
66   endif
67
68   if (! isstable (sys))
69     error ("gram: system matrix a must be stable");
70   endif
71
72   if (isct (sys))
73     W = lyap (a, b*b.');   # let lyap do the error checking about dimensions
74   else  # discrete-time system
75     W = dlyap (a, b*b.');  # let dlyap do the error checking about dimensions
76   endif
77
78 endfunction
79
80
81 %!test
82 %! a = [-1 0 0; 1/2 -1 0; 1/2 0 -1];
83 %! b = [1 0; 0 -1; 0 1];
84 %! c = [0 0 1; 1 1 0]; ## it doesn't matter what the value of c is
85 %! Wc = gram (ss (a, b, c), "c");
86 %! assert (a * Wc + Wc * a.' + b * b.', zeros (size (a)))
87
88 %!test
89 %! a = [-1 0 0; 1/2 -1 0; 1/2 0 -1];
90 %! b = [1 0; 0 -1; 0 1]; ## it doesn't matter what the value of b is
91 %! c = [0 0 1; 1 1 0];
92 %! Wo = gram (ss (a, b, c), "o");
93 %! assert (a.' * Wo + Wo * a + c.' * c, zeros (size (a)))
94
95 %!test
96 %! a = [-1 0 0; 1/2 -1 0; 1/2 0 -1];
97 %! b = [1 0; 0 -1; 0 1];
98 %! Wc = gram (a, b);
99 %! assert (a * Wc + Wc * a.' + b * b.', zeros (size (a)))
100
101 %!test
102 %! a = [-1 0 0; 1/2 1 0; 1/2 0 -1] / 2;
103 %! b = [1 0; 0 -1; 0 1];
104 %! c = [0 0 1; 1 1 0]; ## it doesn't matter what the value of c is
105 %! d = zeros (rows (c), columns (b)); ## it doesn't matter what the value of d is
106 %! Ts = 0.1; ## Ts != 0
107 %! Wc = gram (ss (a, b, c, d, Ts), "c");
108 %! assert (a * Wc * a.' - Wc + b * b.', zeros (size (a)), 1e-12)
109
110 %!test
111 %! a = [-1 0 0; 1/2 1 0; 1/2 0 -1] / 2;
112 %! b = [1 0; 0 -1; 0 1]; ## it doesn't matter what the value of b is
113 %! c = [0 0 1; 1 1 0];
114 %! d = zeros (rows (c), columns (b)); ## it doesn't matter what the value of d is
115 %! Ts = 0.1; ## Ts != 0
116 %! Wo = gram (ss (a, b, c, d, Ts), "o");
117 %! assert (a.' * Wo * a - Wo + c.' * c, zeros (size (a)), 1e-12)