资源描述:
《一维黄金分割法程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#definef(t)(8*pow(t,3)-2*pow(t,2)-7*t+3)#defineepspow(10,-6)voidsb(double*a,double*b){doublet0,t1,t,h,alpha,f0,f1;intk=0;printf("请输入初始点t0=");scanf("%lf",&t0);printf("请输入初始步长h=");scanf("%lf",&h);printf("请输入加步系数alpha(需大于1)=");scanf("%lf",&alpha);f0=f
2、(t0);t1=t0+h;f1=f(t1);while(1){ printf("f1=%lf,f2=%lf,t0=%lf,t=%lf,h=%lf,k=%d",f0,f1,t0,t1,h,k); if(f1t1?t:t1; break; } } t1=t0+h; f1=f(t1);}}d
3、oublehjfg(){doublebeta,t1,t2,t;doublef1,f2;doublea=0,b=0;double*c,*d;intk=0;c=&a,d=&b;sb(c,d);printf("[a,b]=[%lf,%lf]",a,b);beta=(sqrt(5)-1.0)/2;t2=a+beta*(b-a);f2=f(t2);t1=a+b-t2;f1=f(t1);while(1){ printf("第%d次迭代的过程如下:",k+1); printf("[t1,t2]=[%lf,%lf]",t1,t2); if(fabs(t1-t2)
4、5、;t=hjfg();printf("函数的最优值为f(%lf)=%lf",t,f(t));}运行结果如下:请输入初始点t0=0请输入初始步长h=1请输入加步系数alpha(需大于1)=2f1=3.000000,f2=2.000000,t0=0.000000,t=1.000000,h=1.000000,k=0f1=2.000000,f2=180.000000,t0=1.000000,t=3.000000,h=2.000000,k=1[a,b]=[0.000000,3.000000]第1次迭代的过程如下:[t1,t2]=[1.145898,1.854102]第2次
6、迭代的过程如下:[t1,t2]=[0.708204,1.145898]第3次迭代的过程如下:[t1,t2]=[0.437694,0.708204]第4次迭代的过程如下:[t1,t2]=[0.708204,0.875388]第5次迭代的过程如下:[t1,t2]=[0.604878,0.708204]第6次迭代的过程如下:[t1,t2]=[0.541020,0.604878]第7次迭代的过程如下:[t1,t2]=[0.604878,0.644345]第8次迭代的过程如下:[t1,t2]=[0.644345,0.668737]第9次迭代的过程如下:[t1,t2]=[0.
7、629270,0.644345]第10次迭代的过程如下:[t1,t2]=[0.619953,0.629270]第11次迭代的过程如下:[t1,t2]=[0.629270,0.635028]第12次迭代的过程如下:[t1,t2]=[0.625712,0.629270]第13次迭代的过程如下:[t1,t2]=[0.629270,0.631470]第14次迭代的过程如下:[t1,t2]=[0.627911,0.629270]第15次迭代的过程如下:[t1,t2]=[0.629270,0.630110]第16次迭代的过程如下:[t1,t2]=[0.630110,0.630
8、630]第