Search code examples
pythonoptimizationscipynewtons-method

Scipy optimize newton secant method


Scipy optimize.newton has initial step size hard-coded as 1e-4. What's the best way to utilize this function with a different step size (ideally, specified as a parameter)?

# https://github.com/scipy/scipy/blob/v0.19.0/scipy/optimize/zeros.py#L160

else:
    # Secant method
    p0 = x0
    if x0 >= 0:
        p1 = x0*(1 + 1e-4) + 1e-4

Solution

  • The initial step size is not 1e-4, it is abs(x0)*1e-4 + 1e-4. For example, x0 = 1000 will result in the initial step 0.1001.

    If the goal is to have certain initial step size h, that can be achieved with a linear change of variable, x = x0 + 1e4*h*t where t is the new variable. In terms of t, the starting point is 0. So Newton's method will make a step of size 1e-4, which in terms of x translates to h.

    Example:

    root = newton(lambda t: func(x0 + 1e4*h*t), 0) * 1e4 * h  + x0
    

    where func is the original function. This returns the root in terms of original variable x.