数学建模社区-数学中国

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

作者: 工科男    时间: 2011-11-26 16:16
标题: 卡尔曼算法的matlab程序
求卡尔曼算法的matlab预测程序
作者: 厚积薄发    时间: 2011-11-28 10:48
matlab下面的kalman滤波程序
6 L2 H4 ^4 ?2 p7 X$ mclear  N=200; w(1)=0;   
! ^% _+ q3 R: c3 ?w=randn(1,N) : W* C6 C1 ?4 O' ?- j* t
x(1)=0;   3 c, K/ K+ ^, I
a=1;   - K: `) _% g8 @( {* _
for k=2:N;   & i8 t5 w8 R1 X" K% f# t
x(k)=a*x(k-1)+w(k-1);   $ ~* K/ R" L* n& _3 z& Z. z+ }
end   
( J  w9 r6 o. s, P/ E- ZV=randn(1,N);   
; B( B4 a, W' `% F8 @8 Hq1=std(V);   
- F6 V. S! |) m! m+ f* |Rvv=q1.^2;   4 E: ?6 R: V+ B3 D) x+ m8 a, Q5 }( B
q2=std(x);   2 b. N( i4 m, f2 \/ S
Rxx=q2.^2;   ! A3 N) N* w* d4 g
q3=std(w);   
6 y# }" ^- y8 u, G3 @Rww=q3.^2;   
. n9 ]" {  E+ xc=0.2;   
5 t" f& T5 L2 FY=c*x+V;   
: M, y* j4 q" M! [' D8 zp(1)=0;   2 A+ ~. z5 Q* c% J
s(1)=0;
7 p8 @4 {7 z$ b  e' d1 Wfor t=2:N;   1 ^4 P$ X$ ^8 O9 C
p1(t)=a.^2*p(t-1)+Rww;   
* F* W$ H9 |3 u+ O) o9 _b(t)=c*p1(t)/(c.^2*p1(t)+Rvv);   
, Q8 _6 K" C, w" k5 w. @s(t)=a*s(t-1)+b(t)*(Y(t)-a*c*s(t-1));   ; {1 T/ K- _$ q3 I
p(t)=p1(t)-c*b(t)*p1(t);   , }% K7 {6 H8 o4 b
end   
3 x9 N0 @; |* z/ }t=1:N;   - u' G1 l7 f5 |. t
plot(t,s,'r',t,Y,'g',t,x,'b');   
1 U! K; h; L/ h) u1 o. b( Ffunction [x, V, VV, loglik] = kalman_filter(y, A, C, Q, R, init_x, init_V, varargin)   
& X( B/ F6 e8 i) e9 J+ A% Kalman filter.   8 P" G3 q) W1 r, F$ L
% [x, V, VV, loglik] = kalman_filter(y, A, C, Q, R, init_x, init_V, ...)   8 M' F. V9 U( j% R( G
%   
6 W' K1 ^3 A1 {: H+ Q/ |% INPUTS:   
7 o( v: l, X4 I, O; d3 E+ R% y(:,t) - the observation at time t   
7 P1 o* S; S4 S  C% A - the system matrix   # v# a0 M8 }7 H" ]. ?7 r
% C - the observation matrix   6 j) v* @8 k/ d6 i$ Y1 V+ \
% Q - the system covariance   5 O. C5 P3 W  I+ i  l
% R - the observation covariance   
$ G2 U$ H* T  B; ~2 S% init_x - the initial state (column) vector   . P) C3 B; w% S* E
% init_V - the initial state covariance   
, Z5 E1 k( D7 s) l1 K%   : |$ F! J# G' ], A& @& i$ k7 l
% OPTIONAL INPUTS (string/value pairs [default in brackets])   
% G* C/ T' C3 P1 K! k( L% 'model' - model(t)=m means use params from model m at time t [ones(1,T) ]   
- w. W( O) y( y" B# D9 ?9 w% In this case, all the above matrices take an additional final dimension,   / j/ {: a& D9 Q$ Y
% i.e., A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m).   2 P* _- _8 y% E  W7 E* r6 p
% However, init_x and init_V are independent of model(1).   * g) \9 S8 v! ^
% 'u' - u(:,t) the control signal at time t [ [] ]   
5 i# S# a$ ]6 z* ], \# f6 Q0 ?% 'B' - B(:,:,m) the input regression matrix for model m   
$ m6 C" l* }" l. G) B) p2 ~* A& k# _%   & y- h8 s; s6 J9 P+ }  B
% OUTPUTS (where X is the hidden state being estimated)   . ?1 e4 l1 `! c( S5 ?7 s
% x(:,t) = E[X(:,t) | y(:,1:t)]   
/ k9 ~7 p& J; a% w% V(:,:,t) = Cov[X(:,t) | y(:,1:t)]   
, }% G5 [* c2 ]% VV(:,:,t) = Cov[X(:,t), X(:,t-1) | y(:,1:t)] t >= 2   4 q: P* y3 y1 s. E) x
% loglik = sum{t=1}^T log P(y(:,t))   + v# C3 L1 y, p& |) P' Y$ p* H
%   / s) q  h8 }" ?1 x- ?4 E
% If an input signal is specified, we also condition on it:   5 i9 P5 A$ L( ?2 R7 H& ?. c
% e.g., x(:,t) = E[X(:,t) | y(:,1:t), u(:, 1:t)]     ~% c/ D* [# D1 a
% If a model sequence is specified, we also condition on it:   
* H6 N- j: f5 @' T2 R% e.g., x(:,t) = E[X(:,t) | y(:,1:t), u(:, 1:t), m(1:t)]   
* C7 H1 Z  a/ H' e  T5 H[os T] = size(y);   * l! G8 w/ Z8 R# i4 ^$ u
ss = size(A,1); % size of state space   
5 p! P4 k& R( a: J  `: k% R% set default params   4 ]  o$ [5 X# e: ]5 V0 j
model = ones(1,T);   1 c3 R3 D6 B/ [0 z0 u% V
u = [];   
6 q, d5 F, J( W) c/ HB = [];   
) M8 A( K2 a7 R5 ^2 T; m/ sndx = [];   5 G  g4 `& U& F+ w' n' }0 k$ c! ^
args = varargin;   5 p+ Y3 c0 d& `0 Y& Q- _
nargs = length(args);   
" E* ^; v" K/ kfor i=1:2:nargs   $ x5 w# d+ n# t! I) e
switch args   & o5 R8 u1 m) w8 r, M' X
case 'model', model = args{i+1};   8 q0 s" N- y5 q9 r, J
case 'u', u = args{i+1};   ) a! k8 s5 G$ q& u2 S
case 'B', B = args{i+1};   , L: I$ Z- `/ h
case 'ndx', ndx = args{i+1};   
/ h- H4 E1 z( C: r+ xotherwise, error(['unrecognized argument ' args])   
( Q8 d" H) j5 F7 M& G* A& Dend   7 A: {: @3 v, X
end   / j8 f& {  n; }/ Y1 u* ?
x = zeros(ss, T);   5 R+ F/ z3 \; c6 M0 L
V = zeros(ss, ss, T);   
5 D7 x6 g% u% E- _7 ?( pVV = zeros(ss, ss, T);   . M0 U, h0 ?4 b3 h! P% }3 j: w; F
loglik = 0;   6 X/ @2 B3 M- U$ U
for t=1:T   m = model(t);   
: q1 l0 n8 K" ]# A8 h& [# Q* Vif t==1   %prevx = init_x(:,m);   
! e( S' e7 A7 F$ h9 ]( c' B%prevV = init_V(:,:,m);   
% Q; U1 F# w$ s1 Z2 U% lprevx = init_x;   
( @7 K- }0 p. G9 u( s7 F- WprevV = init_V;   
* t; M5 }5 b/ p$ [initial = 1;   
, d  S2 s; K: Qelse   prevx = x(:,t-1);   ; ]( W9 i, g# Q2 I$ d4 Q; D  Q+ @
prevV = V(:,:,t-1);   
$ |3 t2 @1 K+ S  K( _: ginitial = 0;   
' n/ x7 S/ y6 @- f2 K& m% kend   
3 ~* {( y0 v% g' gif isempty(u)   6 h, x0 |+ l$ e
[x(:,t), V(:,:,t), LL, VV(:,:,t)] = ...   4 ]% \9 x; @' N
kalman_update(A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m), y(:,t), prevx, prevV, 'initial', initial);   else   6 Z7 A+ \3 u  D; Q
  if isempty(ndx)   [x(:,t), V(:,:,t), LL, VV(:,:,t)] = ...   
( R8 d. m$ P- W) F     kalman_update(A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m), y(:,t), prevx, prevV, ...   'initial', initial,     'u', u(:,t), 'B', B(:,:,m));   ; X( l- M1 p: t1 B" `2 X2 a+ V
else   $ X, V' ~! w! }  E0 S1 i
i = ndx;   
; m# f7 Q: X. B: ~% copy over all elements; only some will get updated   x(:,t) = prevx;   ! m+ P6 k5 e- G+ F6 q. S4 [
prevP = inv(prevV);   / c% N) [' \3 g; l1 A  G) K5 V
prevPsmall = prevP(i,i);   
6 k# x! [, [$ L+ sprevVsmall = inv(prevPsmall);   % D/ a) C8 O4 f6 X
[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));   
+ [( \/ q7 \, |$ |6 @4 usmallP = inv(smallV);   
+ x9 j, O- B, F0 iprevP(i,i) = smallP;   
8 R9 R+ _; x0 L# c1 ^8 OV(:,:,t) = inv(prevP);   - n9 \1 n  A  K1 ^9 |
end   3 q( V  v+ }$ U3 L. `9 o) u
end   4 M9 L) d, s4 Q6 N" f
loglik = loglik + LL;   & G$ v& k: V3 |. x
end
作者: 剑游九天    时间: 2011-12-19 23:38
厚积薄发 发表于 2011-11-28 10:48
: H# W/ J, l' g; z4 z; a$ gmatlab下面的kalman滤波程序" n) @1 X. u, [/ N9 F1 t
clear  N=200; w(1)=0;   
# J1 V$ x1 a# Xw=randn(1,N)
0 h( W" B+ @$ W6 B% t
强大呀,下了
作者: 工科男    时间: 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