数学建模社区-数学中国

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

作者: 工科男    时间: 2011-11-26 16:16
标题: 卡尔曼算法的matlab程序
求卡尔曼算法的matlab预测程序
作者: 厚积薄发    时间: 2011-11-28 10:48
matlab下面的kalman滤波程序
" f9 e; X6 {8 t0 K* pclear  N=200; w(1)=0;   
1 b/ o( C' t3 F! M4 kw=randn(1,N) ) d8 |7 D9 w/ F& `
x(1)=0;   " V1 E- n% d7 G* L/ v
a=1;   7 ]5 Y4 Z" N, s" Z& f  t% `" S3 P
for k=2:N;   
/ g* x! Q+ R' g* tx(k)=a*x(k-1)+w(k-1);   # ~- D0 r. x! I
end   
7 X" V4 T7 p2 g. q1 g/ [V=randn(1,N);   ! i' p* _' q- l' [- z# m2 c
q1=std(V);   . c# ~" A% {$ y! X
Rvv=q1.^2;   ' Z- O% p& w: e7 x  a
q2=std(x);   
" u  S) P% t9 L. b8 I& ~Rxx=q2.^2;   " C7 h2 [2 r* ]4 _3 r0 g2 D
q3=std(w);   
, T  {0 J- E! W4 v3 MRww=q3.^2;   5 |* {  o; O9 I% k2 [* _6 n: H
c=0.2;   ! U# o5 _& M# O6 @; e9 I
Y=c*x+V;   
* k; ^/ s- L0 W/ z9 mp(1)=0;   9 F, k+ s( p( W/ \4 u4 Q6 R0 l
s(1)=0;+ `8 k; q6 J0 o& r* P$ r. g1 g2 y
for t=2:N;   ! T8 {0 Z# G/ |  w4 R
p1(t)=a.^2*p(t-1)+Rww;   0 W: h3 c* \  [- z& J0 R
b(t)=c*p1(t)/(c.^2*p1(t)+Rvv);   9 e, t/ p; a, f! j5 k. B$ R' z8 r
s(t)=a*s(t-1)+b(t)*(Y(t)-a*c*s(t-1));   ! z. q0 B4 v7 r
p(t)=p1(t)-c*b(t)*p1(t);   
' y! ]/ Y7 f& @& ^" R( Cend   
( ?! a7 \; [+ V( Y1 f, F9 B% A* ?  v( p3 Wt=1:N;   0 i) i* D6 F2 l- Y5 u2 }' p& v
plot(t,s,'r',t,Y,'g',t,x,'b');   
* j0 y9 i5 [  Xfunction [x, V, VV, loglik] = kalman_filter(y, A, C, Q, R, init_x, init_V, varargin)   . y; n( T1 {0 W2 M! ?. n: |. t
% Kalman filter.   
0 E6 r2 c/ _, i3 H* j, n, U% [x, V, VV, loglik] = kalman_filter(y, A, C, Q, R, init_x, init_V, ...)   
5 Q+ G9 o, F' c0 A9 t( o$ P; n%   
2 h$ P9 v8 r3 c% INPUTS:   
1 j; l/ }4 m* F1 H% F& h; h6 d% y(:,t) - the observation at time t   0 @* m4 J1 p' R, c
% A - the system matrix   
( T$ U. t# k! g+ J5 A6 r% C - the observation matrix   
7 v, Y$ H% B' M( |% Q - the system covariance   3 R1 g" z/ _" w0 D; W9 D( u
% R - the observation covariance   : S/ x7 }0 N: g1 b: E+ c
% init_x - the initial state (column) vector   ( b) A2 l( M9 B2 a
% init_V - the initial state covariance   
6 i1 g. f5 s( V0 C7 R3 J%   ) F% x4 x# y" w" E) t3 Z
% OPTIONAL INPUTS (string/value pairs [default in brackets])   6 }7 Q. l$ c, n8 H/ K+ x/ `
% 'model' - model(t)=m means use params from model m at time t [ones(1,T) ]   
6 Z( U1 M0 f; I1 J( S% In this case, all the above matrices take an additional final dimension,   . ^/ U% N6 @; d- T& G% o5 R
% i.e., A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m).   
+ u6 ?  X3 f( p) U% However, init_x and init_V are independent of model(1).   
9 j& S& n/ s1 ~1 B( `  i# d& ~# a% 'u' - u(:,t) the control signal at time t [ [] ]   
6 G! m  W7 I  ?% 'B' - B(:,:,m) the input regression matrix for model m   % q9 W4 ^) {: w4 q8 T
%   : C  I" I) S- p( x1 V
% OUTPUTS (where X is the hidden state being estimated)   
, P! i) @& C/ w3 S$ u9 b# s% x(:,t) = E[X(:,t) | y(:,1:t)]   
* c2 f3 \0 X9 Q3 ~) D; E. U% V(:,:,t) = Cov[X(:,t) | y(:,1:t)]   ( W* g7 J; H* m
% VV(:,:,t) = Cov[X(:,t), X(:,t-1) | y(:,1:t)] t >= 2   
  d9 J6 u1 T4 @$ G( ?, ]4 Z9 g% loglik = sum{t=1}^T log P(y(:,t))   
. _" a0 O- U5 ^8 @, m. ~1 m%   7 i# _5 E' k$ Y  B0 j/ T7 Z, a
% If an input signal is specified, we also condition on it:   
: ~$ M- N- E1 X+ t3 ]4 W; }% e.g., x(:,t) = E[X(:,t) | y(:,1:t), u(:, 1:t)]   4 R' n5 L0 G2 [
% If a model sequence is specified, we also condition on it:   
2 N6 ^; j+ z  {) b$ O% _. Q7 D3 K% e.g., x(:,t) = E[X(:,t) | y(:,1:t), u(:, 1:t), m(1:t)]   
) v5 o3 I& H8 i0 y; O. q) X, Y[os T] = size(y);   . _# R7 S0 s/ m
ss = size(A,1); % size of state space   7 {- I7 [) _3 g% t
% set default params   
/ O+ f! S* j) h: c: v6 dmodel = ones(1,T);   
; C8 V9 Z, l/ C7 Z$ Ku = [];   
  G% J; @& y- B% AB = [];   
2 ^4 _! H9 s  p0 b, Sndx = [];   
" ~0 y1 Y6 a* c9 c0 Jargs = varargin;   
2 n( m0 l0 q8 b( Inargs = length(args);   
6 G) i1 x6 L/ m0 W& F: Tfor i=1:2:nargs   
, F: D" D+ R) pswitch args   / D3 e, K# J% ]9 o8 S( g3 ^
case 'model', model = args{i+1};   3 n- M( F6 s  M
case 'u', u = args{i+1};   $ g1 N4 t+ M8 E/ O6 j! [; p1 u1 H
case 'B', B = args{i+1};   ! w( k+ e2 v1 E. U& k6 y
case 'ndx', ndx = args{i+1};   ' {: [; l/ p* \4 F! L7 q$ X
otherwise, error(['unrecognized argument ' args])   7 j8 _' F9 l- Z) z7 P( g, O
end   
4 t. t' h5 ^2 U& c$ h* Qend   " L! B2 ~7 _: I$ Z  E2 Z" A* M
x = zeros(ss, T);   
; M, q) h# n' G% G4 p0 rV = zeros(ss, ss, T);   ' w: L( \; _4 i: z  E: z
VV = zeros(ss, ss, T);   
0 i7 X  S8 ~3 D& P* H' }6 \loglik = 0;   ' X; ~5 p+ r9 l  }
for t=1:T   m = model(t);   
5 @* Q; Y' Y5 w* Tif t==1   %prevx = init_x(:,m);   / u- V8 a: x1 ^( Q8 Z
%prevV = init_V(:,:,m);   
, _% i8 @9 g% j( {' S# H: Oprevx = init_x;   
6 O2 l; i$ S/ x6 A7 S! SprevV = init_V;   * _9 ~3 g- E+ i. H" f8 g
initial = 1;   % E0 C% u% m& Z0 r: b7 D2 C
else   prevx = x(:,t-1);   8 M; a6 b* E6 f4 `/ t
prevV = V(:,:,t-1);   ! C. h/ Y0 J; X5 o5 D+ d4 y- ]( s* ~; a
initial = 0;   
& {* ^  ]6 S' P% K- w( Zend   
7 v* k/ s; c6 x1 E/ M/ I' iif isempty(u)   
% W# }1 C* z$ a3 P6 C. R2 B+ X! [[x(:,t), V(:,:,t), LL, VV(:,:,t)] = ...   : R: h" l! }/ R/ S0 z& |7 Q
kalman_update(A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m), y(:,t), prevx, prevV, 'initial', initial);   else   , K( Y' g4 I% \! Y9 C* b8 }  H7 f! P* Y
  if isempty(ndx)   [x(:,t), V(:,:,t), LL, VV(:,:,t)] = ...   ' c: k* U7 n0 L3 p
     kalman_update(A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m), y(:,t), prevx, prevV, ...   'initial', initial,     'u', u(:,t), 'B', B(:,:,m));   
& u, v! G4 x5 f$ E' @/ G3 D3 Zelse   
5 v+ ^$ Q; t2 ~" Q. fi = ndx;   
: u; r" g' b" p9 {7 m3 w% copy over all elements; only some will get updated   x(:,t) = prevx;   # x) z* X7 X" I  D/ K0 N. h/ S
prevP = inv(prevV);   
+ d) O( U/ ~7 M8 |prevPsmall = prevP(i,i);   
3 O' K8 X* D! _$ C6 [. k9 i; E7 |4 RprevVsmall = inv(prevPsmall);   
, v! U! k1 C! ~2 R[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));   
! `1 X3 n: f  L. A3 YsmallP = inv(smallV);   4 Y0 R0 \  T9 j3 Y8 ]( e
prevP(i,i) = smallP;   ' l' u, e* j7 Z
V(:,:,t) = inv(prevP);   
4 d9 _0 u  I: k2 V- k" _6 C2 Zend   & _) E+ u: @) j* ?
end   1 Q; V& s9 C5 C9 S
loglik = loglik + LL;   
5 I6 Q* B( G. e3 f0 I, P# Z: oend
作者: 剑游九天    时间: 2011-12-19 23:38
厚积薄发 发表于 2011-11-28 10:48 - Y: Z1 }0 ^* b+ z6 P8 v7 r/ H
matlab下面的kalman滤波程序
6 C! a  I* L2 Y' ], W4 C9 j9 zclear  N=200; w(1)=0;   
: ~7 R6 y3 G4 _3 P8 E3 y/ ww=randn(1,N)

9 Y+ s+ x0 [6 Q+ r- u强大呀,下了
作者: 工科男    时间: 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