求 的解
# (x - 3)** 3 = 0
def f(x):
return (x - 3)**3
def fd(x):
return 3 * (x - 3)**2
def newtonMethod(n, assum):
time = n
x = assum
A = f(x)
B = fd(x)
if f(x) == 0.0:
return time, x
else:
Next = x - A / B
if A - f(Next) < 1e-6:
print('meet f(x) = 0, x = %s', str(Next))
else:
return newtonMethod(n + 1, Next)
newtonMethod(0, 4.0)
网友评论