function[xmin, fmin] = golden2(xlow, xhigh, f)

maxit = 100;
es = 0.01;

R = (5^0.5 - 1)/2;
xl = xlow;
xu = xhigh;
iter = 1;

d = R * (xu - xl);
x1 = xl + d;
x2 = xu - d;

f1 = f(x1);
f2 = f(x2);

if f1 < f2
    xopt = x1;
    f = f1;
else
    xopt = x2;
    f = f2;
end

d = R*d;

if xopt ~= 0
    ea = (1 - R)*abs((xu - xl)/xopt)*100;
end

while (ea <= es || iter >= maxit)

    if f1 < f2
        xl = x2;
        x2 = x1;
        x1 = xl + d;
        f2 = f1;
        f1 = f(x1);
    else
        xu = x1;
        x1 = x2;
        x2 = xu - d;
        f1 = f2;
        f2 = f(x2);
    end

iter = iter + 1;

if f1 < f2
    xopt = x1;
    f = f1;
else
    xopt = x2;
    f = f2;
end

end

xmin = xopt;
fmin = 0

