function [ xi,nit ] = GR(f,I,tol)
% function [ xi ] = GR(f,I,tol)
%  Golden ratio method

if nargin==2
    tol=0.000001;
end

G=(1+sqrt(5))/2-1; % 1/g, g is the golden ratio
x1=I(1);
x4=I(2);
d=x4-x1;
x2=x4-G*d;
x3=x1+G*d;
f2=f(x2);f3=f(x3);

nit=1;

while abs(x3-x2)>tol
    if f2<f3
        x4=x3;
        x3=x2; f3=f2;
        x2=x1+(x4-x3);
        f2=f(x2);
    else
        x1=x2;
        x2=x3; f2=f3;
        x3=x4-(x2-x1);
        f3=f(x3);
    end
    nit=nit+1;
end
xi=(x3+x2)/2;

end

