-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolden_line_search.py
53 lines (40 loc) · 1.33 KB
/
golden_line_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import math
import matplotlib.pyplot as plt
def golden_section_search(f, a, b, tol):
alpha = 0.618 # golden ratio
varlambda = a + (1 - alpha) * (b - a) # base case
varmu = a + alpha * (b - a) # base case
a_values = [a]
b_values = [b]
varlambda_values = [varlambda]
varmu_values = [varmu]
while b - a > tol:
if f(varlambda) > f(varmu):
a = varlambda
varlambda = varmu
varmu = a + alpha * (b - a)
elif f(varlambda) <= f(varmu):
b = varmu
varmu = varlambda
varlambda = a + (1 - alpha) * (b - a)
a_values.append(a)
b_values.append(b)
varlambda_values.append(varlambda)
varmu_values.append(varmu)
return f((a + b) / 2), (a + b) / 2, a_values, b_values, varlambda_values, varmu_values
def f(x1):
return 6 * (math.e) ** (-2 * x1) + 2 * x1**2
a = -1
b = 3
tol = 0.002
min_value, min_x, a_values, b_values, varlambda_values, varmu_values = golden_section_search(f, a, b, tol)
fig, ax = plt.subplots()
iterations = range(len(a_values))
ax.plot(iterations, a_values, label='a')
ax.plot(iterations, b_values, label='b')
ax.plot(iterations, varlambda_values, label='varlambda')
ax.plot(iterations, varmu_values, label='varmu')
ax.set_xlabel('Iterations')
ax.set_ylabel('Values')
ax.legend()
plt.show()