]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/lyapchol.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / lyapchol.m
1 ## Copyright (C) 2010   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 ## @deftypefn{Function File} {@var{u} =} lyapchol (@var{a}, @var{b})
20 ## @deftypefnx{Function File} {@var{u} =} lyapchol (@var{a}, @var{b}, @var{e})
21 ## Compute Cholesky factor of continuous-time Lyapunov equations.
22 ##
23 ## @strong{Equations}
24 ## @example
25 ## @group
26 ## A U' U  +  U' U A'  +  B B'  =  0           (Lyapunov Equation)
27 ##
28 ## A U' U E'  +  E U' U A'  +  B B'  =  0      (Generalized Lyapunov Equation)
29 ## @end group
30 ## @end example
31 ##
32 ## @strong{Algorithm}@*
33 ## Uses SLICOT SB03OD and SG03BD by courtesy of
34 ## @uref{http://www.slicot.org, NICONET e.V.}
35 ##
36 ## @seealso{lyap, dlyap, dlyapchol}
37 ## @end deftypefn
38
39 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
40 ## Created: January 2010
41 ## Version: 0.2.1
42
43 function [u, scale] = lyapchol (a, b, e)
44
45   switch (nargin)
46     case 2
47       
48       if (! is_real_square_matrix (a))
49         ## error ("lyapchol: a must be real and square");
50         error ("lyapchol: %s must be real and square", \
51                 inputname (1));
52       endif
53
54       if (! is_real_matrix (b))
55         ## error ("lyapchol: b must be real")
56         error ("lyapchol: %s must be real", \
57                 inputname (2))
58       endif
59   
60       if (rows (a) != rows (b))
61         ## error ("lyapchol: a and b must have the same number of rows");
62         error ("lyapchol: %s and %s must have the same number of rows", \
63                 inputname (1), inputname (2));
64       endif
65
66       [u, scale] = slsb03od (a.', b.', false);
67
68       ## NOTE: TRANS = 'T' not suitable because we need U' U, not U U'
69
70     case 3
71
72       if (! is_real_square_matrix (a, e))
73         ## error ("lyapchol: a, e must be real and square");
74         error ("lyapchol: %s, %s must be real and square", \
75                 inputname (1), inputname (3));
76       endif
77
78       if (! is_real_matrix (b))
79         ## error ("lyapchol: b must be real");
80         error ("lyapchol: %s must be real", \
81                 inputname (2));
82       endif
83
84       if (rows (b) != rows (a) || rows (e) != rows (a))
85         ## error ("lyapchol: a, b, e must have the same number of rows");
86         error ("lyapchol: %s, %s, %s must have the same number of rows", \
87                 inputname (1), inputname (2), inputname (3));
88       endif
89
90       [u, scale] = slsg03bd (a.', e.', b.', false);
91
92       ## NOTE: TRANS = 'T' not suitable because we need U' U, not U U'
93
94     otherwise
95       print_usage ();
96
97   endswitch
98
99   if (scale < 1)
100     warning ("lyapchol: solution scaled by %g to prevent overflow", scale);
101   endif
102
103 endfunction
104
105
106 %!shared U, U_exp, X, X_exp
107 %!
108 %! A = [ -1.0  37.0 -12.0 -12.0
109 %!       -1.0 -10.0   0.0   4.0
110 %!        2.0  -4.0   7.0  -6.0
111 %!        2.0   2.0   7.0  -9.0 ].';
112 %!
113 %! B = [  1.0   2.5   1.0   3.5
114 %!        0.0   1.0   0.0   1.0
115 %!       -1.0  -2.5  -1.0  -1.5
116 %!        1.0   2.5   4.0  -5.5
117 %!       -1.0  -2.5  -4.0   3.5 ].';
118 %!
119 %! U = lyapchol (A, B);
120 %!
121 %! X = U.' * U;  # use lyap at home!
122 %!
123 %! U_exp = [  1.0000   0.0000   0.0000   0.0000
124 %!            3.0000   1.0000   0.0000   0.0000
125 %!            2.0000  -1.0000   1.0000   0.0000
126 %!           -1.0000   1.0000  -2.0000   1.0000 ].';
127 %!
128 %! X_exp = [  1.0000   3.0000   2.0000  -1.0000
129 %!            3.0000  10.0000   5.0000  -2.0000
130 %!            2.0000   5.0000   6.0000  -5.0000
131 %!           -1.0000  -2.0000  -5.0000   7.0000 ];
132 %!
133 %!assert (U, U_exp, 1e-4);
134 %!assert (X, X_exp, 1e-4);
135
136 %!shared U, U_exp, X, X_exp
137 %!
138 %! A = [ -1.0    3.0   -4.0
139 %!        0.0    5.0   -2.0
140 %!       -4.0    4.0    1.0 ].';
141 %!
142 %! E = [  2.0    1.0    3.0
143 %!        2.0    0.0    1.0
144 %!        4.0    5.0    1.0 ].';
145 %!
146 %! B = [  2.0   -1.0    7.0 ].';
147 %!
148 %! U = lyapchol (A, B, E);
149 %!
150 %! U_exp = [  1.6003  -0.4418  -0.1523
151 %!            0.0000   0.6795  -0.2499
152 %!            0.0000   0.0000   0.2041 ];
153 %!
154 %!assert (U, U_exp, 1e-4);