Basic Calculus

SLAM back-ends spend most of their time minimizing nonlinear cost functions. Two tools from calculus make that possible: differentiation (Jacobians) and Taylor expansion (linearization).

Differentiation and Jacobians

A scalar function f:RnRf: \mathbb{R}^n \to \mathbb{R} has a gradient f=[fx1,,fxn]T\nabla f = \left[\frac{\partial f}{\partial x_1}, \ldots, \frac{\partial f}{\partial x_n}\right]^T. A vector function f:RnRm\mathbf{f}: \mathbb{R}^n \to \mathbb{R}^m has a Jacobian matrix:

J=fx=[f1x1f1xnfmx1fmxn]Rm×nJ = \frac{\partial \mathbf{f}}{\partial \mathbf{x}} = \begin{bmatrix} \frac{\partial f_1}{\partial x_1} & \cdots & \frac{\partial f_1}{\partial x_n} \\ \vdots & \ddots & \vdots \\ \frac{\partial f_m}{\partial x_1} & \cdots & \frac{\partial f_m}{\partial x_n} \end{bmatrix} \in \mathbb{R}^{m \times n}

The Jacobian is the key tool in SLAM optimization: given a residual e(x)\mathbf{e}(\mathbf{x}) (for example, a reprojection error), its Jacobian J=exJ = \frac{\partial \mathbf{e}}{\partial \mathbf{x}} tells us how the residual changes with small perturbations to the state — exactly what Gauss-Newton and Levenberg-Marquardt need.

The chain rule in SLAM residuals

SLAM residuals are almost always compositions of simpler functions, so their Jacobians come from the chain rule. The reprojection error of a map point X\mathbf{X} observed at pixel z\mathbf{z} under pose TT is

e=zπ(TX)\mathbf{e} = \mathbf{z} - \pi\big(T\,\mathbf{X}\big)

a composition of (1) a rigid transformation, (2) perspective division, and (3) the intrinsic mapping. Its Jacobian factors into a product:

ex=πXcXcx\frac{\partial \mathbf{e}}{\partial \mathbf{x}} = -\,\frac{\partial \pi}{\partial \mathbf{X}_c}\cdot\frac{\partial \mathbf{X}_c}{\partial \mathbf{x}}

where Xc=TX\mathbf{X}_c = T\mathbf{X} is the point in the camera frame. Deriving each factor separately and multiplying is far less error-prone than differentiating the whole expression at once — and it is exactly how SLAM libraries structure their analytic Jacobians.

Taylor Expansion

The Taylor series expands a smooth function ff around a point x0x_0:

f(x)=f(x0)+f(x0)(xx0)+12!f(x0)(xx0)2+f(x) = f(x_0) + f'(x_0)(x - x_0) + \frac{1}{2!}f''(x_0)(x - x_0)^2 + \cdots

For a multivariate function f(x)f(\mathbf{x}) around x0\mathbf{x}_0:

f(x)f(x0)+J(x0)(xx0)+12(xx0)TH(x0)(xx0)f(\mathbf{x}) \approx f(\mathbf{x}_0) + J(\mathbf{x}_0)(\mathbf{x} - \mathbf{x}_0) + \frac{1}{2}(\mathbf{x} - \mathbf{x}_0)^T H(\mathbf{x}_0)(\mathbf{x} - \mathbf{x}_0)

where JJ is the Jacobian (first order) and HH is the Hessian matrix (second order). Truncating at first order gives the linear approximation used in Gauss-Newton; truncating at second order gives the quadratic approximation used in Newton’s method.

From Taylor expansion to Gauss-Newton

Consider minimizing a sum of squared residuals F(x)=12e(x)2F(\mathbf{x}) = \frac{1}{2}\|\mathbf{e}(\mathbf{x})\|^2. Linearize the residual around the current estimate, e(x+Δx)e+JΔx\mathbf{e}(\mathbf{x} + \Delta\mathbf{x}) \approx \mathbf{e} + J\Delta\mathbf{x}, and substitute:

F(x+Δx)12e2+eTJΔx+12ΔxTJTJΔxF(\mathbf{x} + \Delta\mathbf{x}) \approx \frac{1}{2}\|\mathbf{e}\|^2 + \mathbf{e}^T J\,\Delta\mathbf{x} + \frac{1}{2}\Delta\mathbf{x}^T J^T J\,\Delta\mathbf{x}

This is a quadratic in Δx\Delta\mathbf{x}; setting its derivative to zero gives the normal equations:

(JTJ)Δx=JTe(J^T J)\,\Delta\mathbf{x} = -J^T \mathbf{e}

Gauss-Newton is therefore “Newton’s method with HJTJH \approx J^T J” — the second-derivative terms of the true Hessian are dropped, which is a good approximation when residuals are small. Levenberg-Marquardt adds a damping term, solving (JTJ+λI)Δx=JTe(J^TJ + \lambda I)\Delta\mathbf{x} = -J^T\mathbf{e}, interpolating between Gauss-Newton (λ0\lambda \to 0) and gradient descent (λ\lambda large).

Checking Jacobians numerically

Analytic Jacobians are notoriously easy to get wrong (a sign, a transposed block). The standard sanity check is central finite differences: perturb one state dimension at a time and compare

J:,ke(x+h1k)e(xh1k)2hJ_{:,k} \approx \frac{\mathbf{e}(\mathbf{x} + h\,\mathbf{1}_k) - \mathbf{e}(\mathbf{x} - h\,\mathbf{1}_k)}{2h}

with a small step (e.g., h106h \sim 10^{-6}). Every serious SLAM codebase has a unit test doing exactly this for each residual type.

Common pitfalls

Why it matters for SLAM

In bundle adjustment, the reprojection error e(x+Δx)e(x)+JΔx\mathbf{e}(\mathbf{x} + \Delta\mathbf{x}) \approx \mathbf{e}(\mathbf{x}) + J\Delta\mathbf{x} is linearized around the current estimate. This converts the nonlinear least-squares problem into a sequence of linear systems (JTJ)Δx=JTe(J^T J)\Delta\mathbf{x} = -J^T \mathbf{e}, solved iteratively. Every optimization-based SLAM system — from pose graph optimization to full bundle adjustment — is built on this linearize-solve-update loop, so being able to derive Jacobians by hand (and check them numerically) is a core skill.