Code

Programmatic Numerical Approximation of a Derivative

To implement the numerical approximation of a derivative in code, we evaluate the finite difference ratio f(x+h)f(x)h\frac{f(x+h) - f(x)}{h} for progressively smaller values of hh. For example, consider the function f(x)=3x24xf(x) = 3x^2 - 4x. We can define this function in Python and calculate its numerical derivative at x=1x=1 as the perturbation hh shrinks exponentially:

import numpy as np def f(x): return 3 * x ** 2 - 4 * x for h in 10.0**np.arange(-1, -6, -1): print(f'h={h:.5f}, numerical limit={(f(1+h)-f(1))/h:.5f}')

As hh approaches 00, the printed output shows the numerical limit correctly converging to the exact analytical derivative value, f(1)=2f'(1) = 2.

0

1

Updated 2026-05-01

Contributors are:

Who are from:

Tags

D2L

Dive into Deep Learning @ D2L

Related