def fvals_sqrt(x):
"""
Return f(x) and f'(x) for applying Newton to find a square root.
"""
f = x**2 - 4.
fp = 2.*x
return f, fp
def solve(fvals_sqrt, x0, debug_solve = False):
"""
Solves the sqrt function, using newtons methon.
"""
iters = 0
f, fp = 0.
while f > 10**-14 | -f < 10**-14:
f, fp = fvals_sqrt(x0)
x0 = x0 - (f/fp)
iters = iters+1
print + " x = %22.15e in %i iterations " % (x0, iters)
return x0, iters
print "we're done"
我想要这个while循环一次f小于10^-14,但我不确定如何修改参数以使循环可迭代,有什么帮助吗?