黄金分割法也叫0.618法,是一种基于区间收缩的极小值搜索算法。
比如以
[
a
,
b
]
[a,b]
[a,b] 为区间,产生两个内点
x
1
=
a
+
0.382
∗
(
b
−
a
)
x
2
=
a
+
0.618
∗
(
b
−
a
)
x_1 = a + 0.382*(b-a)\\ x_2 = a + 0.618*(b-a)
x1=a+0.382∗(b−a)x2=a+0.618∗(b−a)
然后根据
f
(
x
1
)
,
f
(
x
2
)
f(x_1),f(x_2)
f(x1),f(x2) 的大小进行区间更新:
- 如果 f ( x 1 ) < f ( x 2 ) f(x_1)<f(x_2) f(x1)<f(x2),区间变为 [ x 1 , b ] [x_1,b] [x1,b]
- 如果 f ( x 1 ) > f ( x 2 ) f(x_1)>f(x_2) f(x1)>f(x2),区间变为 [ a , x 2 ] [a,x_2] [a,x2]
matlab 实现
% 一维极值搜索
function [x,minf] = minHJ(f,a,b,eps)
format long;
if nargin == 3
eps = 1.0e-6;
end
l = a + 0.382*(b-a);
u = a + 0.618*(b-a);
k = 1;
tol = b-a;
while tol > eps && k < 100000
fl = subs(f, symvar(f), l);
fu = subs(f, symvar(f), u);
if fl > fu
a = l;
l = u;
u = a + 0.618*(b - a);
else
b = u;
u = l;
l = a + 0.382*(b - a);
end
k = k + 1;
tol = abs(b - a);
end
if k == 100000
disp('Can find the solution');
x = NaN;
minf = Nan;
return;
end
x = (a+b)/2;
minf = subs(f, symvar(f),x);
format short;