If you use the Base versions of the matrices:
A = [ 1 0 0;
0 1 0;
0 0 1 ];
H = [ 1 0 0 ];
you don't have an observable system because your measurements only capture the first state (position) and there is no coupling, in the A matrix, between position and its derivatives (velocity, acceleration). The observability matrix is as follows:
O = [ H;
H*A;
H*A*A ];
O = [ 1 0 0;
1 0 0;
1 0 0 ];
which is obviously singular, i.e., your system is not observable. And feeding that through a EKF algorithm should produce an error (the situation should be detected by the algorithm), but if it is not detected, it will lead to NaN results in the estimates, exactly as you are experiencing.
Now, the A matrix from the makeA() function is more suitable:
A = [ 1 h h*h/2;
0 1 h;
0 0 1 ];
H = [ 1 0 0 ]; // use this H matrix (not [ 1 h h*h/2 ])
leading to an observability matrix:
O = [ 1 0 0;
1 h h*h/2;
1 2*h 2*h*h ];
which is full-rank (not singular), and thus, you have an observable system.
Kalman filtering algorithm can be quite sensitive to the conditioning of the matrices, meaning that if the time-step is really small (e.g. 1e-6
), you need to use a continuous-time version. Also, the problem of NaN might come from the linear solver (solves a linear system of equation) which is needed in the KF algorithm. If the author of the library used a naive method (e.g., Gaussian elimination, LU-decomposition with or without pivots, Cholesky without pivots, etc.), then that would make this issue of numerical conditioning much worse.
N.B. You should start your KF filtering with a very high P matrix, because the initial P should reflect the uncertainty on your initial state vector, which is usually very high, so P should be around 1000 * identity
.