所以它和python的练习我完全被卡住了!您在 [a,b] 中有一个随机函数,您已经知道 a 是负数,b 是正数,并且它只有一个根。真正的根是: -0.94564927392359,你必须做一个 def 来找到最接近真正根的根(或零),且差异eps最小。eps是 1e-8 或 1e-6。请注意,我们不知道真正的根,之前是一个例子来了解我们正在寻找的数字是关于什么的。我们也得到了上述内容:
import math
def fnc(x):
""" This the function in which root we are looking for """
global a, b, eps
if not hasattr(fnc, "counter"):
fnc.counter = 0
fnc.maxtimes = (int)(0.1+math.ceil(math.log((b-a)/eps, 2.0)+2))
if fnc.counter<fnc.maxtimes:
fnc.counter += 1
return x*x*x-x-0.1
else:
return 0.0 ##
我们必须从这个开始:
def root(f, a, b, eps):
(对不起我的英语不好 )