数学建模社区-数学中国

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

作者: 工科男    时间: 2011-11-26 16:16
标题: 卡尔曼算法的matlab程序
求卡尔曼算法的matlab预测程序
作者: 厚积薄发    时间: 2011-11-28 10:48
matlab下面的kalman滤波程序/ d/ t7 \6 ]6 y, t" q
clear  N=200; w(1)=0;   * _% D$ [* l8 @, ^
w=randn(1,N)
; y( r) `; I8 X6 ^* p% m5 |x(1)=0;   
; }, w, i1 b% ]' M7 c, oa=1;   8 m$ H% {' T- Q( `: a( R1 {
for k=2:N;   
0 {7 k1 G6 d- k0 W, S  R3 `x(k)=a*x(k-1)+w(k-1);   , ]/ P2 ]9 ]+ s: N
end   
. {7 e9 e- N7 oV=randn(1,N);   
+ H$ _5 T5 G+ [! ]9 |; Gq1=std(V);   3 p8 L0 h% C" y8 P& K
Rvv=q1.^2;   , @& o4 g9 N1 I' x: P+ A
q2=std(x);   # P4 a2 f% N$ Z, s4 N0 H1 ]% F
Rxx=q2.^2;   1 }+ e* J7 N! B2 [. ]" L: r
q3=std(w);   
; l" O# S% Q3 a+ iRww=q3.^2;   
9 ~8 r  i; q/ c# ]/ Bc=0.2;   % k2 @! F: T0 M/ J  B
Y=c*x+V;   5 w/ C. C' S% `( Z, V1 z
p(1)=0;   & ^) v: {, m) E
s(1)=0;" u$ x# u' |9 Q0 j' s7 K, I0 B6 F
for t=2:N;   
% [7 g7 ]# s& b2 S- G8 Hp1(t)=a.^2*p(t-1)+Rww;   1 A- w! K$ t0 ]" K7 Y8 I0 r1 U* g
b(t)=c*p1(t)/(c.^2*p1(t)+Rvv);   
; i. @. P& t( a% w- q8 f/ ms(t)=a*s(t-1)+b(t)*(Y(t)-a*c*s(t-1));     |2 W7 v, D$ H; i  I
p(t)=p1(t)-c*b(t)*p1(t);   
9 S  ]' I) j' G" @0 ?+ Yend   
1 {& g9 ~4 Z  b7 l7 g3 lt=1:N;   
5 `$ }" J7 d; U+ D5 Lplot(t,s,'r',t,Y,'g',t,x,'b');   4 B1 C4 y; A, ^: ~. Q2 I1 z. Q1 ?- f
function [x, V, VV, loglik] = kalman_filter(y, A, C, Q, R, init_x, init_V, varargin)   ; C5 s; X. k" z1 y! E" X
% Kalman filter.   
' }) E; b" }1 @7 o) l) U0 c% [x, V, VV, loglik] = kalman_filter(y, A, C, Q, R, init_x, init_V, ...)   
7 k1 D- I$ n$ v# r%   * U7 A, f' b) q$ b  U* `
% INPUTS:   
2 O0 u" p$ c+ w% y(:,t) - the observation at time t   
( ]: e% K2 _: y+ `5 ?. Y5 C% A - the system matrix   . Q1 t& ^$ q' ?9 Z7 `9 R) n( e
% C - the observation matrix   
: m+ r6 ]1 [- A% Q - the system covariance   7 ]- N3 W4 L; p$ l5 g
% R - the observation covariance   
/ g1 ?* h5 q: N( a6 \9 N% init_x - the initial state (column) vector   
. _, M; g+ o2 T- L7 x. O% init_V - the initial state covariance   
( Y! I% x# i' X/ T%   
7 S0 T! ~7 d# _+ \1 ?9 `% OPTIONAL INPUTS (string/value pairs [default in brackets])   
+ C8 \, B$ N5 q  I& X, O6 ]8 {% \% 'model' - model(t)=m means use params from model m at time t [ones(1,T) ]   
( w$ }# |* y7 m3 O" @; o; N  \; I( o% In this case, all the above matrices take an additional final dimension,   5 R( g! ^" V, V) g% Z# Y0 e
% i.e., A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m).   - M, y8 k& x: R7 ?* N( v
% However, init_x and init_V are independent of model(1).   ) s% M8 w$ Y( N0 }* h( p: A, g
% 'u' - u(:,t) the control signal at time t [ [] ]   
- Q/ n& n2 K6 E# k% 'B' - B(:,:,m) the input regression matrix for model m   
$ o* u0 M2 G1 \%   
$ F3 [1 n  c& y% W5 x6 |* E* w" m8 ^3 q% OUTPUTS (where X is the hidden state being estimated)   
( W7 X: b9 F) e& [7 X% x(:,t) = E[X(:,t) | y(:,1:t)]   
+ e! T6 c4 z5 a1 _, K! W% s% V(:,:,t) = Cov[X(:,t) | y(:,1:t)]   
' n" R+ N9 B& ?4 i8 L9 [$ b4 f- a, C4 I% VV(:,:,t) = Cov[X(:,t), X(:,t-1) | y(:,1:t)] t >= 2   
/ r* x: `/ o# [4 q% loglik = sum{t=1}^T log P(y(:,t))   ; k! ~6 b: D6 w2 |( L. R, q
%   1 ?" L/ r3 V# ^* Q* D3 y! S  x) ?
% If an input signal is specified, we also condition on it:   
# Z# N# W; `) n+ F! P: l; ^% e.g., x(:,t) = E[X(:,t) | y(:,1:t), u(:, 1:t)]   + h# K; V9 u4 U
% If a model sequence is specified, we also condition on it:   
$ G  F& P: X4 J5 B& T" {# F* {1 J% e.g., x(:,t) = E[X(:,t) | y(:,1:t), u(:, 1:t), m(1:t)]   % Z- O! ?! f& @" @0 E8 G
[os T] = size(y);   7 N& |4 |  J: p0 i; q, M
ss = size(A,1); % size of state space   , Q7 X- z) ^9 {, p: {
% set default params   , ?3 Q* Q" z" E$ ~
model = ones(1,T);   
) e0 R- o; J+ S0 n% Au = [];   
/ y1 @6 [! P& D" u3 H. r# e/ m5 CB = [];   0 S3 v7 s9 p; r: H8 h7 y
ndx = [];   * P1 }7 x, p9 E, S9 q  D
args = varargin;   / |" U' s" R% |' V$ s4 i5 V
nargs = length(args);   # }9 u/ [7 Q* Q% h4 n
for i=1:2:nargs   
; j7 u9 y7 g; G/ A' [# V3 nswitch args   1 X3 i: ~' g3 h# t9 ~/ b
case 'model', model = args{i+1};   
) G9 n0 J: i. J5 @: |0 `  icase 'u', u = args{i+1};   ( Y9 U# \+ ?5 q0 q/ Q7 X/ [
case 'B', B = args{i+1};   5 G+ C# U9 `4 {
case 'ndx', ndx = args{i+1};   
: A9 r/ N& T3 K8 U! P2 wotherwise, error(['unrecognized argument ' args])   2 P" T. v8 m1 Q9 k8 D. N6 A4 F
end   / j( j8 `1 \; _! n- l
end   
+ e  j3 C1 v! u' r+ N8 dx = zeros(ss, T);   
  U3 P4 T$ z0 lV = zeros(ss, ss, T);   
6 k( ]& z4 N7 Z4 m8 ]VV = zeros(ss, ss, T);   
! I+ Z; _! d4 m% |loglik = 0;   9 r; n$ G0 C* B& `% H2 l
for t=1:T   m = model(t);   
: b% u3 r/ u* w8 {) Rif t==1   %prevx = init_x(:,m);   
6 p* f5 h$ ]6 T3 K: r%prevV = init_V(:,:,m);   
# ~6 d% i9 b8 S) J( |# xprevx = init_x;   ! O5 K3 c/ T9 p9 Y$ E2 {' m. l
prevV = init_V;   " d2 P* n5 W4 f) p
initial = 1;   
/ u& Q7 c* Z5 L3 w- Telse   prevx = x(:,t-1);   
) j  b( A0 K8 v! G' J$ yprevV = V(:,:,t-1);   
: L0 C) X* Q0 D5 N2 \' s4 Jinitial = 0;   2 ~  V$ g& l& o8 w
end   
+ B; h, j4 ?' W. Q/ y9 \% ~) dif isempty(u)   2 V- _0 |& E" o/ u
[x(:,t), V(:,:,t), LL, VV(:,:,t)] = ...   9 g/ `& R8 i  Z0 T; A6 f
kalman_update(A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m), y(:,t), prevx, prevV, 'initial', initial);   else   
& [) l/ b* D# x( l5 Q  if isempty(ndx)   [x(:,t), V(:,:,t), LL, VV(:,:,t)] = ...   
% B6 Y' f9 H) O) B% }     kalman_update(A(:,:,m), C(:,:,m), Q(:,:,m), R(:,:,m), y(:,t), prevx, prevV, ...   'initial', initial,     'u', u(:,t), 'B', B(:,:,m));   : L& t' C- _% [/ j
else   
: X5 _6 q- g$ u- Z- ?i = ndx;   1 p; v' U) k1 T2 o  W* P* Z$ b
% copy over all elements; only some will get updated   x(:,t) = prevx;   ' ]- m; M/ b8 M/ ~
prevP = inv(prevV);   
9 _" P0 g- v3 N2 q2 q3 }prevPsmall = prevP(i,i);   4 L$ Y* x6 N  S" d; T
prevVsmall = inv(prevPsmall);     M/ X& y7 g$ m7 K
[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));   * s/ @% H! M* b" ^
smallP = inv(smallV);   7 W$ B, H$ N) T& i" H
prevP(i,i) = smallP;   ! ^# n  N1 [: a: B! ^$ Y) U
V(:,:,t) = inv(prevP);   7 ]" F! T" p7 X: L
end   $ o- U2 b, Z& }" I4 A7 e0 |. m
end   0 @3 ?9 z! ^; I, f0 h$ o
loglik = loglik + LL;   3 g7 B5 I" C* D
end
作者: 剑游九天    时间: 2011-12-19 23:38
厚积薄发 发表于 2011-11-28 10:48 $ J8 b# d1 J" T0 q9 ?
matlab下面的kalman滤波程序
3 u; _$ G/ E& N* uclear  N=200; w(1)=0;   % q0 t- b" H" Y& T) c2 f, |
w=randn(1,N)
6 K! \& x! s, l1 A4 g! k$ x8 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