As shown in following code. inp contains 10*4 matrix. I am processing each row but i am getting processed output for the first row and for remaining row I am getting NaN value. why I am getting like that? where is the error?
Show older comments
% I am using this code in simulink model, where Input matrix(10*4) will change for every 1/60 seconds.
%code
function op = fnc(inp)
% define the filter
op = zeros(10,4);
F = [ 1 0 1 0 ; % A
0 1 0 1 ;
0 0 1 0 ;
0 0 0 1 ];
H = [ 1 0 0 0;0 1 0 0;0 0 0 0;0 0 0 0 ] ;
R = [1 0 0 0 ; 0 1 0 0; 0 0 1 0 ; 0 0 1 0];
X = zeros(1,4);
Q = eye(1)*1e-5;
w = 0.2;
P = eye(4)*10;
for i = 1 : 10
x = inp(i,:);
for m = 1
for n = 2
if (x(m,n)==0)
op(i,:) = x;
elseif (0<x(m,n))&&(x(m,n)<70)
X = X*F ; %predicted state (1*4)
S = H'*H*P' + R; %measurement error covariance (4*4)
K = (H*P')/S; %optimal Kalman gain (4*4)
P = P - K*H*P; %(4*4)
P = F*F'*P + Q; % estimate error covariance (4*4)
y = x - X*H; %measurement error/residual (1*4)
X = X + y*K; %updated state estimate (1*4)
X_1 = X*H;
op(i,:) = X_1;
else
%Predictor equations
X = X*F + w; %predicted state (4*1)
P = F*P*F' + Q; % estimate error covariance (4*4)
op(i,:) = X;
end
end
end
end
3 Comments
Sachin Patil
on 7 Oct 2016
dbmn
on 7 Oct 2016
Are you sure that the two following lines are correct? (they don't Loop into anything)
for m = 1
for n = 2
Image Analyst
on 7 Oct 2016
No need for loops then. Simply get rid of the two for loops and use the 1,2 index
if (x(1,2)==0)
Accepted Answer
More Answers (1)
Massimo Zanetti
on 7 Oct 2016
In these two lines anything is looping,
x = inp(i,:);
for m = 1
for n = 2
put something like this:
x = inp(i,:);
for m = 1:SOMETHING
for n = 2:SOMETHING ELSE
1 Comment
Sachin Patil
on 7 Oct 2016
Categories
Find more on State Estimation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!