1 ## Copyright (C) 2012 Lukas F. Reichlin
2 ## Copyright (C) 2012 Megan Zagrobelny
4 ## This file is part of LTI Syncope.
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.
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.
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/>.
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.
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
37 ## State transition matrix of discrete-time system (n-by-n).
39 ## Process noise matrix of discrete-time system (n-by-g).
40 ## If @var{g} is empty @code{[]}, an identity matrix is assumed.
42 ## Measurement matrix of discrete-time system (p-by-n).
44 ## Process noise covariance matrix (g-by-g).
46 ## Measurement noise covariance matrix (p-by-p).
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.
55 ## Kalman filter gain matrix (n-by-p).
57 ## Unique stabilizing solution of the discrete-time Riccati equation (n-by-n).
60 ## Error covariance (n-by-n), cov(x(k|k)-x)
62 ## Closed-loop poles (n-by-1).
68 ## x[k|k] = x[k|k-1] + M(y[k] - Cx[k|k-1] - Du[k])
70 ## x[k+1|k] = Ax[k|k] + Bu[k] for S=0
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
75 ## E = eig(A - A*M*C) for S=0
77 ## E = eig(A - A*M*C - G*S*(C*P*C' + Rv)^-1*C) for non-zero S
81 ## @seealso{dare, care, dlqr, lqr, lqe}
84 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
85 ## Created: April 2012
88 function [m, p, z, e] = dlqe (a, g, c, q, r, s = [])
90 if (nargin < 5 || nargin > 6)
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));
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));
105 [p, e] = dare (a.', c.', g*q*g.', r, g*s);
108 m = p*c.' / (c*p*c.' + r);