I 've done some modification base on the latest matlab code which plot the curve of estimation of z against the real measured z

Innovation

Code:

function [X,P]=KalmanInov(Data,R,X,P)
%
% Kalman Filter Inovation Processing
%
% data is a row vector with the independent vars and the depended var
%
ll=length(Data)
H=Data(1:ll-1); %extract the H matrix or vector from the data
z=Data(ll); %extract the measurement from the data
% Below is the standard Kalman inovation equations in matrix form
S=H*P*H'+R; %note this is a scalar
K=P*H'*inv(S);
X=X+K*(z-H*X);
P=(eye(ll-1,ll-1)-K*H)*P;

Main Kalman filtering running

Code:

function P=KalmanRun(data)
%
% function to run simulation of the Kalman filter for
% MHF question
%
% data is a matrix each row of which contains the vector of data
%
% The system model is:
%
% z=sum(x(i)*aa(i)) + w
%
% where z is the measurement to be explained, the x's are the
% independent variables corresponding to z and w is a scalar zero
% mean gaussian noise term.
%
% Initial values, the state estimate is set to zero, and the covariance
% to a diagonal matrix of the right dimension with 10^2 down the diagonal
%
% the measurement noise variance R (of w in the model) is set to 1.
%
a=data;
sz=size(data);
niter=sz(1); %number of rows of data
ll=sz(2); %length of each row of data
P=eye(ll-1,ll-1).*1000000; %initial covariance of state estimate
X=zeros(ll-1,1); %initial state estimate
R=0.2; %measurement variance
rv=X;
%
% loop
%
for idx=1:niter
D=data(idx,:); % load the nest line of data into D consists of [x(1),..,x(n),z]
[X,P]=KalmanInov(D,R,X,P); % inovate the filter with the data vactor D
rv=[rv,X];%accumulate the state estimate
end
a1=a(1:niter,1:ll-1);
b1=rv(1:ll-1,2:niter+1);
d1=[];
for idx=1:niter;
c1=a1(idx:idx,1:ll-1)*b1(1:ll-1,idx:idx);
d1=[d1,c1];
end
e1=a(1:niter,ll:ll)';
d1=[d1;e1];
plot([1:niter],d1);

You may randomly input some data matrix to test.

The picture attached is the result.

Can i do that in this way?