1 ## Copyright (C) 1996, 2000, 2003, 2004, 2005, 2007
2 ## Auburn University. All rights reserved.
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.
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.
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/>.
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}.
32 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu>
34 ## Adapted-By: Lukas Reichlin <lukas.reichlin@gmail.com>
38 function W = gram (argin1, argin2)
44 if (ischar (argin2)) # the function was called as "gram (sys, mode)"
47 if (! isa (sys, "lti"))
48 error ("gram: first argument must be an LTI model");
51 [a, b, c] = ssdata (sys);
53 if (strncmpi (argin2, "o", 1))
56 elseif (! strncmpi (argin2, "c", 1))
59 else # the function was called as "gram (a, b)"
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)));
69 error ("gram: system matrix a must be stable");
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
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)))
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)))
96 %! a = [-1 0 0; 1/2 -1 0; 1/2 0 -1];
97 %! b = [1 0; 0 -1; 0 1];
99 %! assert (a * Wc + Wc * a.' + b * b.', zeros (size (a)))
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)
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)