function [x,P] = kalmanFunc(x,A,B,u,P,Q,R,C,z)
% This is the code which implements the discrete Kalman filter:

% Prediction for state vector and covariance:
x = A*x + B*u;
P = A * P * A' + Q;

% Compute Kalman gain factor:
K = P*C'*inv(C*P*C'+R);

% Correction based on observation:
x = x + K*(z-C*x);
P = P - K*C*P;

return