When you cannot differentiate symbolically
Symbolic differentiation needs a formula. Numerical differentiation needs only the ability to evaluate the function, which makes it the only option when your "function" is a table of measurements, the output of a simulation, or code you cannot differentiate by hand.
It is also how this site's own test suite checks the symbolic engine: differentiate an expression with the rules, evaluate a finite difference of the original, and confirm they agree at several points.
The formulas
The forward difference is the definition with the limit stopped early:
\[f'(x) \approx \frac{f(x + h) - f(x)}{h}\]
The central difference straddles the point instead, and is far more accurate for the same \(h\):
\[f'(x) \approx \frac{f(x + h) - f(x - h)}{2h}\]
The forward difference has error proportional to \(h\); the central difference has error proportional to \(h^{2}\). Halving \(h\) halves the first error but quarters the second, which is why the central form is the default everywhere — including in the calculator above.
For the second derivative, three points are needed:
\[f''(x) \approx \frac{f(x + h) - 2f(x) + f(x - h)}{h^{2}}\]
Choosing the step size
The obvious move is to shrink \(h\) until the answer stops changing. It does not work, because two errors pull in opposite directions:
- Truncation error shrinks as \(h\) shrinks — the difference quotient gets closer to the true limit.
- Rounding error grows as \(h\) shrinks — you are subtracting two nearly equal numbers, and the leading digits cancel, leaving noise.
The sum has a minimum. For double-precision arithmetic and a central difference, it sits near \(h \approx 10^{-5}\) to \(10^{-6}\); this calculator uses \(10^{-4}\), which is comfortably inside the useful range for the functions people type. Push \(h\) down to \(10^{-12}\) in your own code and the answer will get worse, not better.
How close does it get?
The calculator shows the numerical estimate against the exact symbolic value and the gap between them. For a well-behaved function that gap is usually smaller than \(10^{-8}\) — close enough that the disagreement is invisible at any sensible number of decimal places.
Try it somewhere awkward to see it fail honestly: abs(x) at 0 gives a numerical answer of about 0, averaging the slopes of \(-1\) and \(+1\) from either side, even though no derivative exists there. A finite difference cannot detect a corner — it only ever sees two points.
Questions
What is numerical differentiation?
Estimating a derivative from function values alone, using a finite difference such as \(\frac{f(x+h) - f(x-h)}{2h}\), rather than from a formula.
Why use a central difference?
Its error shrinks like \(h^{2}\) rather than \(h\), so for the same step size it is far more accurate than a forward difference.
What step size should I use?
Around \(10^{-5}\) for a central difference in double precision. Smaller is not better — below that, rounding error from subtracting nearly equal numbers dominates.
Is the numerical answer as good as the exact one?
No. Where a formula exists, differentiate symbolically — it is exact. Numerical differentiation is for when you only have values.
Can it detect a corner or a discontinuity?
No. A finite difference sees two points and reports the slope between them, so at \(|x|\) near zero it returns roughly 0 even though the derivative does not exist.