""" Example from lecture 17/10. A class for representing the derivative of a function. This is useful for instance in Newton's method, which requires both the function f in f(x) = 0 and its derivative. """ class Derivative: def __init__(self, f, h=1E-5): self.f = f self.h = float(h) def __call__(self, x): f, h = self.f, self.h # make short forms return (f(x+h) - f(x))/h def f(x): return 4 * x**2 + 8 * x df = Derivative(f) print('f = 4 * x**2 + 8x, x= 2:') print(f'True derivative: {8 * 2 + 8:.3f}') print(f'"Automatic" derivative: {df(2):.3f}') """ Terminal> python derivative.py f = 4 * x**2 + 8x, x= 2: True derivative: 24.000 "Automatic" derivative: 24.000 """