-
Notifications
You must be signed in to change notification settings - Fork 0
/
golden_section.R
78 lines (67 loc) · 2.4 KB
/
golden_section.R
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
##### Implementing the golden section search method
##### a modification of the bisection method with the golden ratio
##### By Eric Cai - The Chemical Statistician
golden.section.search = function(f, lower.bound, upper.bound, tolerance)
{
golden.ratio = 0.618
### Use the golden ratio to set the initial test points
x1 = upper.bound - golden.ratio*(upper.bound - lower.bound)
x2 = lower.bound + golden.ratio*(upper.bound - lower.bound)
### Evaluate the function at the test points
f1 = f(x1)
f2 = f(x2)
iteration = 0
while (abs(upper.bound - lower.bound) > tolerance)
{
iteration = iteration + 1
cat('', '\n')
cat('Iteration #', iteration, '\n')
cat('f1 =', f1, '\n')
cat('f2 =', f2, '\n')
if (f2 > f1)
# then the minimum is to the left of x2
# let x2 be the new upper bound
# let x1 be the new upper test point
{
cat('f2 > f1', '\n')
### Set the new upper bound
upper.bound = x2
cat('New Upper Bound =', upper.bound, '\n')
cat('New Lower Bound =', lower.bound, '\n')
### Set the new upper test point
### Use the special result of the golden ratio
x2 = x1
cat('New Upper Test Point = ', x2, '\n')
f2 = f1
### Set the new lower test point
x1 = upper.bound - golden.ratio*(upper.bound - lower.bound)
cat('New Lower Test Point = ', x1, '\n')
f1 = f(x1)
}
else
{
cat('f2 < f1', '\n')
# the minimum is to the right of x1
# let x1 be the new lower bound
# let x2 be the new lower test point
### Set the new lower bound
lower.bound = x1
cat('New Upper Bound =', upper.bound, '\n')
cat('New Lower Bound =', lower.bound, '\n')
### Set the new lower test point
x1 = x2
cat('New Lower Test Point = ', x1, '\n')
f1 = f2
### Set the new upper test point
x2 = lower.bound + golden.ratio*(upper.bound - lower.bound)
cat('New Upper Test Point = ', x2, '\n')
f2 = f(x2)
}
}
### Use the mid-point of the final interval as the estimate of the optimzer
cat('', '\n')
cat('Final Lower Bound =', lower.bound, '\n')
cat('Final Upper Bound =', upper.bound, '\n')
estimated.minimizer = (lower.bound + upper.bound)/2
cat('Estimated Minimizer =', estimated.minimizer, '\n')
}