4-State Kalman Filter: 2D Constant-Velocity Tracking#
This demo runs the canonical 4-state Kalman filter on a 2D constant-velocity scenario, the cleanest non-trivial example of multi-state estimation. A vehicle moves in 2D under random acceleration disturbances; a noisy 2D position sensor delivers fixes every few seconds; an optional velocity sensor can be turned on to compare observability behavior. The filter carries position and velocity together as one state vector, propagates the full \(4 \times 4\) covariance, and shows it two ways: the 95% position ellipse moving with the estimate on the trajectory view, and the position–velocity cross-covariance in an error phase plane beside it.
The state, dynamics, and measurement model#
The state vector and constant-velocity dynamics:
The \(\Delta t\) entries in \(\mathbf{F}\) couple position and velocity errors: every prediction feeds velocity uncertainty into position and builds the off-diagonal \(P_{pv}\) (the \(\Delta t^3/2\) terms in \(\mathbf{Q}\) add only a small share). That cross-covariance is why a position-only measurement still produces a useful velocity estimate: it carries the correction across.
The position sensor measures the first two states directly:
The optional velocity sensor measures the last two:
Interactive demo#
The demo simulates the whole run deterministically (given the parameters and seed) and animates a “now” cursor through it at the speed you select. It opens paused at \(t = 0\) and starts playing when you scroll it into view (it stays paused if your system asks for reduced motion). Any parameter change re-runs the simulation instantly and keeps the cursor where it is.
The live trajectory panel is a top-down view, auto-scrolled to keep the estimate centered in a ±200 m window. Cyan path and triangle = truth; blue path and diamond = KF estimate; red dashed circle = the current 95% position-covariance ellipse. POS / VEL dots in its corner flash when a fix arrives, and anything that leaves the window gets an edge arrow with its size.
Beside it (below it in a narrow window), the error phase plane plots the \(x\)-channel error \((\hat{p}_x - p_x,\ \hat{v}_x - v_x)\) inside its live 95% ellipse, together with the prior (dashed) and posterior (solid) ellipses of the last fix. This is where the position–velocity cross-covariance shows.
The two error panels below plot the \(x\) (blue) and \(y\) (orange) position and velocity errors, estimate − truth, against the shared ±1.96σ bound, with a synchronized cursor.
The cards report the live estimate, \(\sigma_p \cdot \sigma_v \cdot \rho_{pv}\), and the bound coverage so far; the line under the play bar states what to notice at the current moment.
The two \(\sigma_a\) sliders are deliberately separate: \(\sigma_a\) (process) controls the truth-side random acceleration, while \(\sigma_a\) (model Q) is the filter’s tuning of \(\mathbf{Q}\). With them equal, the filter is matched to the world; setting them unequal exposes the classic Q-tuning lesson.
Walkthrough#
The demo opens at the canonical Block 5 scenario: nominal \(v_{x,0} = 5\) m/s, \(v_{y,0} = 3\) m/s, both \(\sigma_a\) sliders at 0.5 m/s² (matched), \(\sigma_\text{pos} = 10\) m every 10 s, velocity sensor off, \(T = 300\) s. The filter starts at \(\hat{\mathbf{x}}_0 = (0, 0, 5, 3)\) with \(\sigma_p = 10\) m and \(\sigma_v = 10\) m/s; the truth is drawn from that same \(\mathcal{N}(\hat{\mathbf{x}}_0, \mathbf{P}_0)\). At the default seed it starts about 11 m/s faster in \(x\) than the filter assumes (16.2 m/s against 5). Try the following:
Watch the first 10 seconds. No fix arrives until \(t = 10\) s, so \(\sigma_p\) grows from 10 m to 99.5 m by \(t = 9.9\) s. The trajectory ellipse leaves the window (its callout reads “95% radius 244 m (off scale)”), and the position error (125 m at the default seed) runs off the ±60 m panel. In the phase plane the live 95% ellipse stretches into a thin diagonal needle (\(\rho_{pv} = 0.99\)), and the error dot runs out along it: almost all of the position error is velocity error × time. The trajectory ellipse itself is always a circle: \(x\) and \(y\) have identical models and sensors, so \(P_{xx} = P_{yy}\) and \(P_{xy} = 0\). After the first few fixes it simply breathes, from a 95% radius of 19.5 m just after a fix to 32.1 m just before the next.
Watch the first fix (\(t = 10\) s) in the phase plane. The fix collapses the tilted prior (dashed, \(\rho_{pv}^- = 0.99\)) diagonally to the posterior (solid, \(\rho_{pv}^+ = 0.69\)), and \(\sigma_v\) drops from 10 to 1.43 m/s although velocity was never measured. The \(\sigma_p \cdot \sigma_v \cdot \rho_{pv}\) card shows each fix as prior → posterior: 0.92 → 0.70 at 20 s, 0.84 → 0.64 at 30 s, settling at 0.78 → 0.61, while \(\sigma_v\) falls 1.43 → 0.81 → 0.66 → 0.63 m/s. That correlation is the structural reason a position-only sensor teaches velocity: the velocity row of the gain is proportional to \(P^-_{pv}\), so correcting \(\hat{p}_x\) drags \(\hat{v}_x\) with it (at 20 s, default seed: \(\hat{p}_x\) +23.2 m and \(\hat{v}_x\) +1.44 m/s).
Toggle the velocity sensor on (\(\sigma_\text{vel} = 1\) m/s every 5 s). At this setting the gain is modest: at 300 s, \(\sigma_v\) drops from 0.63 to 0.49 m/s, \(\sigma_p\) from 7.95 to 6.65 m, and \(\rho_{pv}\) settles at 0.66 → 0.48. Set \(\sigma_\text{vel}\) to 0.2 m/s and \(\sigma_v\) drops to 0.18 m/s (\(\sigma_p\) 3.80 m).
Disable position, keep velocity. Now nothing measures position. The first velocity fix (\(t = 5\) s) still pulls \(\sigma_p\) from 50.0 m to 11.2 m: before any fix the position error was almost all velocity error × time (\(\rho_{pv}^- = 0.98\)), so learning velocity removes most of it. After that \(\sigma_p\) only grows: 20.2 m at 60 s, 40.2 m at 300 s, with \(\mathrm{tr}\,\mathbf{P}_\text{pos}\) climbing about 10 m²/s. That rate is the velocity-fix noise integrated as a random walk: each fix’s error (\(\sigma_\text{vel} = 1\) m/s), carried for the 5 s interval, adds a position step, so \(\mathrm{tr}\,\mathbf{P}_\text{pos}\) grows at about \(2\,\sigma_\text{vel}^2\,\Delta t_\text{vel} = 2 \times 1 \times 5 = 10\) m²/s. The trajectory circle grows without limit, and from about 150 s the position bound runs off the ±60 m error panel, where an off-scale marker reports its value. The error stays consistent with those growing bounds (100% position coverage at the default seed, typically 83–100% over other seeds): the filter is honest, but it cannot bound position. This is the canonical observability lesson: velocity-only updates cannot pin position down absolutely.
Q-tuning experiment, conservative side. Re-enable position, turn velocity off, and slide \(\sigma_a\) (model Q) up to 2.5 m/s² while \(\sigma_a\) (process) stays at 0.5 m/s². The filter now believes the truth is five times more turbulent than it is. The bounds widen (\(\sigma_v\) 0.63 → 1.92 m/s, \(\sigma_p\) 7.95 → 9.45 m at 300 s) and coverage climbs to 99% (typically 97–100% across seeds). The filter is over-conservative: it trusts each fix more than it should and leaves its own dynamics model on the table.
Q-tuning experiment, dangerous side. Slide \(\sigma_a\) (model Q) down to 0.1 m/s² with \(\sigma_a\) (process) still at 0.5 m/s². The filter now believes the truth is five times smoother than it is. The bounds tighten (\(\sigma_v\) 0.20 m/s, \(\sigma_p\) 6.00 m at 300 s), but the coverage card turns red: 59% at the default seed (position 70%, velocity 49%), and typically 38–71% across seeds. A tight 95% bound that contains the truth far less than 95% of the time is the operationally most dangerous Kalman tuning failure. Confidence interval ≠ accuracy: the filter is overconfident.
Pause and scrub. Drag the time slider back to a moment of interest. The trajectory, the phase plane, both error-panel cursors, the cards and the note line all snap to that time; coverage counts the samples from \(t = 0\) up to the cursor.
Key observations#
A 4×2 Kalman gain maps measurement-space corrections back into the full state space. The position innovation is a 2-vector, the gain matrix is 4×2, and the result is a 4-vector update. The velocity row of the gain is non-zero precisely because \(\mathbf{P}^-\) has a non-zero \(p\)-\(v\) off-diagonal.
Cross-covariance lives in the phase plane, not in the trajectory ellipse. The \(x\) and \(y\) channels have identical models and sensors and never couple, so \(P_{xx} = P_{yy}\) and \(P_{xy} = 0\). The position ellipse is therefore a circle whatever the direction of motion (try a negative or zero \(v_{x,0}\)). What the filter couples is each position with its own velocity. In the \(p_x\)–\(v_x\) plane the steady-state prior is tilted (\(\rho_{pv}^- = 0.78\)), and each fix collapses it diagonally to the posterior (\(\rho_{pv}^+ = 0.61\)), moving \(\hat{p}_x\) and \(\hat{v}_x\) together.
Filter consistency is checked by bound coverage. The Bound coverage so far card counts the share of samples with \(|\text{error}| \le 1.96\sigma\). It is green at 90% or more, amber at 80–90% and red below 80%. Matched (\(\sigma_a\) model = process), it should sit near 95%: 92% at the default seed, and typically 90–99% across seeds. Coverage well below 95% means the filter is overconfident. Coverage pinned near 100% with wide bounds means it is over-conservative. Both are tuning failures, but only the first is dangerous.
The first few fixes do most of the work. \(\sigma_v\) falls 10 → 1.43 → 0.81 → 0.66 m/s over the first three fixes and settles at 0.63 m/s. After that the filter is in steady state, and the circle breathes between the same two sizes: a 95% radius of 19.5 m after each fix and 32.1 m just before the next.
Same recursion as the scalar filter. Compare the predict-update equations here with Block 4. Every quantity has gained dimensions, but the structure is identical: gain is inverse-variance weighting, innovation is the new information, posterior covariance is smaller than prior.
Source#
MATLAB bundle · demos/KalmanFilter4D.m↓
With the 2D covariance ellipse helper in navutils/Draw2DErrorBounds.m. The script uses the demo’s defaults and initial-condition convention: the filter starts at the nominal \(\hat{\mathbf{x}}_0\), and the truth is drawn from \(\mathcal{N}(\hat{\mathbf{x}}_0, \mathbf{P}_0)\). It plots errors as estimate − truth against time in seconds, adds the \(p_x\)–\(v_x\) phase-plane figure, and prints the final \(\sigma_p\), \(\sigma_v\), \(\rho_{pv}\) (prior → posterior) and bound coverage. The covariance numbers (7.95 m, 0.63 m/s, 0.78 → 0.61) match the demo exactly. Its random draws differ from the browser’s, so the error traces and coverage agree only statistically.