#include<stdio.h>
#include<math.h>doubleF(doublex)
{ returnpow(x,4)+x*2+4;}doublegolden(doublea,doubleb,doublee)
{ inti=0;doublex1,x2,f1,f2;x2=a+0.618*(b-a);
f2=F(x2);x1=a+0.382*(b-a);f1=F(x1);while(fabs(b-a)>e){ printf("\n%d\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf",i++,a,b,x1,x2,f1,f2,fabs(b-a));if(f1<f2){ b=x2;x2=x1;f2=f1;x1=a+0.382*(b-a);f1=F(x1);}elseif(f1>f2){ a=x2;x1=x2;f1=f2;x2=a+0.618*(b-a);f2=F(x2);}else{ a=x1;b=x2;x2=a+0.618*(b-a);f2=F(x2);x1=a+0.382*(b-a);f1=F(x1);}}printf("\n%d\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf",i++,a,b,x1,x2,f1,f2,fabs(b-a));return(a+b)/2;
}intmain()
{ doublea,b,e;a=0-1;b=0;e=0.005;printf("\nresult=%lf",golden(a,b,e));printf("\nover!\n");return0;}