I am trying to count the number of times that f(x)
is evaluated without having to change my code too much, it doesn't seem like it should be very difficult but I can't seem to figure it out.
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()
From here on as before and the count is given by
f.count
. Tested :)