def goldSection eps global counter phi 618034 x1 phi x2 phi y1 x1 y2 x

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
def goldSection(a, b, eps, f):
global counter
phi = 1.618034
x1 = b - (b - a)/phi
x2 = a + (b - a)/phi
y1 = f(x1)
y2 = f(x2)
if y1 >= y2:
a = x1
else:
b = x2
if abs(b - a) < eps:
return (a + b)/2
else:
counter += 1
return goldSection(a, b, eps, f)