Approach from the secant¶
Numerical differentiation approximates the continuous derivative of a function using discrete function evaluations. Geometrically, the true tangent slope is the limiting slope of secant lines connecting to as the step size approaches zero:
As the step size contracts toward zero, the secant line rotates continuously into the true analytical tangent line.
What issues arise in finite precision?
import numpy as np
try:
import executable_engineering as exe
except ImportError:
%pip install -q executable_engineering
import executable_engineering as exe
def f_geom(x):
return np.sin(x) + 0.1 * x**2
fig_secant = exe.plot_secant_tangent_limit(
f_geom, x0=1.5, h_values=(1.2, 0.6, 0.3, 0.1), x_range=(0.2, 3.2),
title="Secant to Tangent Convergence as Step Size h → 0"
)
fig_secant.show()