|
|
我又改了下。
这条语句:P_pre(:,:,i+1)=(F-eye(4))*P_pre(:,:,i)*(F-eye(4))'+Q;%这里为啥不用F=A+I 而用A呢。用A就能估计,用F就发散。咋回事。
clear all
kx = 0.1; ky =0.5; % 阻尼系数
g = 9.8; % 重力
t = 5; % 仿真时间
Ts = 0.1; % 采样周期
len = fix(t/Ts); % 仿真步数
dax = 1.5; day = 1.5; % 系统噪声
mrad = 0.001;
dr = 10; dafa = 10*mrad; % 量测噪声
X=[0;50;500;0];%初始位置
%初始化
P_pre=100*eye(4);
P_post=100*eye(4);
L=[0 0 0 0;0 1 0 0;0 0 0 0;0 0 0 1];
Q = L*diag([0; dax; 0; day])^2*L';
R =[1 0;0 1] *diag([dr; dafa])^2*[1 0;0 1]';
Pk = 100*eye(4);
X_pre=[0 ; 50;500;0];
X_post=[0 ; 50;500;0];
X_est=[0 ; 50;500;0];
U=[0 ;0 ;0 ;-g*Ts];
%实际曲线
for i=1:len
X(:,i+1)=[1 Ts 0 0;0 1-kx*X(2,(i))*Ts 0 0; 0 0 1 Ts;0 0 0 1+ky*X(4,(i))*Ts]*X(:,(i))+[0 ;0 ;0; -g*Ts]+[0;dax*randn(1,1)*Ts;0 ;day*randn(1,1)*Ts];
Y(:,i+1)=[sqrt(X(1,(i+1))^2+X(3,(i+1))^2); atan(X(1,(i+1))/X(3,(i+1)))]+[dr*randn(1,1);dafa*randn(1,1)];
end
X(1, ;
X(3, ;
for i=1:len
F=[1 Ts 0 0;0 1-2*kx*X_pre(2,(i))*Ts 0 0; 0 0 1 Ts;0 0 0 1+2*ky*X_pre(4,(i))*Ts];%用上一时刻后验估计
%X_pre(:,i+1)=[1 Ts 0 0;0 1-kx*X_pre(2,(i))*Ts 0 0; 0 0 1 Ts;0 0 0 1+ky*X_pre(4,(i))*Ts]*X_pre(:,(i))+U;
X_pre(:,i+1)=X_pre(:,i)+([X_pre(2,i);-kx*X_pre(2,i)^2;X_pre(4,i);ky*X_pre(4,i)^2])*Ts+U;
%X_pre(:,i+1)=F*X_pre(:,i)+U;
P_pre(:,:,i+1)=(F-eye(4))*P_pre(:,:,i)*(F-eye(4))'+Q;%这里为啥不用F=A+I
%P_pre(:,:,i+1)=F*P_pre(:,:,i)*(F)'+Q;
%H=[1/sqrt(X_pre(1,(i+1))^2+X_pre(3,(i+1))^2) 0 1/sqrt(X_pre(1,(i+1))^2+X_pre(3,(i+1))^2) 0;X_pre(3,(i+1))/(X_pre(1,(i+1))^2+X_pre(3,(i+1))^2) 0 -X_pre(1,(i+1))/(X_pre(1,(i+1))^2+X_pre(3,(i+1))^2) 0];
H=[X_pre(1,(i+1))/sqrt(X_pre(1,(i+1))^2+X_pre(3,(i+1))^2) 0 X_pre(3,(i+1))/sqrt(X_pre(1,(i+1))^2+X_pre(3,(i+1))^2) 0;X_pre(3,(i+1))/(X_pre(1,(i+1))^2+X_pre(3,(i+1))^2) 0 -X_pre(1,(i+1))/(X_pre(1,(i+1))^2+X_pre(3,(i+1))^2) 0];%用当前时刻先验估计
K=P_pre(:,:,i+1)*H'*inv(H*P_pre(:,:,i+1)*H'+R);
X_post(:,i+1)=X_pre(:,i+1)+K*(Y(:,i+1)-[sqrt(X_pre(1,(i+1))^2+X_pre(3,(i+1))^2); atan(X_pre(1,(i+1))/X_pre(3,(i+1))) ]);
P_post(:,:,i+1)=P_pre(:,:,i+1)-K*H*P_pre(:,:,i+1);
%赋值
P=P_post(:,:,i+1);
X_est(:,i+1)=X_post(:,i+1);
P_pre(:,:,i+1)=P_post(:,:,i+1);
X_pre(:,i+1)=X_post(:,i+1);
end
plot(X(1, ,X(3, , 'r',X_est(1, ,X_est(3, ,'+r')%实际曲线 |
|