[SciPy-User] Is this weird answer a bug in optimize.newton?

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Feb 24 18:36:08 EST 2016


On 24 February 2016 at 19:43, Thomas Baruchel <baruchel at gmx.com> wrote:
> with my students, this afternoon, I encountered the following annoying
> behaviour of Scipy:
>
>>>> from scipy import optimize
>>>> optimize.newton(lambda x: x**3-x+1, 0)
>
> 0.999999989980082
>
> This is very weird, because the equation is rather simple with simple
> coefficients,
> and the initial point is simple also.

Somehow the initial guess is problematic:

In [1]: from scipy import optimize

In [2]: optimize.newton(lambda x: x**3 - x + 1, 0)
Out[2]: 0.999999989980082

In [3]: optimize.newton(lambda x: x**3 - x + 1, 0.1)
Out[3]: -1.324717957244745

In [4]: optimize.newton(lambda x: x**3 - x + 1, -0.1)
Out[4]: -1.3247179572447458

These last two are the correct answer:

In [6]: from numpy import roots

In [7]: roots([1, 0, -1, 1])
Out[7]:
array([-1.32471796+0.j        ,  0.66235898+0.56227951j,
        0.66235898-0.56227951j])

The only thing that jumps out to me is that you've picked an initial
guess at which the function has a zero second derivative. I'm not sure
why that would cause the method to fail but Newton solvers are
sensitive to various singularities in the input function.

--
Oscar



More information about the SciPy-User mailing list