Writing the Derivative Function
For all the fear whipped up about derivatives in calculus courses, the function for calculating a derivative numerically is surprisingly easy.
In a Jupyter notebook, we'll define a function, f(x), to be the parabola y = x2:
def f(x): Â Â Â Â return x**2
Now we can write a function to calculate the derivative at any point (x, f(x)) using the classic formula:
The numerator is the rise and the denominator is the run. Δ x means the change in x, and we're going to make that a really small decimal by dividing 1 by a million:
def f(x):     return x**2 def derivative(f,x):     """     Returns the value of the derivative of     the function at a given x-value.     """     delta_x = 1/1000000  ...