# Kalman Filter - Comparing the Static Kalman gain and the Dynamic/Recursively updating Kalman Gain

I studied Kalman filters some time ago, and recently came to realize I do not understand some parts on them. Specifically related to the difference between

I often see the recursively method used in various applications, but to me the static method seems easier to implement. In what situations is the recursive method chosen over the static method?

Convergence

I always thought these would result in the same gain, once the recursive method had converged. However, I wrote a Matlab script to compare the two methods, and to see how the recurrent method converges.

I used the following system:

$$dt = 0.1 \\ x(k+1) = \begin{bmatrix} 1 & dt \\ 0 & 1 \end{bmatrix} x(k) + \begin{bmatrix} 0 \\ dt \end{bmatrix} u(k) \\ y(k) = C\;x(k)$$

I tried this with $$C = \begin{bmatrix} 1 & 0 \end{bmatrix}$$ to see how the kalman gain would converge. The kalman gain is here a $$2\times 1$$ matrix.

Running both idare to obtain a static gain: (It IS correct that the $$A$$ and $$C$$ matrices are transposed here.)

Q = diag([0.01 0.01]);
R = 0.01;
[~,Kstat,~,~] = idare(A',C',Q,R,[],[]);
Kstat = Kstat';


and the recursive method:

P = zeros(2,2);
for i = 2:60
P = A*P*A' + Q;
S = C*P*C' + R;
Kdyn = P*C'*inv(S); %#ok<MINV>
P = (eye(2) - Kdyn*C)*P;
end


Now if I plot the recursive method over each iteration, and the found static kalman gain of idare, I obtain the following plot. What is wrong with my code; why does $$K_1$$ not converge to the solution found by using the Riccati Equations? Running the recursive method for longer does not solve the problem.

Observability

In Observability for Kalman Filtering? it is stated that "A Kalman Filter build around a system with unobservable state will simply not work." This is also what I was taught. The Riccati equations don't have a solution for an unobservable system, so that made sense to me.

I ran two versions of the filter with the system above and some bogus data: One for the observable one ($$C = \begin{bmatrix} 1 & 0 \end{bmatrix}$$) and an observable one ($$C = \begin{bmatrix} 0 & 1 \end{bmatrix}$$). Indeed the idare function does not give a solution for the unobservable case, so I set the static-kalman gain to [0;0] for the unobservable case, so I at least could run something.

However, running the recursive method seemed to work just fine in the unobservable case. Below are two plots, one with the C matrix $$C = \begin{bmatrix} 1 & 0 \end{bmatrix}$$ and the other with $$C = \begin{bmatrix} 0 & 1 \end{bmatrix}$$. In the first, both filters work fine. In the second, only the dynamic one works well. What is going on here, is this correct? Why is the Kalman filter for the unobservable system working seemly very well? Is it NOT needed for a system to be observable in order to use a Kalman Filter?

Matlab Files Here are the Matlab Files, in case these are needed: https://anonymous.4open.science/r/Kalman-Comparison-D657/

In what situations is the recursive method chosen over the static method?

When the accuracy of the estimates on start-up need to be better than what the static method gives you.

Or when the system is still linear but time-varying -- in which case there is no "static" solution, but there's still a dynamic one.

What is wrong with my code;

It almost looks correct to me. In fact, it may still be correct-ish.

However (this is the "almost" part), when you say $$\mathbf P_0 = \mathbf 0$$ you are telling the Kalman filter that you know $$\left . x_n \right |_{n=0}$$ exactly. Remember that the main diagonal of $$\mathbf P$$ is the estimated variances of the elements of $$\mathbf x$$, and a variance of zero indicates perfect knowledge. This may be getting fixed by your choice of a non-zero $$\mathbf Q$$, but I could see it either not being enough at all, or not being enough when you're using finite-precision arithmetic, or taking a very long time to settle.

You should try $$\mathbf P = b \mathbf I$$, where $$b$$ is "unreasonably" big -- i.e., just small enough that it doesn't cause numerical problems in and of itself. $$10^{10}$$ should result in a slight under-estimate of $$\mathbf P_2$$, but that should get swept under the rug in subsequent iterations of the filter.

why does K1 not converge to the solution found by using the Riccati Equations? (Running the recursive method for longer does not solve the problem.)

See my reasoning about your starting value of $$\mathbf P$$, above. If that's not it, then I'm betting on some dynamics in your model that make it take a very long time to settle (which I doubt, unless your $$dt$$ is very small -- have you run the simulation out to $$n > \frac 1 {dt^2}$$ steps?)

Why is the Kalman filter for the unobservable system working seemly very well? Is it NOT needed for a system to be observable in order to use a Kalman Filter?

It looks like you're only comparing the real output with the filter's estimated output - that will, of course, match, because the whole definition of an unobservable state is that it does not affect the output. $$x_1$$ could be doing any wild thing over time; because $$y = x_2$$, you'd never see (i.e., observe) it's wild behavior.