#Python代码以实例展示求解方程的根。
import math
def f(x):
return math.pow(x,3)-9*x+1
def fd(x):
return 3*math.pow(x,2)-9
def newton_method(n,x):
times = n
next_x = 0
y= f(x)
yd = fd(x)
print('y= ' , y, ',yd = ' , yd , ',times = ' ,times)
next_x = x - y/yd
print('next:',next_x)
if abs(y - f(next_x)) < 1e-6 :
print("next:",next_x)
else:
next_x=x-y/yd
return newton_method(n+1,next_x)
newton_method(0,4.1)