【发布时间】:2013-06-15 21:05:28
【问题描述】:
我无法让这部分程序进入 while 循环。插入poly、x_0 和epsilon 的值后,程序使用函数evaluate_poly() 计算ans 的值,该函数正常工作,在我的具体实例中给出了答案-13.2119。我使用的 epsilon 值为0.0001。由于abs(ans) 实际上大于epsilon,为什么它会跳过循环?
我在 x_01 = 0 行的正下方放置了一个 print ans 语句,以确保它在 while 循环之前正确计算,并且还有一个 print epsilon 语句以确保它正确地接受了我的 epsilon 值(其中确实如此)。
def compute_root(poly, x_0, epsilon):
"""uses newton's method to find a root of a polynomial function"""
ans = evaluate_poly(poly, x_0)
x_01 = 0
while abs(ans) > epsilon:
Dpoly = compute_deriv(poly)
Fprime = evaluate_poly(Dpoly, x_01)
return ans
x_01 = x_0 - (ans/Fprime)
print x_01
return x_01
print ans
【问题讨论】:
-
>>> abs(-13.2119) > 0.0001返回True
标签: python while-loop