"""The Newton-Raphson method"""defnewton(f,df,x,tol=0.001,maxiter=100):"""
f:the function to solve
df:the derivative function of f
x:initial guess value of x
tol:the precision of the solution
maxiter:max number of interations
return:the x-axis value of the root,
number of interations used
"""
n=1while n <= maxiter:
x1 = x-f(x)/df(x)ifabs(x1-x)< tol:return x1,n
else:
x=x1
n=n+1returnNone,n
"""for example"""
y =lambda x:x**3+2.0*x**2-5
dy =lambda x:3*x**2+4*x
root,interations = newton(y,dy,5.0,0.00001,100)print("root is:",root)print("interations is",interations)