Finite Difference Generator
Compute finite difference coefficients with exact rational arithmetic and error terms for any derivative order and stencil.
Finite Difference Approximations
Finite differences approximate derivatives of a function \(f(x)\) using values at discrete grid points \(x + jh\), where \(h\) is the step size and \(j\) is an integer offset. They are fundamental to numerical differentiation, solving ODEs and PDEs, and analysing experimental data.
Taylor's Theorem with Remainder
For a sufficiently smooth function \(f\), Taylor expansion about \(x\) gives:
where \(\xi_j\) lies between \(x\) and \(x+jh\). A finite difference formula combines several such expansions so that all Taylor terms cancel except the target derivative and a leading error term.
Common Formulas
- Forward difference: \(f'(x) \approx \dfrac{f(x+h) - f(x)}{h} + O(h)\)
- Backward difference: \(f'(x) \approx \dfrac{f(x) - f(x-h)}{h} + O(h)\)
- Central difference: \(f'(x) \approx \dfrac{f(x+h) - f(x-h)}{2h} + O(h^2)\) — higher accuracy from symmetry
- Second derivative: \(f''(x) \approx \dfrac{f(x+h) - 2f(x) + f(x-h)}{h^2} + O(h^2)\)
This Tool
Enter any set of integer stencil points (e.g., \(-2, -1, 0, 1, 2\)) and a derivative order. You can also use MATLAB-style ranges instead of listing every point: -2:2 expands to \(-2,-1,0,1,2\), and a:s:b steps by \(s\) (e.g. -4:2:4 gives \(-4,-2,0,2,4\)); ranges and individual points can be mixed and comma-separated. The tool solves the Vandermonde system using exact rational (BigInt) arithmetic — no floating-point rounding — and returns:
- The finite difference formula with exact rational coefficients
- The leading error term \(C \cdot h^p \cdot f^{(q)}(\xi)\) with exact constant \(C\)
- A log-log convergence plot demonstrating the computed order of accuracy \(O(h^p)\) using \(f(x) = e^x\)
For an \(n\)-th derivative you need at least \(n+1\) stencil points. Symmetric stencils (e.g., \(-2,\!-1,\!0,\!1,\!2\)) typically yield higher-order accuracy due to cancellation of odd error terms.
Round-off vs. truncation. The convergence plot shows the error two ways. In ordinary double precision the error decreases as \(O(h^p)\) only until it reaches the finite-difference round-off wall near \(10^{-16}\) — subtracting nearly-equal function values and dividing by \(h^{\text{order}}\) magnifies rounding, so shrinking \(h\) further makes the error worse (dramatically so for high derivatives). Recomputing the identical formula at ~40-digit precision (via decimal.js) pushes that wall far down, so the true \(O(h^p)\) truncation error stays visible — a direct picture of why every finite-difference formula has an optimal step size.
Show step-by-step derivation (Taylor expansion)
Cite this tool
Kapita, S. (2026). Finite Difference Generator. Math Tools. https://doi.org/10.5281/zenodo.20981217
Kapita, Shelvean. "Finite Difference Generator." Math Tools, 2026, doi.org/10.5281/zenodo.20981217.
@online{kapita2026finitediff,
author = {Shelvean Kapita},
title = {{Finite Difference Generator}},
year = {2026},
organization = {Math Tools},
doi = {10.5281/zenodo.20981217},
url = {https://doi.org/10.5281/zenodo.20981217}
}