function [ xi ] = GR_demo(f,I)
% function [ xi ] = GR_demo(f,I)
%  Demonstration of Golden ratio method

nit=4;

G=(1+sqrt(5))/2-1;
x1=I(1);
x4=I(2);
d=(x4-x1);
x2=x4-G*d;
x3=x1+G*d;
f2=f(x2);f3=f(x3);

xx=linspace(x1,x4,101);
fx=f(xx);
Mf=max(fx);
mf=min(fx);
Df=Mf-mf;
ypl=mf;
plot(xx,fx)
axis([x1-0.1*d,x4+0.1*d,mf-0.75*Df,Mf+0.1*Df])
hold on
plot([x1,x4],(ypl-0.05*Df)*[1,1],'k')
plot([x1,x2,x3,x4],ypl-0.05*Df,'r+')
text(x1-0.01*d,ypl-0.09*Df,'x_1')
text(x2-0.01*d,ypl-0.09*Df,'x_2')
text(x3-0.01*d,ypl-0.09*Df,'x_3')
text(x4-0.01*d,ypl-0.09*Df,'x_4')
pause

for k=1:nit
    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
    ypl=ypl-0.15*Df;
    plot([x1,x4],(ypl-0.05*Df)*[1,1],'k')
    plot([x1,x2,x3,x4],ypl-0.05*Df,'r+')
    text(x1-0.01*d,ypl-0.09*Df,'x_1')
    text(x2-0.01*d,ypl-0.09*Df,'x_2')
    text(x3-0.01*d,ypl-0.09*Df,'x_3')
    text(x4-0.01*d,ypl-0.09*Df,'x_4')
    pause
end
xi=(x3+x2)/2;

end

