-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrentLineSearch.m
171 lines (145 loc) · 3.25 KB
/
brentLineSearch.m
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
function varargout = brentLineSearch(fun, ax, bx, cx, tol)
%BRENTLINESEARCH Brent algorithm for line search
%
% output = brentLineSearch(FUN, AX, BX, CX, TOL)
%
% Example
% brentLineSearch
%
% See also
%
%
% ------
% Author: David Legland
% e-mail: david.legland@grignon.inra.fr
% Created: 2010-08-10, using Matlab 7.9.0.529 (R2009b)
% Copyright 2010 INRA - Cepia Software Platform.
%% constants
% max number of iterations
MAX_ITER = 100;
% to protect against divisions by zero
ZEPS = 1e-10;
% golden ratio, approx equal to 0.311966
CGOLD = (3-sqrt(5))/2;
%% Initialisations
% compute bounds in ascending order
if ax < cx
a = ax;
b = cx;
else
a = cx;
b = ax;
end
% move distance from last point
e = 0;
% initialisations to approximated value
x = bx;
v = bx;
w = bx;
% first function evaluations
fx = fun(x);
fv = fx;
fw = fx;
% main loop
done = false;
for iter = 1:MAX_ITER
xm = (a + b)*.5;
tol1 = tol*abs(x)+ZEPS;
tol2 = 2*tol1;
% test if convergence criterion is met
if abs(x-xm) <= (tol2 - (b-a)*.5)
done = true;
xmin = x;
res = fx;
break;
end
% construct a trial parabolic fit
if abs(e) > tol1
r = (x - w) * (fx - fv);
q = (x - v) * (fx - fw);
p = (x - v) * q - (x - w) * r;
q = 2 * (q - r);
if q > 0
p = -p;
end
q = abs(q);
etemp = e;
e = d;
% Test the acceptability of the parabolic fit
if abs(p) <= abs(q*etemp*.5) || p <= q*(a-x) || p>= q*(b-x)
% choose the golden section into the larger of the two segments
if x >= xm
e = a - x;
else
e = b - x;
end
d = CGOLD * e;
else
% take the parabolic step
d = p / q;
u = x + d;
if u-a < tol2 || b-u < tol2
d = tol1 * sign(xm-x);
end
end
else
% choose the golden section into the larger of the two segments
if x >= xm
e = a - x;
else
e = b - x;
end
d = CGOLD * e;
end
% compute new evaluation point
if abs(d) >= tol1
u = x + d;
else
u = x + tol1*sign(d);
end
% evaluate function at new value
fu = fun(u);
% Decide what to do with function evaluation
if fu <= fx
% update search itnerval
if u >= x
a = x;
else
b = x;
end
v = w;
w = x;
x = u;
fv = fw;
fw = fx;
fx = fu;
else
if u < x
a = u;
else
b = u;
end
if fu <= fw || w==x
v = w;
w = u;
fv = fw;
fw = fu;
elseif fu <= fv || v == x || v == w
v = u;
fv = fu;
end
end
end % iteration loop
% process error if no convergence
if ~done
warning('BrentLineSearch:MaxIterReached', ...
'Too many iteration in Brent line search');
xmin = x;
res = fx;
end
% process output arguments
if nargout<=1
varargout{1} = xmin;
else
varargout = {xmin, res};
end