ROB-310 · Lesson 6 of 12 · 29 min

The extended Kalman filter

Robots are not linear. A differential-drive robot moves along arcs governed by sines and cosines of its heading, and a landmark sensor measures ranges with square roots and bearings with arctangents. Pass a Gaussian through such functions and the result is no longer Gaussian, so the Kalman filter's exactness is lost. The extended Kalman filter, EKF, keeps the Kalman structure by linearizing the motion and measurement functions around the current estimate at every step. It is an approximation, and it can fail, but when uncertainty is small relative to the curvature of the functions it works remarkably well. The EKF navigated aircraft and spacecraft for decades and is still the default estimator in drone autopilots and many robot localization stacks. This lesson derives the EKF, applies it to landmark-based localization and examines the conditions under which it breaks down.

Linearization

A nonlinear function g can be approximated near a point by its first-order Taylor expansion, \( g(\mathbf{x}) \approx g(\boldsymbol{\mu}) + \mathbf{G}(\mathbf{x} - \boldsymbol{\mu}) \), where the Jacobian \( \mathbf{G} = \partial g/\partial \mathbf{x} \) is evaluated at μ. A linear function of a Gaussian is Gaussian, so the approximation yields a Gaussian with mean \( g(\boldsymbol{\mu}) \) and covariance \( \mathbf{G}\boldsymbol{\Sigma}\mathbf{G}^{\mathsf{T}} \). The approximation is good when the function is nearly straight over the region where the belief has most of its probability, roughly μ ± 2σ. A robot whose heading is known to within 1° sees almost no curvature in \( \cos\theta \); one whose heading is uncertain by 60° sees a great deal.

A plot with a Gaussian on the horizontal input axis, a curved nonlinear function, and two output densities on the vertical axis. A straight tangent line touches the curve at the input mean. The true output density, obtained by passing the input through the curve, is skewed and its mean is offset. The linearized output Gaussian, obtained through the tangent line, is symmetric and centered at the function value of the mean.
Linearization: the EKF passes the belief through the tangent line at the mean, producing a Gaussian that approximates the true, skewed output distribution

The EKF equations

With motion model \( \mathbf{x}_t = g(\mathbf{x}_{t-1}, \mathbf{u}_t) + \mathbf{w}_t \) and measurement model \( \mathbf{z}_t = h(\mathbf{x}_t) + \mathbf{v}_t \), the EKF replaces A and H in the Kalman filter by Jacobians evaluated at the latest estimate:

\[\bar{\boldsymbol{\mu}}_t = g(\boldsymbol{\mu}_{t-1}, \mathbf{u}_t), \qquad \bar{\boldsymbol{\Sigma}}_t = \mathbf{G}_t\boldsymbol{\Sigma}_{t-1}\mathbf{G}_t^{\mathsf{T}} + \mathbf{Q}_t\]
\[\mathbf{K}_t = \bar{\boldsymbol{\Sigma}}_t\mathbf{H}_t^{\mathsf{T}}(\mathbf{H}_t\bar{\boldsymbol{\Sigma}}_t\mathbf{H}_t^{\mathsf{T}} + \mathbf{R}_t)^{-1}, \quad \boldsymbol{\mu}_t = \bar{\boldsymbol{\mu}}_t + \mathbf{K}_t(\mathbf{z}_t - h(\bar{\boldsymbol{\mu}}_t)), \quad \boldsymbol{\Sigma}_t = (\mathbf{I} - \mathbf{K}_t\mathbf{H}_t)\bar{\boldsymbol{\Sigma}}_t\]

Here \( \mathbf{G}_t = \partial g/\partial\mathbf{x} \) at \( \boldsymbol{\mu}_{t-1} \) and \( \mathbf{H}_t = \partial h/\partial\mathbf{x} \) at \( \bar{\boldsymbol{\mu}}_t \). Note what is and is not linearized: the means are propagated through the true nonlinear functions g and h, while only the covariances use the Jacobians. When the noise enters through the controls, as in the motion models of lesson 3, the process noise becomes \( \mathbf{Q}_t = \mathbf{V}_t\mathbf{M}_t\mathbf{V}_t^{\mathsf{T}} \), mapping the control-noise covariance M into state space with the control Jacobian V. Computationally the EKF costs about the same as a Kalman filter, roughly cubic in the state dimension, which for a three-dimensional pose is trivial.

EKF localization with known landmarks

Consider a planar robot with state \( [x, y, \theta]^{\mathsf{T}} \), odometry inputs of a forward distance d and a turn δ per step, and a sensor that measures range and bearing to landmarks with known positions and identities. The motion model and its Jacobians come straight from lesson 3, the measurement model and its Jacobian from lesson 4. The EKF loop is: predict with each odometry step; for each landmark observed, compute the expected range and bearing from the predicted pose, form the innovation (with the bearing wrapped), compute H for that landmark and apply the update. Multiple landmarks seen at the same time can be processed one after another, or stacked into one larger measurement vector; with independent noise the result is the same.

Worked problem

Jacobians at a given pose

The predicted pose is \( (1, 1, 0) \) and a landmark is at \( (4, 5) \). Compute the expected measurement and the measurement Jacobian H.

Show solution
  1. \( \Delta x = 3 \), \( \Delta y = 4 \), \( q = 25 \), \( \sqrt{q} = 5 \).
  2. Expected range 5 m; expected bearing \( \operatorname{atan2}(4, 3) - 0 = 0.927 \) rad (53.1°).
  3. Range row: \( [-3/5,\ -4/5,\ 0] = [-0.6,\ -0.8,\ 0] \).
  4. Bearing row: \( [4/25,\ -3/25,\ -1] = [0.16,\ -0.12,\ -1] \).

Answer: The expected measurement is (5 m, 0.927 rad), with H = [[−0.6, −0.8, 0], [0.16, −0.12, −1]]. The range row says moving toward the landmark (along its direction) shortens the range; the bearing row says bearing depends strongly on heading (−1) and weakly on position, more weakly the farther the landmark.

Worked problem

A single EKF update

For the pose above, suppose \( \bar{\boldsymbol{\Sigma}} = \text{diag}(0.04, 0.04, 0.01) \), R = diag(0.01, 0.0025), and the sensor measures range 5.2 m and bearing 0.90 rad. Find the innovation and the correction to x.

Show solution
  1. Innovation: \( [5.2 - 5.0,\ 0.90 - 0.927] = [0.2,\ -0.027] \).
  2. Compute \( \mathbf{S} = \mathbf{H}\bar{\boldsymbol{\Sigma}}\mathbf{H}^{\mathsf{T}} + \mathbf{R} \): range–range \( 0.04(0.36 + 0.64) + 0.01 = 0.05 \); bearing–bearing \( 0.04(0.0256 + 0.0144) + 0.01 + 0.0025 = 0.0141 \); cross term \( 0.04(-0.096 + 0.096) = 0 \).
  3. Gain for x: \( \bar{\boldsymbol{\Sigma}}\mathbf{H}^{\mathsf{T}} \) row x is \( [0.04(-0.6),\ 0.04(0.16)] = [-0.024,\ 0.0064] \); divide by S diagonal: \( [-0.48,\ 0.454] \).
  4. Correction to x: \( -0.48(0.2) + 0.454(-0.027) = -0.096 - 0.012 = -0.108 \) m.

Answer: The innovation is (0.2 m, −0.027 rad) and x moves by about −0.11 m: the range was longer than expected, so the robot must be farther from the landmark than predicted, which, with the landmark up and to the right, means moving down and to the left. The y and θ components are corrected in the same way.

EKF localization of a robot driving a circle among three landmarks with known positions
import numpy as np
rng = np.random.default_rng(21)
LM = np.array([[5.0, 5.0], [-4.0, 6.0], [2.0, -5.0]])       # known landmarks
def wrap(a): return (a + np.pi) % (2*np.pi) - np.pi
def g(x, u): d, dth = u; return np.array([x[0] + d*np.cos(x[2]), x[1] + d*np.sin(x[2]), wrap(x[2] + dth)])
def h(x, m): dx, dy = m - x[:2]; return np.array([np.hypot(dx, dy), wrap(np.arctan2(dy, dx) - x[2])])
M = np.diag([0.02**2, np.radians(1)**2]); R = np.diag([0.1**2, np.radians(2)**2])
x = np.array([0.0, 0.0, 0.0]); mu = x.copy(); S = np.diag([0.01, 0.01, 0.001])
errs = []
for t in range(200):
    u = (0.1, 0.05)                                              # drive in a circle
    x = g(x, (u[0] + rng.normal(0, 0.02), u[1] + rng.normal(0, np.radians(1))))
    th = mu[2]; d = u[0]
    G = np.array([[1, 0, -d*np.sin(th)], [0, 1, d*np.cos(th)], [0, 0, 1]])
    V = np.array([[np.cos(th), 0], [np.sin(th), 0], [0, 1]])
    mu = g(mu, u); S = G @ S @ G.T + V @ M @ V.T
    for m in LM:
        z = h(x, m) + rng.multivariate_normal([0, 0], R)
        dx, dy = m - mu[:2]; q = dx*dx + dy*dy; r = np.sqrt(q)
        H = np.array([[-dx/r, -dy/r, 0], [dy/q, -dx/q, -1]])
        y = z - h(mu, m); y[1] = wrap(y[1])
        K = S @ H.T @ np.linalg.inv(H @ S @ H.T + R)
        mu = mu + K @ y; mu[2] = wrap(mu[2]); S = (np.eye(3) - K @ H) @ S
    errs.append(np.r_[x[:2] - mu[:2], wrap(x[2] - mu[2])])
e = np.array(errs)
print("RMSE x, y (m):", np.round(np.sqrt((e[:, :2]**2).mean(0)), 3), " heading (deg):", round(np.degrees(np.sqrt((e[:, 2]**2).mean())), 2))
print("final 1-sigma (m, m, deg):", np.round(np.sqrt(np.diag(S))[:2], 3), round(np.degrees(np.sqrt(S[2, 2])), 2))
Output
RMSE x, y (m): [0.027 0.021]  heading (deg): 0.87
final 1-sigma (m, m, deg): [0.035 0.017] 0.87

With three landmarks always in view, the EKF holds the pose to a few centimeters and a fraction of a degree, and the reported standard deviations are of the same size as the actual errors, a sign of a well-matched filter. Remove the landmarks and the covariance would grow without bound as odometry drift accumulates, exactly as dead reckoning does; add them back and the filter pulls the estimate in and shrinks the covariance in a single update.

When the EKF fails

The EKF's linearization introduces two kinds of error. The first is bias in the mean: the mean of a nonlinear function is not the function of the mean, so the EKF's predicted mean can be systematically wrong, as the banana example of lesson 3 showed. The second is error in the covariance: the Jacobian captures only the local slope, so when curvature matters the true spread is larger or differently shaped than \( \mathbf{G}\boldsymbol{\Sigma}\mathbf{G}^{\mathsf{T}} \). Both grow with the size of the uncertainty relative to the scale over which the functions curve. A particularly dangerous consequence is inconsistency: the EKF can become overconfident, its covariance shrinking faster than its actual error, after which it ignores the very measurements that could rescue it. Linearizing about a wrong estimate makes this worse, because the Jacobians themselves are then wrong.

The EKF also inherits the Kalman filter's unimodal belief. It cannot represent 'I am at one of these three doors', so it cannot perform global localization from an unknown start, and it handles data association errors badly: a single wrong landmark match pulls the estimate firmly in the wrong direction. Practical remedies include initializing carefully (from a known start or a global method), gating measurements whose innovation is implausible (lesson 9), inflating Q to stay humble, and, for strongly nonlinear measurements, the iterated EKF, which re-linearizes the measurement function around the updated estimate and repeats the update a few times, equivalent to Gauss–Newton optimization of the posterior.

Worked problem

When does curvature matter?

A robot predicts its position after driving d = 10 m with heading standard deviation σ_θ. The forward-distance bias from heading error is approximately \( d\sigma_\theta^2/2 \). Compare the bias with the lateral standard deviation \( d\sigma_\theta \) for σ_θ = 2° and 20°.

Show solution
  1. 2° = 0.0349 rad: bias \( 10(0.00122)/2 = 0.0061 \) m; lateral σ = 0.349 m; ratio 0.017.
  2. 20° = 0.349 rad: bias \( 10(0.1218)/2 = 0.609 \) m; lateral σ = 3.49 m; ratio 0.17.

Answer: At 2° the bias is under 2 % of the spread and the EKF's linearization is excellent. At 20° the bias is 61 cm, 17 % of the spread, and the true distribution is visibly curved; an unscented or particle filter would represent it better.

A simple diagnostic catches many EKF problems before they reach the field. Log the NIS of every landmark update. If the EKF is consistent, the NIS values for 2-D range-bearing updates should average about 2, and about 99 % of them should fall below 9.21. A slow upward trend in NIS while the reported covariance keeps shrinking is the signature of creeping overconfidence, and it usually appears long before the position error becomes visible on a map.

Two refinements are common in modern robotics. The error-state EKF, used in most inertial navigation and visual-inertial odometry systems, estimates a small error around a nominal trajectory rather than the full state; small errors keep linearization accurate and make it natural to handle orientations, which do not form a vector space, with a three-element rotation error. And for orientations in general, filters now often work on the rotation group directly (sometimes called Lie-group or invariant EKFs), avoiding the singularities of Euler angles discussed in ROB-210.

Common mistakes

  • MistakePropagating the mean through the Jacobian instead of the nonlinear function

    InsteadUse g and h for the means; use G and H only for the covariances.

  • MistakeEvaluating H at the old estimate instead of the predicted one

    InsteadEvaluate each Jacobian at the most recent estimate available.

  • MistakeForgetting to wrap heading and bearing after updates

    InsteadWrap angles after every prediction, innovation and correction.

  • MistakeStarting an EKF with a wrong pose and small covariance

    InsteadInitialize with a realistic covariance or use a global method first.

  • MistakeTrusting the EKF when heading uncertainty is large

    InsteadUse an unscented or particle filter, or an iterated or error-state EKF, when curvature matters.

Summary and key terms

  • The EKF linearizes nonlinear motion and measurement functions with Jacobians evaluated at the current estimate.
  • Means pass through the true nonlinear functions; covariances pass through the Jacobians.
  • Process noise on the controls maps into state space as \( \mathbf{V}\mathbf{M}\mathbf{V}^{\mathsf{T}} \).
  • EKF landmark localization combines the odometry motion model with range-bearing updates and wrapped bearings.
  • Linearization causes mean bias and covariance error that grow with uncertainty relative to curvature.
  • The EKF is unimodal, cannot localize globally and is sensitive to data association errors.
  • Iterated, error-state and Lie-group EKFs reduce linearization problems in practice.
Extended Kalman filter
A Kalman filter that linearizes nonlinear models with Jacobians at each step.
Linearization
Approximating a function by its tangent at a point.
Jacobian
The matrix of first partial derivatives of a vector function.
Linearization error
The difference between a nonlinear function and its tangent approximation.
Inconsistency
A filter whose covariance is smaller than its actual errors.
Iterated EKF
An EKF that re-linearizes the measurement around the updated estimate several times.
Error-state EKF
An EKF that estimates a small error around a nominal state.
EKF localization
Pose estimation with an EKF using a known map of landmarks.
Control Jacobian
The matrix V mapping control noise into state noise.
Gauss–Newton
An iterative method for nonlinear least squares that the iterated EKF resembles.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. Linearize \( \sin x \) at x = 0.

    Show answer

    \( \sin x \approx x \).

  2. For y = 2x with σ_x = 0.5, what is σ_y?

    Show answer

    1.0.

  3. Landmark at relative (6, 8). Range Jacobian row with respect to (x, y)?

    Show answer

    [−0.6, −0.8].

  4. Bearing Jacobian with respect to heading?

    Show answer

    −1.

  5. At which estimate is \( \mathbf{G}_t \) evaluated?

    Show answer

    The previous posterior mean \( \boldsymbol{\mu}_{t-1} \).

  6. At which estimate is \( \mathbf{H}_t \) evaluated?

    Show answer

    The predicted mean \( \bar{\boldsymbol{\mu}}_t \).

  7. Heading σ of 10°, drive 4 m. Lateral σ?

    Show answer

    \( 4 \times 0.1745 = 0.70 \) m.

  8. Why can't an EKF localize globally from an unknown start?

    Show answer

    Its belief is a single Gaussian.

  9. What does the iterated EKF repeat?

    Show answer

    The measurement linearization and update.

  10. Why do inertial systems favor the error-state EKF?

    Show answer

    Errors stay small, keeping linearization accurate and handling rotations cleanly.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz