用C++ 实现的一维搜索算法——黄金分割法
参考
【1】https://www.bilibili.com/video/BV1e64y1Y7Sr/?p=21&spm_id_from=pageDriver&vd_source=615f6fbe48340dd5ecd1f767a0bed249 3:05
【2】https://blog.csdn.net/qq_33414271/article/details/99686934
【3】https://blog.csdn.net/jirryzhang/article/details/53391379?locationNum=1&fps=1
计算 min f(x) = 2x^2-x-1
初始区间 [a,b]=[1,1], ε<=0.16
代码如下:
const float rho6 = 0.5f * sqrt(5.0f) - 0.5f;
const float rho3 = 1.0f - rho6;
float fun(float x)
{
return 2 * x * x - x - 1.0f;
}
float Fibonacci()
{
float a = -1.0f;
float b = 1.0f;
//λ
float Lambda = a + rho3 * (b - a);
//μ
float Mu = a + rho6 * (b - a);
//f(λ)
float F_L = fun(Lambda);
//f(μ)
float F_M = fun(Mu);
while ((b - a) > 0.16f)
{
if (F_L > F_M)
{
a = Lambda;
Lambda = Mu;
Mu = a + rho6 * (b - a);
F_L = F_M;
F_M = fun(Mu);
}
else
{
b = Mu;
Mu = Lambda;
Lambda = a + rho3 * (b - a);
F_M = F_L;
F_L = fun(Lambda);
}
}
return 0.5f * (a + b);
}