Phase I — Mathematical Foundations | Week 1 | 2.5 hours The gradient points uphill. Optimization is the art of going downhill efficiently.
Every optimization step in SLAM, calibration, and MPC requires computing gradients and Jacobians. The Jacobian of the sensor model maps state perturbations to measurement changes — it’s the linearization that makes nonlinear problems tractable.
For $f: \mathbb{R}^n \to \mathbb{R}$:
Key property: $\nabla f(x)$ points in the direction of steepest ascent. The magnitude $\|\nabla f\|$ is the steepness.
Directional derivative: $D_v f(x) = \nabla f(x) \cdot v$ — rate of change along direction $v$.
For $f: \mathbb{R}^n \to \mathbb{R}^m$:
Row $i$ = gradient of $f_i$. Column $j$ = how all outputs change when $x_j$ changes.
For $h(x) = f(g(x))$:
This is fundamental to: backpropagation, AD, and computing gradients of complex residuals.
| Function | Gradient |
|---|---|
| $f(x) = a^T x$ | $\nabla f = a$ |
| $f(x) = x^T A x$ | $\nabla f = (A + A^T)x$ |
| $f(x) = \|Ax - b\|^2$ | $\nabla f = 2A^T(Ax - b)$ |
| $f(x) = \log \det(X)$ | $\nabla f = X^{-T}$ |
# See: code/week01/gradients_jacobians.py
Compute gradients by hand, verify with SymPy.
Compute the Jacobian of $f(x,y,z) = [x^2+y, \; yz, \; xz^2]$ by hand, verify with SymPy.
Implement the central difference gradient:
Compare numerical and analytic gradients. Plot error vs step size $h$ to see the truncation/rounding tradeoff.
Compute gradients by hand (verify with code):
Problem: Compute $\nabla_x \|Ax - b\|_2$ (not the squared norm!). What happens at $Ax = b$?
Solution: Using chain rule: $\nabla_x \|Ax - b\|_2 = \frac{A^T(Ax - b)}{\|Ax - b\|_2}$. At $Ax = b$, the gradient is undefined (0/0) — the norm is not differentiable at zero. This is why we usually minimize $\|Ax - b\|^2$ instead (differentiable everywhere). Using the non-squared norm leads to $\ell_1$-like behavior and requires subgradient methods.
Problem: In a pose-graph SLAM with $n$ poses and $m$ relative-pose measurements, what is the sparsity pattern of the measurement Jacobian? Why does this matter for efficiency?
Solution: Each measurement only involves 2 poses, so each row of the Jacobian has at most $2 \times \text{pose\_dim}$ nonzero entries out of $n \times \text{pose\_dim}$ columns. The Jacobian is extremely sparse. The normal equations $J^T J$ inherit this sparsity (block structure). Exploiting this sparsity is why Ceres, g2o, and GTSAM can solve thousand-pose problems in milliseconds.
Problem: Design a robust gradient checker that works for functions with very small gradients (where relative error is meaningless). What should the comparison metric be?
Solution: Use the relative error formula: $\frac{|g_\text{analytic} - g_\text{numerical}|}{|g_\text{analytic}| + |g_\text{numerical}| + \epsilon}$ where $\epsilon \sim 10^{-7}$. The denominator prevents division by near-zero. Threshold: error < $10^{-5}$ for float64 with central differences. Use $h \approx \epsilon^{1/3} \approx 10^{-5}$ for central differences. Run at multiple random points — a single point might be at a saddle where gradients are zero anyway.