1

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()
4

2 に答える 2

0
class F:
    count = 0
    def __call__(self, x):
        self.count += 1
        return 12*x**5-45*x**4+40*x**3+5

f = F()

ここから先は前と同じで、カウントは で与えられf.countます。テスト済み:)

>>> f = F()
>>> f(1)
12
>>> f(2)
-11
>>> f.count
2
>>> f(2)
-11
>>> f.count
3
于 2015-10-23T23:46:52.963 に答える