This is the same function as in a previous example, but we have shifted it so that the root lies at a large distance from zero.
Consider the function $$ f(x) = \exp(x-x_0) - \frac{3}{2} - \arctan(x-x_0) $$ where $x_0$ is some large number, e.g., $x_0 = 10^{10}$. The root lies around $x_0$ and due to precision of floating point numbers, we cannot compute it to good absolute tolerance, but only to relative tolerance.
%config InlineBackend.figure_format = 'svg'
from numpy import linspace,abs
from matplotlib.pyplot import figure,subplot,plot,grid,title
Define function and derivative.
x0 = 1e10
# Define the function
F = lambda x: exp(x-x0)-3.0/2.0-atan(x-x0)
# Compute its derivative
DF = lambda x: exp(x-x0) - 1.0/(1 + (x-x0)**2)
Now we implement the Newton method. We have to specify the maximum number of iterations, an initial guess and a tolerance to decide when to stop.
M = 50 # maximum iterations
eps = 1e-14 # relative tolerance on root
x = x0 + 0.5 # initial guess
f = F(x)
for i in range(M):
df = DF(x)
dx = -f/df
x = x + dx
e = abs(dx)
f = F(x)
print("%6d %22.14e %22.14e %22.14e" % (i,x,e,abs(f)))
if e < eps * abs(x):
break
0 1.00000000008711e+10 3.71059792151655e-01 1.72847126992936e-01 1 1.00000000007761e+10 9.49264320087584e-02 1.30346281936581e-02 2 1.00000000007677e+10 8.41497025309343e-03 9.77825039385483e-05 3 1.00000000007677e+10 6.40915294389154e-05 1.15114296217467e-06
Root converges in relative sense to machine zero in few iterations.
x = x0 + 0.5 # initial guess
f = F(x)
for i in range(M):
df = DF(x)
dx = -f/df
x = x + dx
e = abs(dx)
f = F(x)
print("%6d %22.14e %22.14e %22.14e" % (i,x,e,abs(f)))
if e < eps:
break
0 1.00000000008711e+10 3.71059792151655e-01 1.72847126992936e-01 1 1.00000000007761e+10 9.49264320087584e-02 1.30346281936581e-02 2 1.00000000007677e+10 8.41497025309343e-03 9.77825039385483e-05 3 1.00000000007677e+10 6.40915294389154e-05 1.15114296217467e-06 4 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 5 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 6 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 7 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 8 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 9 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 10 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 11 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 12 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 13 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 14 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 15 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 16 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 17 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 18 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 19 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 20 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 21 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 22 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 23 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 24 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 25 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 26 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 27 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 28 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 29 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 30 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 31 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 32 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 33 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 34 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 35 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 36 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 37 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 38 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 39 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 40 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 41 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 42 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 43 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 44 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 45 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 46 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 47 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 48 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06 49 1.00000000007677e+10 7.54605114902618e-07 1.15114296217467e-06
There is no convergence. The root cannot be computed to good absolute accuracy, and consequently, we cannot make $f(x)$ sufficiently small. We could reduce it to about $10^{-6}$ which is also achieved using relative tolerance as stopping criterion.