3

I wanted to solve a function being close to 0.

I tried using the newton function in the Scipy package, but the tolerance seems to apply to the input and not the function of the input:

from scipy.optimise import newton
fn = lambda x: x*x-60
res = newton(fn, 0, tol=0.1, maxiter=10000)
print(res)
print(fn(res))

res is close to 0, and fn(res) is about -60. It looks like newton() stopped because it found two x values which are bounding the solution and within the tolerance.

Is that correct that the tolerance is on x and not fn(x)?

That seems very counterintuitive to me.

BlueTrin
  • 9,610
  • 12
  • 49
  • 78

1 Answers1

2

Yes, the tolerance refers to the function argument x of the function f(x). See here: zeros.py The code checks if the step from p0 to p is small enough (they call the function argument p and not x in their code), and if yes, the algorithm stops.

You're in fact using the secant method and not Newton's method because you did not provide the derivative of your function. Use newton(func=lambda x: x*x-60, fprime=lambda x:2*x, x0=0, tol=0.1, maxiter=10000) instead.

When you run the above code you get an error. That is because you're starting from a local minimum of your function where the tangent line is parallel to the x-axis. In each iteration Newton's method jumps to the position where the tangent line and the x-axis intersect. This is of course not possible if x-axis and tangent line are parallel. Try starting e.g. from x0=0.1 to get the correct output. The plot below shows your quadratic function and the tangent lines at x=0 and x=2. It easy to see that starting from x0=2 works because x-axis and tangent will intersect at some point.

enter image description here

Harry
  • 1,105
  • 8
  • 20