我试图编写一个二进制搜索函数以在间隔fun
中找到函数[,]
的根:
这里是我所拥有的,虽然很近但是缺少标记:
def binarySearchIter(fun, start, end, eps=1e-10):
'''
fun: funtion to fund the root
start, end: the starting and ending of the interval
eps: the machine-precision, should be a very small number like 1e-10
return the root of fun between the interval [start, end]
'''
root = (start + end)/2
print(root)
answer=fun(root)
if abs(answer) <= eps:
print(root)
return root
elif answer - eps > 0:
binarySearchIter(fun, start, root, eps=1e-10)
else:
binarySearchIter(fun, root, end, eps=1e-10)
这是我要测试的功能:
def f(x):
return x ** 2 - 2
当我运行:binarySearchIter(f, -3, 0, eps = 1e-10)
时,我期望得到的答案是:-1.4142135623842478
,但根收敛到-3直到超时。
当我运行binarySearchIter(f, 0, 3, eps = 1e-10)
时,我会得到1.4142135623842478
的正确答案。
我显然缺少使函数中断的东西,具体取决于它是(-3,0)还是(3,0)。
谢谢您的帮助。
答案 0 :(得分:5)
您看到的是您的函数仅对递增函数起作用,对于x**2 - 2
和0
之间的3
来说是正确的,但对递减函数不起作用,即在-3
和0
之间的功能为true。
有几种方法可以修复您的功能。一种方法是交换start
和end
的值。换句话说,将fun(start) > fun(end)
行更改为三行
root = (start + end)/2
这确实减慢了您的例程,因此有更好的方法来执行例程。特别是,使用迭代而不是递归。与迭代相比,Python的递归速度相当慢。
但是,您的功能不够强大。您应该首先检查if fun(start) > fun(end):
start, end = end, start
root = (start + end)/2
和fun(start)
的符号是否不同。然后,您的例程将继续重新定义fun(end)
和start
,以使它们的图像继续具有相反的符号。如果符号相同,则在该间隔中您的功能可能没有根,并且您的例程肯定没有好的方法来确定继续搜索间隔的哪一半。一种方法是在我已经插入的行之前添加这两行:
end