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 while循環一次f一次小於10^-14,但我不知道如何修改參數,使循環迭代,任何幫助?while block does not expect expected
還有,你是初始化'F = 0.'循環 – wim 2013-05-02 00:58:26
之外其實,'樓FP = 0.'是一個問題語法錯誤是不是? – wim 2013-05-02 01:00:22
@wim - 我不知道語法錯誤,但至少有一個TypeError ... – mgilson 2013-05-02 01:24:46