]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/lqe.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / lqe.m
1 ## Copyright (C) 2012   Lukas F. Reichlin
2 ## Copyright (C) 2012   Megan Zagrobelny
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} {[@var{l}, @var{p}, @var{e}] =} lqe (@var{sys}, @var{q}, @var{r})
21 ## @deftypefnx {Function File} {[@var{l}, @var{p}, @var{e}] =} lqe (@var{sys}, @var{q}, @var{r}, @var{s})
22 ## @deftypefnx {Function File} {[@var{l}, @var{p}, @var{e}] =} lqe (@var{a}, @var{g}, @var{c}, @var{q}, @var{r})
23 ## @deftypefnx {Function File} {[@var{l}, @var{p}, @var{e}] =} lqe (@var{a}, @var{g}, @var{c}, @var{q}, @var{r}, @var{s})
24 ## @deftypefnx {Function File} {[@var{l}, @var{p}, @var{e}] =} lqe (@var{a}, @var{[]}, @var{c}, @var{q}, @var{r})
25 ## @deftypefnx {Function File} {[@var{l}, @var{p}, @var{e}] =} lqe (@var{a}, @var{[]}, @var{c}, @var{q}, @var{r}, @var{s})
26 ## Kalman filter for continuous-time systems.
27 ##
28 ## @example
29 ## @group
30 ## .
31 ## x = Ax + Bu + Gw   (State equation)
32 ## y = Cx + Du + v    (Measurement Equation)
33 ## E(w) = 0, E(v) = 0, cov(w) = Q, cov(v) = R, cov(w,v) = S
34 ## @end group
35 ## @end example
36 ##
37 ## @strong{Inputs}
38 ## @table @var
39 ## @item sys
40 ## Continuous or discrete-time LTI model (p-by-m, n states).
41 ## @item a
42 ## State transition matrix of continuous-time system (n-by-n).
43 ## @item g
44 ## Process noise matrix of continuous-time system (n-by-g).
45 ## If @var{g} is empty @code{[]}, an identity matrix is assumed.
46 ## @item c
47 ## Measurement matrix of continuous-time system (p-by-n).
48 ## @item q
49 ## Process noise covariance matrix (g-by-g).
50 ## @item r
51 ## Measurement noise covariance matrix (p-by-p).
52 ## @item s
53 ## Optional cross term covariance matrix (g-by-p), s = cov(w,v).
54 ## If @var{s} is empty @code{[]} or not specified, a zero matrix is assumed.
55 ## @end table
56 ##
57 ## @strong{Outputs}
58 ## @table @var
59 ## @item l
60 ## Kalman filter gain matrix (n-by-p).
61 ## @item p
62 ## Unique stabilizing solution of the continuous-time Riccati equation (n-by-n).
63 ## Symmetric matrix.  If @var{sys} is a discrete-time model, the solution of the
64 ## corresponding discrete-time Riccati equation is returned.
65 ## @item e
66 ## Closed-loop poles (n-by-1).
67 ## @end table
68 ##
69 ## @strong{Equations}
70 ## @example
71 ## @group
72 ## .
73 ## x = Ax + Bu + L(y - Cx -Du)          
74 ##
75 ## E = eig(A - L*C)
76 ## 
77 ## @end group
78 ## @end example
79 ## @seealso{dare, care, dlqr, lqr, dlqe}
80 ## @end deftypefn
81
82 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
83 ## Created: April 2012
84 ## Version: 0.1
85
86 function [l, p, e] = lqe (a, g, c, q = [], r = [], s = [])
87
88   if (nargin < 3 || nargin > 6)
89     print_usage ();
90   endif
91
92   if (isa (a, "lti"))
93     [l, p, e] = lqr (a.', g, c, q);         # lqe (sys, q, r, s), g=I, works like  lqr (sys.', q, r, s).'
94   elseif (isempty (g))
95     [l, p, e] = lqr (a.', c.', q, r, s);    # lqe (a, [], c, q, r, s), g=I, works like  lqr (a.', c.', q, r, s).'
96   elseif (columns (g) != rows (q) || ! issquare (q))
97     error ("lqe: matrices g(%dx%d) and q(%dx%d) have incompatible dimensions", \
98             rows (g), columns (g), rows (q), columns (q));
99   elseif (isempty (s))
100     [l, p, e] = lqr (a.', c.', g*q*g.', r);
101   elseif (columns (g) != rows (s))
102     error ("lqe: matrices g(%dx%d) and s(%dx%d) have incompatible dimensions", \
103             rows (g), columns (g), rows (s), columns (s));
104   else
105     [l, p, e] = lqr (a.', c.', g*q*g.', r, g*s);
106   endif
107
108   l = l.';
109   
110   ## NOTE: for discrete-time sys, the solution L' from DARE
111   ##       is different to L from DLQE (a, s)
112
113 endfunction