数学建模社区-数学中国

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

作者: 工科男    时间: 2011-11-26 16:16
标题: 卡尔曼算法的matlab程序
求卡尔曼算法的matlab预测程序
作者: 厚积薄发    时间: 2011-11-28 10:48
matlab下面的kalman滤波程序; s( r& T: q" C6 D+ b
clear  N=200; w(1)=0;   
" H, h# i+ u$ i' k" Pw=randn(1,N)
3 U# j9 ]$ h8 q8 I( N/ F' g  Kx(1)=0;   , h. f( M' j6 c$ ]/ V
a=1;   
3 ]  a% K6 P/ P. ?  ?$ y* \for k=2:N;     O. ?7 _4 O  ]' t
x(k)=a*x(k-1)+w(k-1);   % @9 s, [) z7 o( M
end   
4 }5 w/ H9 o: wV=randn(1,N);   : E! K, h5 L% q1 c- y
q1=std(V);   
  F! N7 @* |* c* M" E# ?Rvv=q1.^2;   5 G* Y+ D  c! c& q! S9 W
q2=std(x);   ) B) r: ?8 l/ g& ]( y
Rxx=q2.^2;   ( V# D* q5 r: w  {( ]" |
q3=std(w);   6 x4 F9 h( c/ C! Y. `
Rww=q3.^2;   4 r7 E" M$ h) G, j
c=0.2;   ' [2 I) n4 y  q# h6 B, y* _
Y=c*x+V;   3 |, J" P6 Y  q$ ]# p
p(1)=0;   ; W4 P9 S% b, V1 W' J
s(1)=0;) U) O, ^) r; G' C6 g& ^9 s6 n/ H
for t=2:N;   $ X- s" r* S, W$ r: O0 S4 F
p1(t)=a.^2*p(t-1)+Rww;   
, Q  g4 H4 r$ q' N3 Z, kb(t)=c*p1(t)/(c.^2*p1(t)+Rvv);   
& N" ]6 h0 w& w7 ]& \4 d' Q" ms(t)=a*s(t-1)+b(t)*(Y(t)-a*c*s(t-1));   
. X4 N! Q' v8 E5 E& Xp(t)=p1(t)-c*b(t)*p1(t);   
& x6 H* U, h+ t+ l4 |! P7 Oend   
8 \; S4 T+ A0 ?t=1:N;   7 R3 k7 V/ J: W* Z
plot(t,s,'r',t,Y,'g',t,x,'b');   1 N0 W* K- B- W* |
function [x, V, VV, loglik] = kalman_filter(y, A, C, Q, R, init_x, init_V, varargin)   
* ~- O( e# Q* ^5 t% Kalman filter.     u0 g1 {; M/ r
% [x, V, VV, loglik] = kalman_filter(y, A, C, Q, R, init_x, init_V, ...)   
3 s+ i3 C8 w4 {' g* L$ K%   " }0 j% I& z+ u4 N" N; b
% INPUTS:   . g, ~  l# i  d' a) i( b. ^
% y(:,t) - the observation at time t   0 y/ e! m0 z( @4 v2 _8 ]
% A - the system matrix   0 p: i- c5 S& s
% C - the observation matrix   
( |' W& y# O1 @) i9 H% Q - the system covariance   
, j' T% ?( A7 w" F8 t% R - the observation covariance   
6 r$ g( a3 Q7 ?- O# s% init_x - the initial state (column) vector   
, O) ?5 f! C6 s% init_V - the initial state covariance   ! ?% p- F/ z" ], K
%   3 C' \3 X7 S) V1 ~2 }$ w
% OPTIONAL INPUTS (string/value pairs [default in brackets])   ; V( a6 f6 o+ F7 I
% 'model' - model(t)=m means use params from model m at time t [ones(1,T) ]   
& \, e, B+ b! y" E- s9 h( Q) [1 D% In this case, all the above matrices take an additional final dimension,   ( s9 s  [) x) @* F+ a
% i.e., A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m).   
# d0 F' w4 Z5 Z0 f* N! u( t7 R! X8 g% However, init_x and init_V are independent of model(1).   ! D% }7 ?4 D& L8 z( B' s' z
% 'u' - u(:,t) the control signal at time t [ [] ]   / h/ A. q' V  V+ A
% 'B' - B(:,:,m) the input regression matrix for model m   
  M1 m  z6 l& R- z+ _3 \%   9 y( x: {( J2 G0 O' ]# V0 S* a
% OUTPUTS (where X is the hidden state being estimated)   
6 }/ _  L/ [! b8 r: N  z% x(:,t) = E[X(:,t) | y(:,1:t)]   " {2 k; J9 U) c1 K1 A6 a& e$ }' W
% V(:,:,t) = Cov[X(:,t) | y(:,1:t)]   
* R; W1 y6 O6 L- Z: o1 \+ g, r0 t% VV(:,:,t) = Cov[X(:,t), X(:,t-1) | y(:,1:t)] t >= 2   7 A6 l4 C( o- X, a8 L5 z' f. O
% loglik = sum{t=1}^T log P(y(:,t))   
: s4 }, s+ O, {- i%   
  l1 L$ u. J+ w- a' x+ ^( s0 V2 E3 i% If an input signal is specified, we also condition on it:   
/ D( u8 O3 i4 |& L, X) i! v% L% e.g., x(:,t) = E[X(:,t) | y(:,1:t), u(:, 1:t)]   
& G5 ]& R8 r4 z6 R% If a model sequence is specified, we also condition on it:   7 `- Y# E6 P# J/ c) b- b; R
% e.g., x(:,t) = E[X(:,t) | y(:,1:t), u(:, 1:t), m(1:t)]   
3 [8 W' B! K. N5 N, S" i[os T] = size(y);   9 w8 @; z+ O8 m! l) X8 U
ss = size(A,1); % size of state space   . t3 F, U+ Q' e0 x1 ]
% set default params   
: ?5 K2 u6 \' b1 O" d5 i! s$ _model = ones(1,T);   
2 S( I) b! o9 {5 r& u" Cu = [];   
: T, q1 z" k1 H, P0 gB = [];   
4 X4 a8 U# i/ f' o) Sndx = [];   
& r4 C  ?3 T4 v% H5 ^- cargs = varargin;   
! |# l! W$ [) h! Q9 [3 Wnargs = length(args);   0 }( d* \) G7 R5 m  o
for i=1:2:nargs   
: ]% q0 F7 H0 J& u" \" d. u' `switch args   6 y& e. O( A3 b4 l% @+ A
case 'model', model = args{i+1};   
9 r8 o! a8 V( Y( L: M% jcase 'u', u = args{i+1};   - i5 q& p& }9 l% A9 j
case 'B', B = args{i+1};   # R" Q' A! i! c% l
case 'ndx', ndx = args{i+1};   ; s6 d, r& U9 e0 V5 P5 \' n2 O
otherwise, error(['unrecognized argument ' args])   
  I7 z' s* a0 f/ ?; cend   / x' Q  C+ E+ C9 J
end   & N, E- C! T& v, N* H
x = zeros(ss, T);   1 k6 o6 D7 H& o. J2 f7 d1 w$ P, p
V = zeros(ss, ss, T);   
# A, x% \; U+ }. n% yVV = zeros(ss, ss, T);   
: d% [, ]  d, Z" O6 {loglik = 0;   " J! J4 E  E/ i8 U. G! v2 o% M
for t=1:T   m = model(t);   
% v6 w+ }6 b6 H. \8 Xif t==1   %prevx = init_x(:,m);   % e/ g- @7 v& [# q/ T' t% r
%prevV = init_V(:,:,m);   
+ d* }, Z, t5 Aprevx = init_x;   
7 h3 b: T0 s# O# {prevV = init_V;   
! k; ~4 K. m- `: _' Ainitial = 1;   8 M- B6 C$ t3 N1 H
else   prevx = x(:,t-1);   1 U6 ~3 Q* J5 {7 j" a
prevV = V(:,:,t-1);   ' t3 v  y* k  }% O" r
initial = 0;   8 U# ]+ @( _) j* i* e! H! a
end     `, c# U$ D3 V/ u2 F  o
if isempty(u)   
1 n* B9 _, G6 ~" r4 a[x(:,t), V(:,:,t), LL, VV(:,:,t)] = ...   1 C+ h* _$ J9 h8 R3 r& _
kalman_update(A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m), y(:,t), prevx, prevV, 'initial', initial);   else   2 T( X" h: ^! k3 F* X
  if isempty(ndx)   [x(:,t), V(:,:,t), LL, VV(:,:,t)] = ...   
; |9 A0 T8 X% j$ e" k     kalman_update(A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m), y(:,t), prevx, prevV, ...   'initial', initial,     'u', u(:,t), 'B', B(:,:,m));   1 m/ Z+ h  X8 B$ D! t2 ]4 N, w7 ?
else   
0 g+ m, L2 E0 \# y0 q& Q# Ni = ndx;   
3 n0 K4 u5 j% e/ Q# m7 _. ^" k% copy over all elements; only some will get updated   x(:,t) = prevx;   
# f& y& z5 e( t9 IprevP = inv(prevV);   " x8 l) l8 H  q
prevPsmall = prevP(i,i);   9 `4 e1 {9 G( r: `- a6 c8 c% P
prevVsmall = inv(prevPsmall);   2 q3 J" E9 t$ m6 C* }* x& G
[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));   
, T8 P5 t# E' e5 t+ EsmallP = inv(smallV);   
  u) y* X3 _% H& [prevP(i,i) = smallP;   8 [' `% x6 f$ s% w1 ]
V(:,:,t) = inv(prevP);   
, N# ^8 C2 Q3 \end   
/ w; v( a7 F' D% u- bend   ( M& t- f3 ^) ]2 o7 l
loglik = loglik + LL;   7 b( |# b# B+ H9 @5 {, P6 r: S
end
作者: 剑游九天    时间: 2011-12-19 23:38
厚积薄发 发表于 2011-11-28 10:48
0 a9 i) U% K. V% [2 a4 q: amatlab下面的kalman滤波程序
6 \1 {, K& S5 L. h& M5 w  F- Lclear  N=200; w(1)=0;   ( }% x6 c3 B7 J, D2 E" I! K; F& ~
w=randn(1,N)

4 X7 u# u; f/ O) J  O2 z强大呀,下了
作者: 工科男    时间: 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