Foundations to Frontier
Tests
A. Mathematical Foundations › A8 Numerical Methods and Scientific Computing

Numerical Differentiation vs Automatic Differentiation

There are three ways to get derivatives on a computer: symbolic, numerical (finite differences) and automatic. Finite differences are easy but trade truncation error against rounding error, so the step size can never be made arbitrarily small. You will derive the error of forward and central differences and the optimal step, use the complex-step trick for derivatives accurate to machine precision, build a tiny forward-mode AD with dual numbers, and use gradient checking to catch a sign bug in a robot arm's Jacobian.

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.

How small should the step be? Finite differences vs complex step
1e01e-41e-81e-121e-16forwardcentralcomplex steplog₁₀ h →
f'(1.5) = 4.78748452276085 · at h = 1.0e-8: forward err 1.5e-8, central 1.5e-8, complex step 1.0e-17
Large h leaves truncation error (slope 1 for forward, 2 for central). Small h amplifies rounding: f(x + h) − f(x) cancels, leaving error ≈ ε/h. The best forward step is near √ε ≈ 1e−8 (≈8 correct digits) and the best central step near ∛ε ≈ 1e−5 (≈10 digits). The complex step, Im f(x + ih)/h, never subtracts, so it gives full precision for h as small as 1e−200. Automatic differentiation gives exact derivatives with no step at all.
Why engineers care

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.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Making the finite-difference step smaller always improves accuracy?
  2. 2. Automatic differentiation gives derivatives that are…
  3. 3. Which is used inside PyTorch and JAX?

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Finite differences

D+f=f(x+h)−f(x)h=f′(x)+h2f′′(ξ)D_+f = \frac{f(x + h) - f(x)}h = f'(x) + \frac h2f''(\xi)D0f=f(x+h)−f(x−h)2h=f′(x)+h26f′′′(ξ)D_0f = \frac{f(x + h) - f(x - h)}{2h} = f'(x) + \frac{h^2}6f'''(\xi)

Forward differences are first order, central differences second order. That’s the truncation error.

Rounding error

Each value of ff carries an error of about ε∣f∣\varepsilon\lvert f\rvert, and the difference divides it by hh. So the total error is roughly

E(h)≈Mh2+2ε∣f∣h ⇒ h∗∼ε≈10−8E(h)\approx\frac{Mh}2 + \frac{2\varepsilon\lvert f\rvert}h\ \Rightarrow\ h^*\sim\sqrt\varepsilon\approx10^{-8}

For central differences, h∗∼ε1/3≈10−5h^*\sim\varepsilon^{1/3}\approx10^{-5}. You get about 8 and 10–11 correct digits respectively, never 16.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Central difference truncation error is O(…)
  2. 2. Optimal forward-difference step in double ≈
  3. 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 ff: f(x+ih)=f(x)+ihf′(x)−h22f′′(x)−ih36f′′′(x)+…f(x + ih) = f(x) + ihf'(x) - \frac{h^2}2f''(x) - \frac{ih^3}6f'''(x) + \dots. So

f′(x)=Im⁡f(x+ih)h+O(h2)f'(x) = \frac{\operatorname{Im}f(x + ih)}h + O(h^2)

There’s no subtraction, so no cancellation. Take h=10−200h = 10^{-200} 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 a+bϵa + b\epsilon with ϵ2=0\epsilon^2 = 0. Arithmetic then propagates derivatives exactly: (a+bϵ)(c+dϵ)=ac+(ad+bc)ϵ(a + b\epsilon)(c + d\epsilon) = ac + (ad + bc)\epsilon and sin⁡(a+bϵ)=sin⁡a+bcos⁡a ϵ\sin(a + b\epsilon) = \sin a + b\cos a\,\epsilon. Seed x=x0+1ϵx = x_0 + 1\epsilon, and the ϵ\epsilon part of f(x)f(x) is f′(x0)f'(x_0).

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 ε
🐍 Exercise 1: step size, complex step and dual numbers
Implement forward and central differences, the complex-step derivative and a Dual number class. On the Squire–Trapp test function, find the best step for each finite difference, and show that the complex step and dual numbers reach machine precision.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The complex step avoids cancellation because…
  2. 2. In dual numbers, ε² =
  3. 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 E(h)=Mh2+2ε∣f∣hE(h) = \frac{Mh}2 + \frac{2\varepsilon\lvert f\rvert}h. Setting E′(h)=0E'(h) = 0 gives h∗=2ε∣f∣/Mh^* = 2\sqrt{\varepsilon\lvert f\rvert/M} and E(h∗)=2ε∣f∣ME(h^*) = 2\sqrt{\varepsilon\lvert f\rvert M}, so about half the digits are lost. ∎ For central differences, E=Mh26+ε∣f∣hE = \frac{Mh^2}6 + \frac{\varepsilon\lvert f\rvert}h, giving h∗∝ε1/3h^*\propto\varepsilon^{1/3} and E∝ε2/3E\propto\varepsilon^{2/3}.

Complex step: accuracy

Taylor-expand f(x+ih)f(x + ih) and take imaginary parts: Im⁡f(x+ih)/h=f′(x)−h2f′′′(x)/6+…\operatorname{Im}f(x + ih)/h = f'(x) - h^2f'''(x)/6 + \dots. The computation of Im⁡\operatorname{Im} involves no subtraction of nearly equal quantities, so its rounding error is O(ε∣f′∣)O(\varepsilon\lvert f'\rvert) regardless of hh. ∎

Dual numbers implement the chain rule

For a smooth gg, g(a+bϵ)=g(a)+g′(a)bϵg(a + b\epsilon) = g(a) + g'(a)b\epsilon (Taylor series truncated by ϵ2=0\epsilon^2 = 0). Composition then gives g(f(a+ϵ))=g(f(a))+g′(f(a))f′(a)ϵg(f(a + \epsilon)) = g(f(a)) + g'(f(a))f'(a)\epsilon. ∎

Exercise 3.1: digits from forward differences in fp32

ε≈1.2×10−7\varepsilon\approx1.2\times10^{-7}, so h∗≈3.5×10−4h^*\approx3.5\times10^{-4} and error about 3.5×10−43.5\times10^{-4} relative: only about 3–4 correct digits.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The optimal forward-difference error is about…
  2. 2. Central differences can reach roughly…
  3. 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.

🐍 Exercise 2 (research): checking a manipulator Jacobian
Write forward kinematics and the analytic Jacobian of a 3-link planar arm. Check the Jacobian against central differences and the complex step at 50 random poses, then show that the check catches a sign bug.

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

  1. W. Squire, G. Trapp (1998). “Using complex variables to estimate derivatives of real functions.” SIAM Review, 40(1).
  2. J. Martins, P. Sturdza, J. Alonso (2003). “The complex-step derivative approximation.” ACM TOMS, 29(3).
  3. A. Griewank, A. Walther (2008). Evaluating Derivatives: Principles and Techniques of Algorithmic Differentiation, 2nd ed. SIAM.
  4. A. Baydin, B. Pearlmutter, A. Radul, J. Siskind (2018). “Automatic differentiation in machine learning: a survey.” JMLR, 18.
  5. J. Nocedal, S. Wright (2006). Numerical Optimization, 2nd ed. Springer. Ch. 8.
  6. J. Andersson et al. (2019). “CasADi: a software framework for nonlinear optimization and optimal control.” Mathematical Programming Computation, 11.
  7. R. Tedrake et al. Drake: Model-based design and verification for robotics. drake.mit.edu.
  8. N. Higham (2002). Accuracy and Stability of Numerical Algorithms, 2nd ed. SIAM.