Mathematics

Numerical Derivatives Done Right

Mathcube
Intuition
5 min readDec 4, 2021

--

Photo by Joshua Sortino on Unsplash

Recently I have found this article on Medium giving a nice first introduction of how to to differentiation numerically with Python. But how can you do it with more accuracy and maybe even in higher dimensions? Texts on the more general case are hardly ever found on the web, although it’s actually quite easy. So I decided to go beyond the introductory level and write about how it is done in general. This is not just some academic exercise. No! The simple formulas at the introductory level almost never suffice to solve even simple practical problems. That’s what I will write about in the first section. In the second section, I will then show how to do better and in the last section I show sample Python code for solving the general case.

The easy cases and what’s problematic about them

The “easy cases” for first and second derivatives like in most tutorials are usual derived from geometric intuition. The introductory formulas read

for the first derivative 𝑓′ evaluated at 𝑥_𝑛 (Medium really sucks for math notation!). ℎ is the spacing between two grid points on the 𝑥-axis and 𝑓_𝑛 is shorthand for the value of the function 𝑓 evaluated at the point 𝑥_𝑛.

For the second derivative we are taught that it is approximately

--

--

Mathcube
Intuition

Blogging about math, physics, and programming.