Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Measuring a slope
To find how steep a hill is, walk a few steps and measure how much you climbed. Take big steps and you miss the curves; take tiny steps and your measuring tape isn’t precise enough to see the change. A computer has exactly this problem when it estimates derivatives from function values. Automatic differentiation avoids it completely: it computes exact slopes by applying the rules of calculus to each step of the program.
Every gradient-based optimizer, trajectory planner and neural network needs derivatives. Finite differences are the quick check; automatic differentiation is what PyTorch, JAX and trajectory-optimization tools like CasADi use in production. Gradient checking against finite differences catches the bugs.
- 1. Making the finite-difference step smaller always improves accuracy?
- 2. Automatic differentiation gives derivatives that are…
- 3. Which is used inside PyTorch and JAX?
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.Finite differences
Forward differences are first order, central differences second order. That’s the truncation error.
Rounding error
Each value of carries an error of about , and the difference divides it by . So the total error is roughly
For central differences, . You get about 8 and 10–11 correct digits respectively, never 16.
- 1. Central difference truncation error is O(…)
- 2. Optimal forward-difference step in double ≈
- 3. Rounding error in a finite difference scales like…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.The complex-step derivative
For real-analytic : . So
There’s no subtraction, so no cancellation. Take and the result is exact to machine precision (Squire and Trapp, 1998; Martins, Sturdza and Alonso, 2003). It needs complex-capable code with no abs or comparisons on the imaginary part.
Forward-mode AD with dual numbers
Extend each number to with . Arithmetic then propagates derivatives exactly: and . Seed , and the part of is .
Costs
| Method | Gradient of f: ℝⁿ → ℝ | Accuracy |
|---|---|---|
| Forward FD | n + 1 evaluations | ~√ε |
| Central FD | 2n evaluations | ~ε^(2/3) |
| Complex step | n complex evaluations | ε |
| Forward AD | n passes (or vectorized) | ε |
| Reverse AD | ~1 forward + 1 backward | ε |
- 1. The complex step avoids cancellation because…
- 2. In dual numbers, ε² =
- 3. The cost of a central-difference gradient in n dimensions:
Level 3: Advanced
★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.Optimal step for forward differences
Minimize . Setting gives and , so about half the digits are lost. ∎ For central differences, , giving and .
Complex step: accuracy
Taylor-expand and take imaginary parts: . The computation of involves no subtraction of nearly equal quantities, so its rounding error is regardless of . ∎
Dual numbers implement the chain rule
For a smooth , (Taylor series truncated by ). Composition then gives . ∎
Exercise 3.1: digits from forward differences in fp32
, so and error about relative: only about 3–4 correct digits.
- 1. The optimal forward-difference error is about…
- 2. Central differences can reach roughly…
- 3. Dual numbers work because…
Level 4: Expert
★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.Gradient checking a robot Jacobian
Hand-derived Jacobians for kinematics, dynamics and filters are a classic source of silent bugs. A sign error still lets a controller run, just badly. The standard test compares the analytic Jacobian with central differences (agreement to better than 1e−9; the exact figure differs slightly by platform) or the complex step (about 1e−16) at many random configurations. The exercise does this for a 3-link planar arm and catches a planted sign error with a relative discrepancy of order 1. Projects like Drake and the Ceres solver ship automatic gradient checkers for exactly this reason.
Differentiation in AI, robotics and aerospace
- Aerospace design optimization: the complex step and adjoints for CFD and structural sensitivities (Martins, Sturdza and Alonso, 2003; the MDO community).
- Trajectory optimization: CasADi and Drake use AD to build exact Jacobians and Hessians for NLP solvers (IPOPT, SNOPT).
- Deep learning: reverse-mode AD (backprop), with finite-difference gradient checks during development.
- Differentiable simulation: differentiable physics engines (Brax, MuJoCo MJX, Warp) for learning and control.
References
- W. Squire, G. Trapp (1998). “Using complex variables to estimate derivatives of real functions.” SIAM Review, 40(1).
- J. Martins, P. Sturdza, J. Alonso (2003). “The complex-step derivative approximation.” ACM TOMS, 29(3).
- A. Griewank, A. Walther (2008). Evaluating Derivatives: Principles and Techniques of Algorithmic Differentiation, 2nd ed. SIAM.
- A. Baydin, B. Pearlmutter, A. Radul, J. Siskind (2018). “Automatic differentiation in machine learning: a survey.” JMLR, 18.
- J. Nocedal, S. Wright (2006). Numerical Optimization, 2nd ed. Springer. Ch. 8.
- J. Andersson et al. (2019). “CasADi: a software framework for nonlinear optimization and optimal control.” Mathematical Programming Computation, 11.
- R. Tedrake et al. Drake: Model-based design and verification for robotics. drake.mit.edu.
- N. Higham (2002). Accuracy and Stability of Numerical Algorithms, 2nd ed. SIAM.