2、y3:(b)如y2>y3,加大步长h=2h,a1=a2,a2=a3,转(3)继续探测。(a)如y2#include#definef(x)3*x*x*x-4*x+2main(){doublef1,f2,f3,temp,x1,x2,x3,h,a,b,Y1,Y2,A,A1,A2,e=0.2;intn;printf("x1=");scanf("%lf",&x1);pri
3、ntf("h=");scanf("%lf",&h);f1=f(x1);x2=x1+h;f2=f(x2);if(f1==f2)a=x1<(x1+h)?x1:x1+h;b=x1>(x1+h)?x1:x1+h;if(f2>f1){h=-h;temp=x1;x1=x2;x2=temp;temp=f1;f1=f2;f2=temp;x3=x2+h;f3=f(x3);}elseif(f2f3){x1=x2;x2=x3;f2=f(x3);x3=x2+h;f3=f(x3)
4、;}a=x1x3?x1:x3;printf("[%f,%f]",a,b);A1=b+0.328*(b-a);Y1=f(A1);A2=a+0.618*(b-a);Y2=f(A2);do{n++;if(!(Y1=e
5、
6、(a-b)<=-e);A=(a+b)*0.5;printf("
7、A=%lf",A);printf("f(A)=%lf",f(A));printf("n=%d",n);printf("a=%f,b=%f",a,b);getch();}陈文斌#includedoublem,n,p,e,a,b;doubleF(doublex){returnm*x*x+n*x+p;}InputData()//数据输入函数的名称要和倒数第四行的一起改,要同时改成同一名称{printf("x,y,z=?");//这里提示输入方程的系数,提示语句x,y,z=?改成你自己的scanf("%lf
8、,%lf,%lf",&m,&n,&p);printf("e=?");//这里提示输入黄金分割的精度,提示语句e=?改成你自己的scanf("%lf",&e);}JTF()//进退法的名称要和倒数第三行一起改{doublex1,x2,x3,f1,f2,f3,t,h;printf("x0,h=?");//这里提示输入进退法的初始点和步长,提示语句x0,h=?改成你自己的scanf("%lf,%lf",&x1,&h);f1=F(x1);x2=x1+h;f2=F(x2);if(f19、f1;f1=f2;f2=t;}else{h=2*h;}x3=x2+h;f3=F(x3);while(!(f2x3){a=x3;b=x1;}else{a=x1;b=x3;}printf("[%lf,%lf]",a,b);}HJFGF()//黄金分割法的名称要和倒数第二行一起改{doublex1,x2,f1,f2;x1=a+0.382*(b-a);x2=a+0.618*(b-a);do{if(F(x1)10、x1;f2=f1;x1=a+0.382*(b-a);f1=F(x1);}else{a=x1;x1=x2;f1=f2;x2=a+0.618*(b-a);f2=F(x2);}}while(!((a-b>0?a-b:b-a)