function [LB, UB] = forwardback(xinit,dxinit,objfun)
%% Input
x0 = xinit; dx = dxinit;
flag = 0;
%% step1
f0 = objfun(x0);
%% step2
x1 = x0 + dx;
f1 = objfun(x1);
if f1 <= f0
%% step3
while dx<=1e8 && flag == 0
dx = 2*dx;
x2 = x1+dx;
f2 = objfun(x2);
if f1 <= f2
a = x0;
c = x1;
b = x2;
flag = 1;
else
%% step4
x0 = x1;
x1 = x2;
f0 = f1;
f1 = f2;
end
end
else
%% step5
while dx<=1e8 && flag == 0
dx = 2*dx;
x2 = x0-dx;
f2 = objfun(x2);
if f0 <= f2
a = x2;
c = x0;
b = x1;
flag = 1;
else
%% step6
x1 = x0;
x0 = x2;
f1 = f0;
f0 = f2;
end
end
end
%% Output
if flag == 0
% fprintf('Fail!\n')
LB = xinit;
UB = xinit+dx;
else
% fprintf('Success!\n a_left=%3.1f, c_middle=%3.1f, b_right=%3.1f.\n',a,c,b);
LB = a;
UB = b;
end
end