% Example on calculating the Kalman gain matrix and the solution of the Riccati equation
% Iterate for:
% kalman gain K. 
% Apriori state error covariance matrix Xb. 
% Aposteriori state error covariance matrix Xh.

A=0.9; C=0.6; D=1;
W=1; V=1;

n=1;

Xb=eye(n)*100;

for i=1:10
    K=Xb*D'*inv(D*Xb*D'+W)
    IKD=eye(n)-K*D;
    Xh=IKD*Xb*IKD'+K*W*K';
    Xb=A*Xh*A'+C*V*C';
end