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

David Mikolas david.mikolas1 at gmail.com
Wed Feb 24 19:21:42 EST 2016


First of all it sounds like your students are receiving a very good
educational experience!

typing help(optimize.newton) returns the following:

   fprime : function, optional
        The derivative of the function when available and convenient. If it
        is None (default), then the secant method is used.

A look in https://en.wikipedia.org/wiki/Secant_method#Convergence returns
the following:

"If the initial values are not close enough to the root, then there is no
guarantee that the secant method converges."

So it looks like the behavior of scipy.optimize.newton in your example is
neither unexpected nor a bug.



On Thu, Feb 25, 2016 at 7:36 AM, Oscar Benjamin <oscar.j.benjamin at gmail.com>
wrote:

> 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
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> https://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20160225/45afbee4/attachment.html>


More information about the SciPy-User mailing list