function result=gold(fun,XL,XU,max_iter,es)
clc
clear all
fun=inline('2*sin(x)-x^2/10','x'); XL=-4;Xu=4;max_iter=20; es=0.001;
R=(5^0.5-1)/2;iter=1; %gold radio
if XL>Xu;error('wrong input');end
d=R*(Xu-XL); X1=XL+d;X2=Xu-d;
f1=fun(X1);f2=fun(X2);
while iter<max_iter
d=R*d;
if f1>f2
xopt=f1; XL=X2;X2=X1;X1=XL+d;f2=f1;f1=fun(X1);
else
xopt=f2;Xu=X1;X1=X2;X2=Xu-d;f1=f2;f2=fun(X2);
end
if abs(Xu-XL)<es;break;end
iter=iter+1;
end
result=xopt
end