A camera fits its attitude to six direction pairs. Fit all nine matrix entries and the answer has determinant 0.91: not a rotation. Fit Euler angles from 89.9° pitch and the first step swings yaw and roll by about 3,000°. Six steps later the fit is still 42° off. Step on the manifold and the same Gauss-Newton is within 0.001° after five steps.
Pick a solver and a start, then press Run. Try the start at 89.9° with each solver. Then we build it piece by piece: the rules, the lock, the step, its Jacobian, the loop, and the covariance.
You need Lecture 2 (hat, exp and log of a rotation), Lecture 9 (Gauss-Newton and Levenberg-Marquardt), a little matrix algebra and a little Python. We build the rest from zero.
Lecture 10 of 14 in our follow-along of MIT 16.485, Visual Navigation for Autonomous Vehicles (VNAV, taught by Luca Carlone). Every idea is re-taught here in our own words and devices. Previous: Lecture 9, estimation and nonlinear least squares. Next: Lecture 11, visual and visual-inertial odometry.
Illustrative toy: six fixed body directions and a true attitude of yaw 50°, pitch 45°, roll −40°. The noise, 0.05 per component (about 3°), is one seeded draw. Error is the angle to the best rotation. Exact maths in your browser; NumPy and SciPy give the same digits.
Chapter 0
Fit all nine entries and watch the frame stretch as the noise grows
A drone's camera must know where it points. Its attitude is a rotation matrix R = RWB: it turns body numbers into world numbers. The camera has six direction pairs. Each pair is one direction written in body numbers, bi, and in world numbers, wi. The pairs carry noise of 0.05 per component, about 3°.
Lecture 9 gives a recipe. Write the residual ri = R bi − wi, add the squares, and minimise. The residual is linear in the nine entries of R, so Gauss-Newton on nine numbers lands on the minimum in one step. The answer has the lowest cost of any 3 × 3 matrix. Is it a rotation?
The purple arrows are the three columns of the fitted matrix; the pink frame is the true attitude. A rotation's columns end on the grey sphere and meet at right angles. Raise the noise.
Illustrative toy: the hero's six pairs. The slider scales one fixed standard-normal draw.
A rotation keeps lengths and angles. That gives rules on its nine entries:
R-transpose R equals I, and the determinant of R equals plus one.
The table R-transpose R has six different entries, because it is symmetric. Each must match I, so the nine entries obey six rules. Nine numbers minus six rules leave three free numbers: a rotation has 3 degrees of freedom.
The nine-entry fit at noise 0.05 has columns 0.87, 0.99 and 1.06 long, meeting at 91.6°, 88.8° and 90.8°. Its determinant is 0.91, and the size of R⊤R − I is 0.28. Its cost is 0.0178, below the best rotation's 0.0325: it wins by breaking the rules.
A tiny step breaks the rules too. Add the skew matrix of (0.03, 0.05, 0.10) to I and the determinant is 1.0134. A rotation's three free numbers sit inside nine, on a curved set called a manifold: a shape that looks flat when you zoom in. This lecture optimises on it.
Symptom: a fitted attitude that stretches or shears the map it builds.
Measure: the size of R⊤R − I and det R after every update. A rotation gives about 10−15 and 1.
Fix: keep R on the manifold at every step (Chapter 2). Do not repair it afterwards.
Chapter 1
Pitch the camera toward 90° and watch the yaw axis and the roll axis line up
Why not use three numbers and no rules? Euler angles do that: yaw ψ ("psi"), pitch θ ("theta") and roll φ ("phi"), with R = Rz(ψ) Ry(θ) Rx(φ), as in Lecture 1. Any three values give a rotation. Gauss-Newton can step them like Lecture 9's x and y.
The trouble is the map from angle changes to turns. Call a small change of the three angles Δe ("delta e"). It turns the camera by a small body turn δ, a 3-vector in radians.
Delta equals E of theta and phi, times delta e. The determinant of E equals minus cos theta.
Each angle turns the camera about one axis: the yaw column, the pitch column, the roll column. As the pitch theta nears 90°, the yaw column becomes minus the roll column. Two of the three angles then turn about the same axis, and det E falls to 0.
At θ = 89.9°, det E = −0.0017 and the yaw and roll axes are 0.1° apart. To turn the camera 1° about its own z axis, yaw and roll must each move 573° in opposite senses.
The teal frame is the camera. The two warm arrows are the axes yaw and roll turn about. The bars show how strongly the three angles reach each direction of turn. Slide the pitch to 90°.
Exact maths, yaw 0, roll 0. The bars are the singular values of E.
Gauss-Newton on the angles uses the Jacobian J E, so it solves with (J E)⊤(J E). At 89.9° that matrix is near singular, and the hero's first Euler step changes yaw and roll by about 3,000°. At exactly 90° it is singular, and the solver cannot take a step. This is gimbal lock. It belongs to the coordinates, not to the rotations: the camera at 90° can turn any way it likes.
Quaternions have four numbers and one rule: unit length. They never lock, but plain Gauss-Newton on four numbers steps off the unit sphere, like the nine-entry fit, unless you renormalise. Also q and −q are the same rotation.
Symptom: an attitude solver that is fast in level flight and wild or stuck when the camera points straight down.
Measure: cos θ at each iterate, and the condition number of the Gauss-Newton matrix.
Fix: step a small body turn on the manifold (Chapter 2). Keep Euler angles for display only.
Chapter 2
Take a step along the flat line and watch exp bend it back onto the circle
Keep R as nine numbers, but move it with three. At any attitude R, the tangent space is the flat space of small body turns δ, a 3-vector. It has no rules, so Gauss-Newton can solve for δ freely. Then one map brings the step back onto the rotations.
Lecture 2 built that map. The hat of δ, written δ∧, is its skew matrix. The exponential, exp, turns it into a rotation by Rodrigues' formula. The log goes back. Two operators package them:
R box-plus delta equals R times exp of delta-hat.
Turn the step delta into a rotation with exp. Multiply R by it on the right. A product of two rotations is a rotation, so R box-plus delta obeys all six rules for any delta.
R = Rz(30°) and δ = (0.10, 0.05, −0.03). The sum R + δ∧ has determinant 0.982 and R⊤R − I of size 0.085. R ⊞ δ gives 1.000 and 2 × 10−16.
The reverse operator measures the step between two rotations:
R-two box-minus R-one equals the vee of the log of R-one-transpose R-two.
Undo R-one with its transpose, then apply R-two. The log of that leftover turn is the step delta with R-one box-plus delta equal to R-two.
R1 = Rz(30°), R2 = Rz(40°): R2 ⊟ R1 = (0, 0, 0.1745), a 10° turn about z. In 3-D, (R1 ⊞ δ) ⊟ R1 returns δ = (0.10, 0.05, −0.03) to 3 × 10−17.
Planar rotations live on the grey circle. The warm line is the tangent space at the purple R. The red dot is R + Rδ∧; the purple ring is R ⊞ δ. Push the step past π.
Exact maths in SO(2), the planar case: a rotation is one angle, and δ is one number.
Absil, Mahony and Sepulchre call any such map a retraction: it returns R for a zero step and moves in the direction δ to first order. R exp(δ∧) is one. Projecting R(I + δ∧) to the nearest rotation is another; it agrees to first order.
Symptom: a heading residual that jumps from +179° to −179°, and a solver that kicks the camera round.
Measure: |R2 ⊟ R1| near π.
Fix: the chart covers one turn. Start close enough that steps stay well inside π.
Chapter 3
Shrink the step and watch the right Jacobian's error fall twice as fast as the wrong one's
Gauss-Newton needs a Jacobian: how each residual changes per unit of the unknown. The unknown is now the step δ, so we differentiate ri(R ⊞ δ) at δ = 0. That gives a 3 × 3 block per pair, not 3 × 9.
For a small step, exp(δ∧) ≈ I + δ∧. The hat turns a product into a cross product: δ∧ b = δ × b = −b × δ = −b∧ δ. Put that into the residual:
r-i at R box-plus delta is about r-i at R, minus R b-i-hat, times delta.
Turning the body by delta swings the direction b-i by delta cross b-i, in body numbers. R carries that swing into the world, where the residual lives. J-i is that whole recipe as a matrix.
At R = I with b = (1, 0, 0), J = −b∧ has rows (0, 0, 0), (0, 0, 1), (0, −1, 0). A turn δ = (0, 0, 0.1) about z moves the residual by (0, 0.1, 0): the tip of b swings toward +y.
Check an analytic Jacobian against the truth. Take a step of size s, compare the true residual with the linear prediction, and shrink s. A correct Jacobian misses by an amount that falls like s2. A wrong one misses like s.
Both lines predict r(R ⊞ δ). Purple uses −R b∧, made for a step on the right. Red uses the left-side Jacobian with the same right-side step. Slide the step size.
Exact maths: R = exp((0.3, −0.2, 0.6)∧), b = the hero's first direction, step axis (0.6, 0, 0.8).
A step can also go on the left, exp(δ∧) R, turning about world axes. Then Ji = −(R bi)∧, which equals the right-side Jacobian times R⊤: Lecture 2's adjoint. At s = 0.01 the matched pair misses by 2.7 × 10−5; the mismatched pair by 2.2 × 10−3, 80 times more. Libraries differ. GTSAM composes its step on the right; Ceres's QuaternionManifold multiplies on the left; Barfoot's book perturbs on the left. This series uses the right.
Symptom: Gauss-Newton converges slowly or wanders, though each piece looks right.
Measure: finite differences with the same ⊞ your solver uses. Shrink the step 10 times: the miss must fall 100 times.
Fix: make the Jacobian and the update use the same side.
Chapter 4
Step the solver and watch the six fitted directions slide onto the measured ones
Now Lecture 9's loop runs with one line changed. Stack the six blocks Ji into J, 18 × 3, and the six residuals into r, 18 numbers. Solve for the step, then move with ⊞, not +.
J transpose J, times delta, equals minus J transpose r. Then R becomes R box-plus delta.
From the level start, J⊤r = (3.92, −0.50, −3.73). The step is δ = (−0.584, 0.157, 0.801), a 57.5° turn. The cost falls from 10.23 to 1.58, and the error to the best rotation from 87.6° to 32.2°.
Blue dots are the measured directions wi; purple dots are R bi for the current R. Yellow lines are the residuals. Choose a start and press Step.
The hero's pairs. Drag sideways on the sphere to turn the view.
From level, the error runs 87.6°, 32.2°, 2.92°, 0.059°, 0.0015°. R⊤R − I stays below 10−15 the whole way. From 500 random starts every run came within 0.001° of the best rotation, after a median of 6 steps. The best rotation matches SciPy's closed-form answer, Rotation.align_vectors, to 10−13°.
Levenberg-Marquardt carries over the same way: add λI to J⊤J, try R ⊞ δ, and keep it only if the cost falls. From level it needs 6 steps, every one downhill.
Symptom: the cost falls but the step δ never shrinks to zero.
Measure: |δ| per step, and the finite-difference check of Chapter 3.
Fix: the update and the Jacobian disagree on the side, or the update adds δ∧ to R.
Chapter 5
Move a cluster of headings across 180° and watch the plain average jump to the wrong side
Five sensors each measure the same attitude: R̃1 to R̃5 ("R-tilde"). What is their mean? Averaging nine entries breaks the rules, and averaging Euler angles depends on the chart. On the manifold, the mean is the R that makes the sum of squared tangent distances smallest. That R is the geodesic mean, also called the Karcher mean.
The residual is now a log: ri = R̃i ⊟ R. Its Jacobian is −Jl−1(ri), with Jl Lecture 2's left Jacobian. Because Jl(r)⊤ = Jr(r) and Jr(r) r = r, the slope of the cost is exactly −Σ ri. Replacing the Jacobian by −I gives a famous update:
Delta equals one over N times the sum over i of R-tilde-i box-minus R-bar.
Stand at the current mean R-bar. Measure the turn to each measurement with box-minus. Average the turns, and box-plus the average onto R-bar. Repeat until the average turn is zero.
In the plane: measurements at 20° and 40°, R̄ = 0°. The turns are 0.349 and 0.698 rad, so δ = 0.524 rad = 30°. One step lands on the mean, 30°.
Blue dots are five measured headings. The ring is the plain average of their angles; the purple dot is the geodesic mean, with its steps in warm. Slide the cluster across 180°.
Illustrative: one fixed standard-normal draw times the spread. The mean starts at the first measurement.
In 3-D, take five measurements with 0.3 rad of noise and start at the identity, 34° away. After two steps the exact Jacobian is 0.0033° off and the mean update 0.0008°. Both stop at the same R, because they share the slope. The average of the Euler angles lands 0.50° off it. The rotation nearest to the plain matrix average lands 0.024° off: close, because the spread is small.
Symptom: an averaged heading on the opposite side of the circle from every measurement.
Measure: the residuals at the answer. They should sum to zero and be small.
Fix: average turns from a nearby rotation with ⊟, and start at one of the measurements.
Chapter 6
Align a scan to a map and watch a mismatched update throw the pose away
A pose T = (R, t) adds a shift t to the turn. A lidar scan gives points pi in body numbers, and a map gives the same points qi in world numbers. The residual is ri = R pi + t − qi. The step is now 6 numbers, a twist ξ = (φ, ρ) ("xi equals phi, rho"), rotation first as in Lecture 2.
On the right, T ⊞ ξ = T exp(ξ∧) turns by exp(φ∧) and shifts by R Jl(φ) ρ. To first order the shift is Rρ: ρ is in body numbers, like φ.
r-i at T box-plus xi is about r-i at T, plus the block row minus R p-i-hat, R, times xi.
A body turn phi swings point p-i by phi cross p-i, as before. A body shift rho moves every point by R rho. Stack the rows, solve the 6 × 6 system, and move with T exp of xi-hat.
A planar scan of 8 points with true pose 35°, (1.2, 0.6) m and 0.05 m of noise. From 0°, (0, 0) the cost runs 22.64, 1.87, 0.033, 0.0317. The estimate, 37.14°, (1.213, 0.533) m, matches the closed-form answer.
Blue dots are the map points qi; purple dots are the scan under the current pose. Choose the update and a start, then press Run.
Illustrative toy: SE(2), the planar pose, with a 3-number twist. SE(3) is the same with 6.
The Jacobian assumes the shift is Rρ. Add ρ in world numbers instead and the step points the wrong way whenever R is far from I. From 170° the cost goes 93.3, 245, 554, 730 before it turns round. With T ⊞ ξ it goes 93.3, 18.6, 3.84, 0.22, 0.032. Forster and colleagues' visual-inertial odometry uses the same right-side update; Lecture 11 builds on it.
Symptom: pose estimates that converge from small rotations and blow up from large ones.
Measure: the cost after each step from a start 90° or more away.
Fix: apply the shift as Rρ (or use T exp(ξ∧)) to match the Jacobian's R block.
Chapter 7
Repeat the fit thousands of times and watch the cloud fill the predicted ellipse
How sure is the attitude? A rotation has no mean and spread in nine entries. But the error can live in the tangent space: the true R is R̂ ⊞ δ ("R-hat box-plus delta"), with δ a small random body turn. Its covariance is a 3 × 3 matrix, like Lecture 9's.
Sigma-delta is about sigma squared, times J transpose J inverse.
The curvature of the cost for small body turns is J transpose J over sigma squared. Invert it: directions where the cost curves gently are the ones you know least.
J⊤J = [[4.06, −0.51, −1.84], [−0.51, 4.50, −0.63], [−1.84, −0.63, 3.44]]. With σ = 0.05 the predicted spread about the body x, y and z axes is 1.68°, 1.41° and 1.84°. In 4,000 seeded repeats in NumPy it was 1.70°, 1.39° and 1.82°, and 95.6% of the errors fell inside the 95% ellipsoid.
Each purple dot is one repeat: new noise, a new fit, and its error δ about body x (across) and z (up), in degrees. The purple ellipse is the 95% prediction. Press Run.
Illustrative: the hero's six directions, noise 0.05, fresh seeded draws. Each fit is Gauss-Newton on the manifold, run in your browser.
The ellipse leans because the six directions are not spread evenly. A world-frame covariance is R̂ Σδ R̂⊤, by the adjoint of Chapter 3.
Symptom: a filter that is overconfident in heading after the camera turns.
Measure: the frame of the 3 × 3 covariance: body or world.
Fix: keep one side for steps, Jacobians and covariances; convert with R Σ R⊤.
Chapter 8
Choose the true attitude, the start and the noise, and watch four solvers race step by step
Everything at once. Four solvers fit the same six pairs: nine entries, Euler angles, Gauss-Newton on the manifold, and Levenberg-Marquardt on the manifold. The plot shows each one's error to the best rotation per step, and the read-out shows whether each answer is a rotation.
Set the true pitch, the start and the noise, then press Play to step all four together.
Illustrative toy: the hero's pairs, true yaw 50° and roll −40°. Levenberg-Marquardt starts with λ = the largest diagonal entry of J⊤J and divides or multiplies it by 10.
Things to try. Put the true pitch at 90° and start level: Euler angles still converge, because the lock bites only where an iterate sits near 90°. Start at 89.9° and they wander. At true pitch 45°, raise the noise to 0.15: the nine-entry fit's determinant falls to 0.74, while both manifold solvers stay rotations to 15 digits.
Chapter 9
Keep the operators, the numbers and the code side by side
A rotation is nine numbers with six rules, so free entries break it and three angles lock. Solve for a small body turn δ in the tangent space, with Jacobians taken with respect to δ, and move with R ⊞ δ = R exp(δ∧). The rest is Lecture 9: Gauss-Newton, Levenberg-Marquardt, and a covariance (J⊤J)−1, now over δ.
| Piece | Formula | Number on this page | Watch out |
|---|---|---|---|
| Rules of a rotation | R⊤R = I, det R = +1 | nine-entry fit: det 0.91 | projecting lands 4.4° off |
| Euler map | δ = E Δe, det E = −cos θ | 89.9°: first step about 3,000° | every order locks somewhere |
| Box-plus, box-minus | R exp(δ∧), log(R1⊤R2)∨ | round trip to 3 × 10−17 | the chart covers one turn |
| Jacobian | Ji = −R bi∧ (right) | miss 2.7 × 10−5 at 0.01 rad | left: −(R bi)∧ |
| Gauss-Newton | (J⊤J)δ = −J⊤r, R ← R ⊞ δ | 500 of 500 starts; median 6 steps | same side as J |
| Rotation averaging | δ = (1/N) Σ R̃i ⊟ R̄ | 20°, 40° → 30° in one step | start near the cluster |
| Pose Jacobian | [−R pi∧ R], ξ = (φ, ρ) | 37.14°, (1.213, 0.533) m | shift is Rρ, not ρ |
| Covariance | Σδ ≈ σ2(J⊤J)−1 | 1.68°, 1.41°, 1.84°; 95.6% inside | say which frame |
python import numpy as np from scipy.spatial.transform import Rotation def hat(p): return np.array([[0, -p[2], p[1]], [p[2], 0, -p[0]], [-p[1], p[0], 0]]) def boxplus(R, d): return R @ Rotation.from_rotvec(d).as_matrix() # R exp(d^), d in body numbers def boxminus(R2, R1): return Rotation.from_matrix(R1.T @ R2).as_rotvec() # log(R1^T R2)^v def fit_attitude(B, W, R, iters=20, lam=0.0): """B, W: (N, 3) body and world directions. Gauss-Newton (lam = 0) or damped, on SO(3).""" for _ in range(iters): r = (R @ B.T).T - W # residuals, (N, 3) J = np.stack([-R @ hat(b) for b in B]) # (N, 3, 3): J_i = -R b_i^ H = np.einsum('nki,nkj->ij', J, J) # J^T J = sum(I - b b^T) g = np.einsum('nki,nk->i', J, r) # J^T r d = np.linalg.solve(H + lam * np.eye(3), -g) R = boxplus(R, d) # the one changed line if np.linalg.norm(d) < 1e-12: break return R, np.linalg.inv(H) # estimate; covariance / sigma^2 def best_closed_form(B, W): # the SVD answer, as a check U, _, Vt = np.linalg.svd(W.T @ B); D = np.diag([1, 1, np.linalg.det(U @ Vt)]) return U @ D @ Vt
Our loop and the closed-form SVD answer (also SciPy's Rotation.align_vectors) agree to 10−13° on the hero's pairs. Our exp and log match scipy.spatial.transform.Rotation to 5 × 10−14 on 2,000 random turns. The SVD answer exists only for this problem; the loop works for any residual.
Lecture 2 built exp, log, the adjoint and the Jacobians of exp. Lecture 9 built the solvers for vector unknowns. This lecture joins them, so rotations and poses can be unknowns. Lecture 11 runs it on IMU and camera states, and Lecture 13 on thousands of poses at once.
Now press Present or Teach. Count the rules of a rotation, show where Euler angles lock, define ⊞ and ⊟, derive Ji = −R bi∧, and write the loop. Then go back to the hero and explain why the Euler solver wanders from 89.9° while the manifold solver comes home in five steps.