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