数学建模社区-数学中国

标题: 卡尔曼算法的matlab程序 [打印本页]

作者: 工科男    时间: 2011-11-26 16:16
标题: 卡尔曼算法的matlab程序
求卡尔曼算法的matlab预测程序
作者: 厚积薄发    时间: 2011-11-28 10:48
matlab下面的kalman滤波程序: P, a( k. `9 b- C" V+ v8 q+ ?
clear  N=200; w(1)=0;   4 k+ c" T0 j+ t/ }& s
w=randn(1,N)
6 ^4 n1 L* _3 I( N9 h0 zx(1)=0;   
+ x$ I" p% T. `5 ma=1;   0 |1 {" Q: x& {* `* C- v0 T3 t
for k=2:N;   
3 j8 U9 s* ~$ a  x" W8 r2 {+ jx(k)=a*x(k-1)+w(k-1);   
9 r, E! F  Y2 G; [- B7 ~end   
; A8 e" `, D6 @4 o7 @- g! d3 jV=randn(1,N);   - i" V1 \! H$ K% B& [3 z4 ]0 m
q1=std(V);   
  s1 }* P( Z4 Q( b1 a/ W7 RRvv=q1.^2;   
# d1 @! `* j9 p: r! V& ]q2=std(x);   ' R3 H# ~4 Y! n! e
Rxx=q2.^2;   ! p2 C# f" O3 ^/ s  {
q3=std(w);   
5 U+ w/ u- u( B) lRww=q3.^2;   
7 O* b4 ^, u; ^6 q2 o9 }4 g) Sc=0.2;   + t' y% C0 f1 |8 m! l4 K; \
Y=c*x+V;   1 W& ~! h( t# n# p& m- u. t; n
p(1)=0;   
) a' }) ^3 U2 }2 h: l8 F2 n* r  D& xs(1)=0;1 D+ c7 L, W! h3 g+ [2 Z  \
for t=2:N;   
% o# a# `+ _6 E* l5 O3 l! zp1(t)=a.^2*p(t-1)+Rww;   
5 R8 L! W9 v3 _b(t)=c*p1(t)/(c.^2*p1(t)+Rvv);   3 W+ ?  v. x. F- Q* f
s(t)=a*s(t-1)+b(t)*(Y(t)-a*c*s(t-1));   8 v- b, |8 b; B
p(t)=p1(t)-c*b(t)*p1(t);   
% I/ x8 V% Q4 }! _5 Q$ Z3 D( H$ Yend   ! k% I$ K) l! M$ v) W
t=1:N;   
+ k+ C  D$ Q' K! I# {plot(t,s,'r',t,Y,'g',t,x,'b');   ( t# Y7 e: k+ W
function [x, V, VV, loglik] = kalman_filter(y, A, C, Q, R, init_x, init_V, varargin)   
9 b4 M9 y& {/ D$ k! D% Kalman filter.   
+ W! e! a  D% @% [x, V, VV, loglik] = kalman_filter(y, A, C, Q, R, init_x, init_V, ...)   8 B$ C# J7 r! L; S( r
%   
6 K2 s0 X; [1 j( i$ M$ Y7 [% INPUTS:   $ m  m( m. o4 N1 C
% y(:,t) - the observation at time t   / ]$ g, s0 f) h- c3 |
% A - the system matrix   6 O1 v3 ^3 c( C/ g( ]1 R
% C - the observation matrix   2 X- C$ F" e9 {1 j0 T9 c3 x7 a
% Q - the system covariance   
1 g' l6 ^# ]  ^. b6 v) a% R - the observation covariance   4 T8 l) m8 f; k( r4 p
% init_x - the initial state (column) vector   0 U5 x+ P0 q1 a" @+ T  q' \
% init_V - the initial state covariance   * z0 |" j) _& @% @2 o8 z
%   
5 @: s5 [! Y, N+ S. M0 K6 U% OPTIONAL INPUTS (string/value pairs [default in brackets])   
* [7 x8 i, o2 U% 'model' - model(t)=m means use params from model m at time t [ones(1,T) ]   2 v) N& I# U9 U! l5 M9 I" U
% In this case, all the above matrices take an additional final dimension,   
- k% Y. Q. }% s3 O9 a2 |9 ^5 r% i.e., A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m).   
2 d, Q) N5 C( c; j" j9 Q; G% However, init_x and init_V are independent of model(1).   ' H; L2 E) d' S' h" j3 ^- x' x! D
% 'u' - u(:,t) the control signal at time t [ [] ]   . u6 {3 s3 N6 Y0 O1 B. L) m
% 'B' - B(:,:,m) the input regression matrix for model m   9 e& K4 V3 W2 a& q! a
%   % |* T, x" ^3 m- W, ?5 c1 n
% OUTPUTS (where X is the hidden state being estimated)   
( d1 f6 _# L& I# z3 d  Z& E5 P% x(:,t) = E[X(:,t) | y(:,1:t)]   
' R$ u4 m8 m. v/ d( L2 C6 J% V(:,:,t) = Cov[X(:,t) | y(:,1:t)]   
; w8 S9 ^! Z5 n2 w% VV(:,:,t) = Cov[X(:,t), X(:,t-1) | y(:,1:t)] t >= 2   
5 ]; c$ s/ ]/ z) A5 S% loglik = sum{t=1}^T log P(y(:,t))   ! W) J6 a. A; g2 U1 l7 F. ?7 ]
%   9 k+ A' p# K2 J# M" z6 O: s6 A: f
% If an input signal is specified, we also condition on it:   5 I& t& i8 _2 M6 c& o! h% N5 F
% e.g., x(:,t) = E[X(:,t) | y(:,1:t), u(:, 1:t)]   7 v, W: T& x1 O- u- G7 {: \* }2 L
% If a model sequence is specified, we also condition on it:   : Z1 P, L- n3 u, |8 i
% e.g., x(:,t) = E[X(:,t) | y(:,1:t), u(:, 1:t), m(1:t)]   
8 c+ I/ j- l8 h3 F" A, d# R$ G, E4 A[os T] = size(y);   
6 k. v* ]/ E' j) Z( _ss = size(A,1); % size of state space   
4 n, o% i4 G+ w% Y& I1 `: E% set default params   1 X8 R# z) _) }/ G1 }- j
model = ones(1,T);   / `/ S* O+ ]. k) X. i) r  o+ ?
u = [];   3 M- j5 h* m) L1 T0 ?  W1 @4 F4 \! z7 o2 q& z
B = [];   1 [  }8 D6 Y8 Z% K2 s; d& `/ Y
ndx = [];   
9 e" l! a9 T6 V* r! g) o0 bargs = varargin;   
+ h  O5 H4 k3 |: znargs = length(args);   0 Y! D, b7 P, J# y5 ^( n, {
for i=1:2:nargs   . x+ F+ I: r+ e
switch args   
2 n  F2 j! b( d, I$ Ocase 'model', model = args{i+1};   1 ^1 C2 g* J6 l1 U9 U6 X
case 'u', u = args{i+1};   . P5 w$ G5 Z: v0 r2 Q9 z0 }
case 'B', B = args{i+1};   
3 K0 x0 }" E1 @! ncase 'ndx', ndx = args{i+1};   
0 k  \% s- W. E. q2 l; Votherwise, error(['unrecognized argument ' args])   / t  o1 }& Q' @7 m5 R! k5 E  T. S
end   5 i  X* A9 K5 i! D& M; Q
end   
  s0 m) C0 d2 Ux = zeros(ss, T);   
6 l$ n& f( T# e. F5 {7 fV = zeros(ss, ss, T);   
" J( I' d; _# g9 U2 eVV = zeros(ss, ss, T);   # F  D6 A) b& A4 T: p4 @
loglik = 0;   ! N1 y5 `8 q& f* c1 D/ M
for t=1:T   m = model(t);   ! q. ~) l* Q! z6 j
if t==1   %prevx = init_x(:,m);   & ^* e' d/ `1 n! m, A, d( e
%prevV = init_V(:,:,m);   
- Z" @8 \  H6 b: h- ?prevx = init_x;   
$ ?. _/ U0 J2 M, mprevV = init_V;   
6 n. T+ H/ n& ?# n6 M' h/ binitial = 1;   : d6 z$ D6 b6 W- s
else   prevx = x(:,t-1);   : ~6 A" ^. W* n+ [# u  P. {: ]% l1 ?9 V0 D
prevV = V(:,:,t-1);   - P: M( U; U. Z* g6 G
initial = 0;   * C8 {, _# ~7 ^9 d3 b: [& R8 d; R
end   ; c  g9 f" d& w: G
if isempty(u)   
9 d' ^  Q2 M6 d2 C8 y: H9 |( \[x(:,t), V(:,:,t), LL, VV(:,:,t)] = ...   
/ n& B# `4 J! F- s" hkalman_update(A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m), y(:,t), prevx, prevV, 'initial', initial);   else   + d2 a+ w5 d# E* U' C
  if isempty(ndx)   [x(:,t), V(:,:,t), LL, VV(:,:,t)] = ...   
4 O5 Z7 T% [+ v$ N- T! e# k5 O5 ~6 M! t     kalman_update(A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m), y(:,t), prevx, prevV, ...   'initial', initial,     'u', u(:,t), 'B', B(:,:,m));   ) A- @3 \7 i1 f8 O
else   # b/ p& D& }6 e- R
i = ndx;   
7 N: K, }- P: b3 i1 p% copy over all elements; only some will get updated   x(:,t) = prevx;   
+ _) Q: @. [5 c- z3 FprevP = inv(prevV);   ! A$ y8 F, \/ v
prevPsmall = prevP(i,i);   ' l7 m1 ]5 t; J
prevVsmall = inv(prevPsmall);   
( l2 p; U" k/ o[x(i,t), smallV, LL, VV(i,i,t)] = ...   kalman_update(A(i,i,m), C(:,i,m), Q(i,i,m), R(:,:,m), y(:,t), prevx(i), prevVsmall, ...   'initial', initial, 'u', u(:,t), 'B', B(i,:,m));   3 }- }" B  m7 t5 l8 D4 [! k* J/ l
smallP = inv(smallV);   / \) g2 l& {! Q
prevP(i,i) = smallP;   $ X0 p* [2 r/ |
V(:,:,t) = inv(prevP);   2 d9 `; w( ]$ o8 P
end   / X% x1 c6 ?- a' y+ R5 ?5 K) `' q& e
end   ' r& T# w" |) u
loglik = loglik + LL;   
; s% w7 a' E9 Z5 K8 v& send
作者: 剑游九天    时间: 2011-12-19 23:38
厚积薄发 发表于 2011-11-28 10:48
2 g3 h  l" B- c& K) o  V; Nmatlab下面的kalman滤波程序$ C2 O6 |8 K6 S" U. `* L6 _
clear  N=200; w(1)=0;   
0 Q6 R+ K( v& N1 n+ Z8 T: h* xw=randn(1,N)

! X& }/ o8 Q" \强大呀,下了
作者: 工科男    时间: 2012-2-5 10:24
太强大了
作者: 244190977    时间: 2013-10-7 19:59
太令人。。。。
作者: yulun9988    时间: 2014-1-13 21:45
谢谢。。。。。。。。。。。。。。。。。。。。。。。。。。
作者: yulun9988    时间: 2014-1-13 21:45





欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5