from math import * def f(x): return sin(x) def df_approx(f, x, h): return (f(x+h)-f(x))/h #return (f(x+h)-f(x-h))/(2*h) a = 1/2 for n in range(1, 19): h = 10**(-n) df_app = df_approx(f, a, h) df = cos(a) print("h: %e, df_approx: %13.10f, abs_err: %e" % (h, df_app, abs(df - df_app)))