Newton's method in 1D

In [28]:
import numpy as np
import matplotlib.pyplot as pt

Here's a function:

In [29]:
a = 17.09
b = 9.79
c = 0.6317
d = 0.9324
e = 0.4565

def f(x):
    return a*x**4 + b*x**3 + c*x**2 + d*x + e

def df(x):
    return 4*a*x**3 + 3*b*x**2 + 2*c*x + d

def d2f(x):
    return 3*4*a*x**2 + 2*3*b*x + 2*c

Let's plot the thing:

In [30]:
xmesh = np.linspace(-1, 0.5
                    , 100)
pt.ylim([-1, 3])
pt.plot(xmesh, f(xmesh))
Out[30]:
[<matplotlib.lines.Line2D at 0x7f3774d78cf8>]

Let's fix an initial guess:

In [31]:
x = 0.3
In [32]:
dfx = df(x)
d2fx = d2f(x)

# carry out the Newton step
xnew = x - dfx / d2fx

# plot approximate function
pt.plot(xmesh, f(xmesh))
pt.plot(xmesh, f(x) + dfx*(xmesh-x) + d2fx*(xmesh-x)**2/2)
pt.plot(x, f(x), "o", color="red")
pt.plot(xnew, f(xnew), "o", color="green")
pt.ylim([-1, 3])

# update
x = xnew
print(x)
0.14466962664624314

  • What convergence order does this method achieve?
In [33]:
# Quadratic, because it's just like doing 'equation-solving Newton' on f'.