Given f, is there an automatic way to calculate fprime for Newton's method?
Question: The following was ported from the pseudo-code from the Wikipedia article on
[Newton's method](http://en.wikipedia.org/wiki/Newton%27s_method):

    
    
    #! /usr/bin/env python3
    # https://en.wikipedia.org/wiki/Newton's_method
    import sys
    
    x0 = 1
    f = lambda x: x ** 2 - 2
    fprime = lambda x: 2 * x
    tolerance = 1e-10
    epsilon = sys.float_info.epsilon
    maxIterations = 20
    
    for i in range(maxIterations):
        denominator = fprime(x0)
        if abs(denominator) < epsilon:
            print('WARNING: Denominator is too small')
            break
        newtonX = x0 - f(x0) / denominator
        if abs(newtonX - x0) < tolerance:
            print('The root is', newtonX)
            break
        x0 = newtonX
    else:
        print('WARNING: Not able to find solution within the desired tolerance of', tolerance)
        print('The last computed approximate root was', newtonX)
    

**Question**

Is there an automated way to calculate some form of `fprime` given some form
of `f` in Python 3.x?


Answer: A common way of approximating the derivative of `f` at `x` is using a finite
difference:

    
    
    f'(x) = (f(x+h) - f(x))/h                   Forward difference
    f'(x) = (f(x+h) - f(x-h))/2h                Symmetric
    

The best choice of `h` depends on `x` and `f`: mathematically the difference
approaches the derivative as h tends to 0, but the method suffers from loss of
accuracy due to catastrophic cancellation if `h` is too small. Also x+h should
be distinct from x. Something like `h = x*1e-15` might be appropriate for your
application. See also [implementing the derivative in
C/C++](http://stackoverflow.com/questions/1559695/implementing-the-derivative-
in-c-c).

You can avoid approximating f' by using the [secant
method](http://en.wikipedia.org/wiki/Secant_method). It doesn't converge as
fast as Newton's, but it's computationally cheaper and you avoid the problem
of having to calculate the derivative.

