您好,欢迎访问三七文档
当前位置:首页 > 高等教育 > 其它文档 > 坐标轮换法C语言相关程序
坐标轮换法C语言相关程序#includestdio.h#includemath.hfloatf(floatx[]){floatz;z=4*(x[0]-5)*(x[0]-5)+(x[1]-6)*(x[1]-6);returnz;}floaty,y1,y2,y3;floatx1[2],x2[2],x3[2],s[2],a[2],b[2];inti,n=2;huangjin()/*黄金分割法确定每轮最优值*/{floate=0.01,h0=0.03,r=0.618,h=h0;/*外推法确定区间*/{y1=f(x1);for(i=0;in;i++)s[i]=s[i]*h;for(i=0;in;i++)x2[i]=x1[i]+s[i];y2=f(x2);if(y2y1){for(i=0;in;i++){s[i]=-s[i];x3[i]=x1[i];y3=y1;}for(i=0;in;i++){x1[i]=x2[i];y1=y2;x2[i]=x3[i];y2=y3;}}for(i=0;in;i++){x3[i]=x2[i]+s[i];y3=f(x3);}while(y3y2){for(i=0;in;i++)s[i]=s[i]*2;for(i=0;in;i++){x1[i]=x2[i];y1=y2;x2[i]=x3[i];y2=y3;}for(i=0;in;i++){x3[i]=x2[i]+s[i];y3=f(x3);}}for(i=0;in;i++){a[i]=x1[i];b[i]=x3[i];}}for(i=0;in;i++){x1[i]=b[i]-r*(b[i]-a[i]);x2[i]=a[i]+r*(b[i]-a[i]);}y1=f(x1);y2=f(x2);if(y1=y2){for(i=0;in;i++)a[i]=x1[i];for(i=0;in;i++)x1[i]=x2[i];y1=y2;for(i=0;in;i++)x2[i]=a[i]+r*(b[i]-a[i]);y2=f(x2);}else{for(i=0;in;i++)b[i]=x2[i];for(i=0;in;i++)x2[i]=x1[i];y2=y1;for(i=0;in;i++)x1[i]=b[i]-r*(b[i]-a[i]);y1=f(x1);}while(abs(y2-y1)=e){if(y1=y2){for(i=0;in;i++)a[i]=x1[i];for(i=0;in;i++)x1[i]=x2[i];y1=y2;for(i=0;in;i++)x2[i]=a[i]+r*(b[i]-a[i]);y2=f(x2);}else{for(i=0;in;i++)b[i]=x2[i];for(i=0;in;i++)x2[i]=x1[i];y2=y1;for(i=0;in;i++)x1[i]=b[i]-r*(b[i]-a[i]);y1=f(x1);}}for(i=0;in;i++)x1[i]=0.5*(a[i]+b[i]);}main(){floatx0[2],e0=0.06,sum;x0[0]=8.0;x0[1]=9.0;s[0]=0;s[1]=1;do{if(s[1]1){s[0]=1;s[1]=0;}else{s[0]=0;s[1]=1;}for(i=0;in;i++)x1[i]=x0[i];huangjin();sum=0;for(i=0;in;i++)sum+=(x1[i]-x0[i])*(x1[i]-x0[i]);if(sqrt(sum)e0)break;for(i=0;in;i++)x0[i]=x1[i];}while(1);printf(thebestresult:\n);for(i=0;in;i++)printf(x(%d)=%f\n,i,x1[i]);y=f(x1);printf(y=%f\n,y);getch();}
本文标题:坐标轮换法C语言相关程序
链接地址:https://www.777doc.com/doc-7153223 .html