← Lesson
Derivatives
Joeven
Run
Reset
Python loads on first run
def f(x): return x * x def forward_diff(f, x, h): return (f(x + h) - f(x)) / h def central_diff(f, x, h): return (f(x + h) - f(x - h)) / (2 * h) x = 3.0 analytic = 2 * x print("true slope at 3 =", analytic) for h in [1.0, 0.1, 0.01, 1e-4, 1e-6, 1e-12]: fd = forward_diff(f, x, h) cd = central_diff(f, x, h) print("h", h, "forward", round(fd, 6), "central", round(cd, 6))
Run to execute this in your browser. Nothing is sent to a server.