1

The scipy.optimize.newton documentation reads: "The Newton-Raphson method is used if the derivative fprime of func is provided, otherwise the secant method is used." The secant method requires two estimates of the zero of func, x0 and x1. But the examples provided show that optimize.newton works with the secant method when ONLY x0 is input. So I assume optimize.newton is somehow assigning x1 based off our input of x0, but I'm not sure and am curious to know what's going on. Any clarification would be immensely appreciated!

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214

1 Answers1

0

Short answer: a perturbed version of x0 is used.

It looks like the actual behavior should be documented better. The source code for the function starts at this line in the file zeros.py; the code relevant to your question is:

        # Secant method
        if x1 is not None:
            if x1 == x0:
                raise ValueError("x1 and x0 must be different")
            p1 = x1
        else:
            eps = 1e-4
            p1 = x0 * (1 + eps)
            p1 += (eps if p1 >= 0 else -eps)

p1 is the actual value that will be used in the subsequent code as the second point. So if x1 is None, p1 is set to a slightly modified version of x0.

(See this github issue for a discussion of that calculation.)

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214