为什么这个 newton-raphson 方法用日志查找返回错误

Why is this newton-rapshon method of finding the returning an error with log

如上所述,我创建了一个 newton-raphson 方法来计算给定数字的平方根

def newton(f, fprime, eps):

    x0 = 10
    while True:
        fx0 = f(x0)
        if abs(fx0) < eps:
            return x0
        fpx0 = fprime(x0)
        if fpx0 == 0:
            return None
        x0 = x0 - fx0/fpx0

我知道通常你不应该使用 while True 循环,但在我的情况下它很好,我的问题是当 f(x)=logx 和 f'(x) = 1/x 时,我 运行 代码并得到一个数学错误,我假设要么记录负数要么除以 0。无论哪种方式,一些关于如何修复它的帮助都会很棒,因为我似乎无法找到为什么只记录有问题

尝试将初始 x0 猜测更改为更接近根的值。

例如,

x0 = 2 会给你答案。