import numpy as np
import matplotlib.pyplot as pt
Here's a function:
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:
xmesh = np.linspace(-1, 0.5
, 100)
pt.ylim([-1, 3])
pt.plot(xmesh, f(xmesh))
Let's fix an initial guess:
x = 0.3
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)
# Quadratic, because it's just like doing 'equation-solving Newton' on f'.