我如何调整这个 Newton-Raphson for 循环以包含我的初始猜测值的范围,以便它打印每个值的迭代次数?

How can I adjust this Newton-Raphson for loop to include a range of values for my initial guess so that it prints the number of iterations for each?

我目前有这个,我试图通过定义一个 'np.arrange' 来替换 x 来执行此操作,但不断收到一个错误,提示“只能将整数标量数组转换为标量索引”,我认为我可能需要重新定义我的函数,但我希望有一种简单的方法可以在数组中插入,从而为我提供一系列的根值和迭代次数。

import math

#Define Newton-Raphson method as a function using a for loop
def nraphson(fn, dfn, x, tol, maxiter):
    for i in range(maxiter):
        xnew = x - fn(x)/dfn(x)
        if abs(xnew - x) < tol: break
        x = xnew
    return xnew, i

#Define f(x) and f'(x) to be used in Newton-Raphson function
y = lambda x: math.exp(x) - x**2
dy = lambda x: math.exp(x) - 2*x

#Run the Newton-Raphson method with initial x as -1 from estimate
x, n = nraphson(y, dy, -1, 0.0001, 100)
#Printing text allows us to see the value determined for the root
print("the root is %f at %d iterations." % (x,n))

由于这道题输入数组的每个单元格的迭代次数可能不同,那么最好通过嵌套迭代来解决这个问题。 (嵌套 for 循环),我的意思是循环遍历 nraphson 函数上的每个输入单元格。