Search code examples
pythonscipynewtons-methodscipy-optimize

SciPy's Newton function does not find the intersection point


I am trying to understand why the code below does not return -1.17 but instead returns -6.67e-09. What does this number actually tell me?

If I alter the estimation from 0 to -1 it does correctly compute -1.17. However, if I would have to do this for 100+ different functions, I would have to write a while-loop for each making the calculation process incredibly slow.

Is this simply the way this is calculated or am I missing a specific parameter for this case?

from scipy.optimize import newton


def f(x):
    return x**2-3


def g(x):
    return x**3

def insection():
    def difference(x):
        return g(x) - f(x)
    insection_point_value = newton(difference, 0)

    return insection_point_value 

print(insection())
Returns: -6.665555511432543e-09
Has to be: -1.1745594102929802

Plot


Solution

  • The Newton-Raphson method(NR) is highly sensitive to the initial value you provide.

    Check the graph of the difference function: graph
    The derivative of the function at x = 0 is 0. NR being an iterative method, cannot progress from the initial point x0 = 0 using a zero derivative. That's why, it continues to stay there, instead of converging towards the intended point. Try x0 = -0.1 and it works, or anything less than that.
    Any x > 0 will continue to fail, because there is another zero derivative at x = 0.667, and the iterative method will slide into the valley, to use layman language.

    The weird decimal value that you get (instead of 0)is an artifact of floating-point math, discretized values for the function, or a combination of both.