使用牛顿拉夫森法求根

时间:2019-10-06 01:30:12

标签: python newtons-method

我正在尝试使用newton raphson方法查找数字的第四根,并且在代码中遇到了这个奇怪的错误。谁能帮我解决这个问题吗?

谢谢

while abs(g**4-k)>=epsilon:
builtins.OverflowError: (34, 'Result too large')


def root():
epsilon = 0.01
k = 100
g = k/2.0
count = 1
while abs(g**4-k)>=epsilon:
    g = g-(((g**4)-k/(4*g**3)))
    count += 1
print("Approximate fourth root of", k, 'is about', g)

1 个答案:

答案 0 :(得分:0)

while条件后的下一行中的括号有误(k后面的右括号)。参见下面的代码:

def root():
    epsilon = 0.01
    k = 100
    g = k/2.0
    count = 1
    while abs(g**4-k) >= epsilon:
        g = g-(((g**4)-k)/(4*g**3))
        count += 1
    print("Approximate fourth root of", k, 'is about', g)