Originally Posted by

**atlove** Actually, i am going to use VB.net to build such a model. Before that, i need to understand how to build this mathematics model.

I have tried to use the code you posted before in MatLab, but i was given a different graphic and not similar to yours. Are you plotting the result of function rv=KalmanSim only?

Is Data() a build-in function? I can't excute directly by typing x=data(1) in command window.

What do the S represent for ?

Do you mind to show me the full model you built?

besides, i don't know what really the power consumption is. So i do not sure that it is not dynamic. Should i consider the estimation on state (X(K+1)=FX(K)+u(k)if it is dynamic?

Thank You

A generator of random data vectors:

Code:

function RV=DataGen(aa,nlines)
%
% Function to generate simulated data matrix
%
% aa the row vector of coefficients
ll=length(aa);
RV=[];
for idx=1:nlines
x=rand(1,ll).*20; % generate a random independednt variable vector
% these are uniform of (0,20) in this case
y=x*aa'+randn(1,1); % generate the noisey depended variable
rv=[x,y]; % pack x and y into a data vactor
RV=[RV;rv];
end

The Kalman Inovation process:

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;

The main program which is passed an array containing the data -either real data or simulated:

Code:

function rv=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.
%
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).*100; %initial covariance of state estimate
X=zeros(ll-1,1); %initial state estimate
R=1; %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
plot([0:niter],rv');

If you post some of your data I could run this code over it to see what we get.

CB