【发布时间】:2015-10-23 23:26:58
【问题描述】:
我正在尝试计算 f(x) 被评估的次数,而不必过多地更改我的代码,看起来这应该不是很困难,但我似乎无法弄清楚。
def f (x):
f = 12*x**5-45*x**4+40*x**3+5
return f
def bounding():
d=.1
x=6
n=0
while(n<50):
Lb=x-d
n+=1
Ub=x+d
if f(Lb)>=f(x) and f(Ub)<=f(x):
x=x+d
elif f(Lb)<=f(x) and f(Ub)>=f(x):
x=x-d
elif f(Lb)>=f(x) and f(Ub)>=f(x):
print("Lower bound:",Lb,"Upperbound:",Ub)
break
print (n)
bounding()
【问题讨论】:
-
我不确定你到底在问什么,但你的代码中有很多多余的东西,比如整个
bounding()函数。 -
当
def f(x)被计算时,函数被创建。这只会在您的代码中发生一次。你是指函数f被调用和执行的次数吗?
标签: python recursion numbers counting evaluation-strategy