# 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 have some questions about this, which I put in italic-bold.

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.

Try comparing your "real" state against your filter's state.

• Thanks for the extensive answer. Your explanation of static vs recurrent, and how the initial state and the P-matrix are related make sense and gave me some new insights. Commented Dec 17, 2022 at 18:05
• I tried initializing the P-matrix with a much higher value (up to 10^100), and for many more iterations (up to 10^10), and it keeps converging to the same numbers shown above. It feels like there must be some mistake in my code/implementation, but I cannot find it. (see original question for a github-link btw) Commented Dec 17, 2022 at 18:05
• "It looks like ... Try comparing your "real" state against your filter's state." In the bottom figures from my question, I am already comparing those 'real' states with the filter states. "data" is the state of the system being observed, "static" is the estimated states by the static-gain-filter, and "dynamic" is the estimated states with the dynamic-gain-filter. Both states seem to be estimated quite well, even though only the first one is unobservable. Commented Dec 17, 2022 at 18:05