function FibnacciSearch()
%% input
a = 1.5; b = 6.3;
eps = 1e-4;
%% compute iteration number n
n = 1;
F0 = 1; F1 = 1; F = 0;
while F < (b-a)/eps
n = n+1;
F = F0 + F1;
F0 = F1;
F1 = F;
end
%% step4
k = 1;
fprintf('%2s \t %1.6s \t %1.6s \t %1.6s \t %1.6s \n','k','a_k','b_k','f(x_1)','f(x_2)')
% fprintf('%2d \t %1.6f \t %1.6f \t %1.6f \t %1.6f \n',k,a,b,f1,f2)
x1 = a+fibonacci(n-2)/fibonacci(n)*(b-a);
x2 = a+fibonacci(n-1)/fibonacci(n)*(b-a);
f1 = objfun(x1);
f2 = objfun(x2);
%% step 5 and 6
while k<n-2
fprintf('%2d \t %1.6f \t %1.6f \t %1.6f \t %1.6f \n',k,a,b,f1,f2)
if f1 < f2
b = x2;
x2 = x1;
f2 = f1;
x1 = a+fibonacci(n-k-2)/fibonacci(n-k)*(b-a);
f1 = objfun(x1);
else
a = x1;
x1 = x2;
f1 = f2;
x2 = a+fibonacci(n-k-1)/fibonacci(n-k)*(b-a);
f2 = objfun(x2);
end
k = k+1;
end
%% step7
if f1 < f2
b = x2;
x2 = x1;
f2 = f1;
else
a = x1;
end
%% step8
x1 = x2-0.1*(b-a);
f1 = objfun(x1);
if f1 < f2
x = (a+x2)/2;
elseif f1 == f2
x = (x1+x2)/2;
else
x = (x1+b)/2;
end
%% output
fprintf('最优点x = %1.6f,最优值为f(x)=%3.6f\n',x,objfun(x))
end
function y = objfun(x)
y = x^4-4*x^3-6*x^2-16*x+4;
end