scipy.optimize.newton fails for a function with integer coefficients (Trac #1214) #1741

Closed
scipy-gitbot opened this Issue Apr 25, 2013 · 7 comments

Projects

None yet

1 participant

@scipy-gitbot

Original ticket http://projects.scipy.org/scipy/ticket/1214 on 2010-06-29 by @WarrenWeckesser, assigned to unknown.

The following demonstrates an incorrect value returned by scipy.optimize.newton:

>>> from scipy.optimize import newton
>>> newton(lambda x: x**2 - 1, x0=2, fprime=lambda x: 2*x)
2

The solution should be 1. Changing any of the coefficients or the initial guess to a float results in the correct solution:

>>> newton(lambda x: x**2 - 1, x0=2.0, fprime=lambda x: 2*x)
1.0
>>> newton(lambda x: x**2 - 1, x0=2, fprime=lambda x: 2.0*x)
1.0
>>> newton(lambda x: x**2 - 1.0, x0=2, fprime=lambda x: 2*x)
1.0
@scipy-gitbot

Attachment added by @WarrenWeckesser on 2010-06-29: ticket_1214_patch.diff

@scipy-gitbot

@WarrenWeckesser wrote on 2010-06-29

The attached patch has a simple fix. Any reason not to apply it?

@scipy-gitbot

Title changed from scipy.optimize.newton fails for a function with integer coefficient to scipy.optimize.newton fails for a function with integer coefficients by @WarrenWeckesser on 2010-06-29

@scipy-gitbot

@charris wrote on 2010-06-29

Looks good to me, but float(x0) might be clearer than 1.0 * x0.

@scipy-gitbot

@WarrenWeckesser wrote on 2010-06-29

float(x0) was my first choice, but then I realized that newton actually works with complex numbers (at least when fprime is given; I didn't experiment with the secant method), so it seemed a shame to break that by using float. By multiplying by 1.0, an int becomes a float, but a complex number stays complex.

I'll add a comment about that before I check in the change.

@scipy-gitbot

@WarrenWeckesser wrote on 2010-06-29

Fixed in 3e5294d.

@scipy-gitbot

@WarrenWeckesser wrote on 2010-07-01

Replying to [comment:4 warren.weckesser]:

Fixed in 3e5294d.
That should be 965e600.

@tonysyu tonysyu pushed a commit to tonysyu/scipy that referenced this issue May 9, 2013
@pv pv BUG: special: replace wofz implementation with a license-compatible o…
…ne (#1741)
e80cb04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment