Lekshmi B S
Member level 1
- Joined
- Apr 22, 2013
- Messages
- 34
- Helped
- 2
- Reputation
- 4
- Reaction score
- 2
- Trophy points
- 1,288
- Location
- India
- Activity points
- 1,514
what happens while running the following code?
the statement in 'for' loop after the 'if' loop shouldnot be calculated when error1=-0.8735,, right??
this is the effect of break,,i think..but for the full code given after this one, the same concept is not working..can anyone explain why it is so..?
Thank you..
----------------------------------------------------------------------------------
Full code:
the statement in 'for' loop after the 'if' loop shouldnot be calculated when error1=-0.8735,, right??
this is the effect of break,,i think..but for the full code given after this one, the same concept is not working..can anyone explain why it is so..?
Thank you..
Code:
for i=2:N
x(i)=F*x(i-1)+w(i);
z(i)=H*x(i)+v(i);
apriori_state(i)=F*aposteriori_state(i-1);
apriori_error(i)=F*F'*aposteriori_error(i-1)+Q;
residual(i)=z(i)-H*apriori_state(i);
K(i)=H*apriori_error(i)/(H*H'*apriori_error(i)+R);
aposteriori_state(i)=apriori_state(i)+K(i)*residual(i);
aposteriori_error(i)=apriori_error(i)*(1-(H*K(i)));
error1(i)=apriori_state(i)-x(i)
if (error1(i)==-0.8735)
break
end
error2(i)=aposteriori_state(i)-x(i)
end
----------------------------------------------------------------------------------
Full code:
Code:
clc;
clear all;
close all;
N=20;
F=2.8;
H=0.07;
Q=7;
R=4;
x=zeros(1,N);
z=zeros(1,N);
residual=zeros(1,N);
K=zeros(1,N);
apriori_state=zeros(1,N);
apriori_error=ones(1,N);
aposteriori_state=zeros(1,N);
aposteriori_error=ones(1,N);
x_0=1;
aposteriori_state_0=1.5;
aposteriori_error_0=1;
randn('state',0);
w = wgn(1,N,5)*sqrt(Q);
v = wgn(1,N,5)*sqrt(R);
x(1)=F*x_0+w(1);
z(1)=H*x(1)+v(1);
apriori_state(1)=F*aposteriori_state_0;
apriori_error(1)=F*F'*aposteriori_error_0+Q;
residual(1)=z(1)-H*apriori_state(1);
K(1)=H*apriori_error(1)/(H*H'*apriori_error(1)+R);
aposteriori_state(1)=apriori_state(1)+K(1)*residual(1);
aposteriori_error(1)=apriori_error(1)*(1-(H*K(1)));
error(1)=aposteriori_state_0-x_0
error2(1)=aposteriori_state_0-x_0
for i=2:N
x(i)=F*x(i-1)+w(i);
z(i)=H*x(i)+v(i);
apriori_state(i)=F*aposteriori_state(i-1);
apriori_error(i)=F*F'*aposteriori_error(i-1)+Q;
residual(i)=z(i)-H*apriori_state(i);
K(i)=H*apriori_error(i)/(H*H'*apriori_error(i)+R);
aposteriori_state(i)=apriori_state(i)+K(i)*residual(i);
aposteriori_error(i)=apriori_error(i)*(1-(H*K(i)));
error1(i)=apriori_state(i)-x(i)
if (error1(i)==-0.8735)
break
end
error2(i)=aposteriori_state(i)-x(i)
end
figure(1);
t=1:N;
plot(t,K);
xlabel('Observation number');
ylabel('Kalman gain');
title('Kalman gain');
figure(2);
t=1:N;
subplot(2,1,1);
plot(t,w);
xlabel('Observation number');
ylabel('w');
title('Process noise');
subplot(2,1,2);
plot(t,v);
xlabel('Observation number');
ylabel('v');
title('Observation noise');
figure(3);
t=1:N;
subplot(2,1,1);
plot(t,error1);
xlabel('Observation number');
ylabel('Error1');
title('Error between actual value and predicted value');
subplot(2,1,2);
plot(t,error2);
xlabel('Observation number');
ylabel('Error2');
title('Error between actual value and updated value');
figure(4);
subplot(2,1,1);
plot(error1,K);
xlabel('Error1');
ylabel('Kalman gain');
title('Error1 Vs Kalman gain');
subplot(2,1,2);
plot(error2,K);
xlabel('Error2');
ylabel('Kalman gain');
title('Error2 Vs Kalman gain');
Last edited by a moderator: