Numerical Differentiation Calculator
Approximate derivatives of functions using forward, backward, and central difference methods.
Compare accuracy, step size effects, and higher-order formulas.
What Is Numerical Differentiation? Numerical differentiation approximates the derivative of a function using only function values at discrete points. It is essential when the function has no closed-form expression — for example, experimental data, simulation outputs, or complex algorithms. It is also used in numerical ODE solvers, optimization algorithms, and computational physics.
Finite Difference Formulas The derivative is defined as: f’(x) = lim_{h→0} [f(x+h) − f(x)] / h Numerical methods approximate this limit with a finite step h.
Forward difference (first-order, O(h) error): f’(x) ≈ [f(x+h) − f(x)] / h
Backward difference (first-order, O(h) error): f’(x) ≈ [f(x) − f(x−h)] / h
Central difference (second-order, O(h²) error): f’(x) ≈ [f(x+h) − f(x−h)] / (2h)
Five-point central difference (fourth-order, O(h⁴) error): f’(x) ≈ [−f(x+2h) + 8f(x+h) − 8f(x−h) + f(x−2h)] / (12h)
Higher-Order Derivatives Second derivative (central difference, O(h²)): f’’(x) ≈ [f(x+h) − 2f(x) + f(x−h)] / h² Fourth-order second derivative: f’’(x) ≈ [−f(x+2h) + 16f(x+h) − 30f(x) + 16f(x−h) − f(x−2h)] / (12h²)
Step Size Selection Too large h: truncation error dominates — approximation is inaccurate. Too small h: roundoff error from floating-point arithmetic dominates (catastrophic cancellation). Optimal h for forward/backward: h ≈ √ε_mach ≈ 10⁻⁸ (for double precision, ε_mach ≈ 2.2×10⁻¹⁶). Optimal h for central difference: h ≈ ε_mach^(1/3) ≈ 10⁻⁵. Rule of thumb: h ≈ 10⁻⁴ to 10⁻⁶ for central differences in double precision.
Richardson Extrapolation Richardson extrapolation combines two approximations to cancel the leading error term. Using central difference with h and h/2: f’(x) ≈ [4D(h/2) − D(h)] / 3 where D(h) is the central diff. This yields a fourth-order accurate result from two second-order computations. Named after Lewis Fry Richardson (British mathematician) who described the technique in 1911.
Applications Automatic differentiation (AD) is preferred for software — it gives exact derivatives. But finite differences are essential for: validating AD implementations, sensitivity analysis, black-box models. In finite element analysis, stiffness matrices are assembled from numerical derivatives of shape functions. Neural networks once used finite differences to check gradient implementations (gradient checking).
Comparison With Symbolic Differentiation Symbolic: exact, but requires analytic formula. Can explode in complexity for nested functions. Automatic: exact to machine precision, works on code. Used in PyTorch, TensorFlow, JAX. Numerical (finite difference): simple, no code changes needed, but introduces truncation and roundoff errors.