资源描述:
《坐标轮换法(麦华锋 东莞理工)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#includeintmain(void){doublef(doublex1,doublex2);doublefun(doublex,doublea0,doubleh0,double*a,double*b,intN);doublefun2(doublex,doublea,doubleb,intN);doubleh0=1,p,a,b,y,x1[2]={0},x2[2]={0};inti=0;printf("input:x2[1],x2[2]:");scanf("%f%f",&x2[1],x1[2]);d
2、o{x1[1]=x2[1];x1[2]=x2[2];fun(x1[1],x1[2],h0,&a,&b,1);x2[2]=fun2(x1[1],a,b,1);fun(x2[2],x1[1],h0,&a,&b,2);x2[1]=fun2(x2[2],a,b,2);i=i+1;printf("(%d)[%f,%f]",i,x2[1],x2[2]);p=sqrt((x2[1]-x1[1])*(x2[1]-x1[1])+(x2[2]-x1[2])*(x2[2]-x1[2]));printf("p=%f",p);}while(sqrt((x2[1]-x1[1])*(x2[1]-x1[1])+(x2
3、[2]-x1[2])*(x2[2]-x1[2]))<=0.0001);y=f(x2[1],x2[2]);printf("x=[%f,%f],f(x)=%f",x2[1],x2[2],y);system("pause");return0;}doublef(doublex1,doublex2){doubley;y=2*x1*x1+3.1*x2*x2+2*x1*x2-2*x1+6*x2-1;returny;}voidfun(doublex,doublea0,doubleh0,double*a,double*b,intN){doubley1,y2,y3,a2,a1,a3,h;a1=a0
4、;h=h0;switch(N){case1:y1=f(x,a1);a2=a1+h;y2=f(x,a2);break;case2:y1=f(a1,x);a2=a1+h;y2=f(a2,x);break;}if(y2>y1){h=-h;a3=a1;y3=y1;a1=a2;y1=y2;a2=a3;y2=y3;}a3=a2+h;switch(N){case1:y3=f(x,a3);break;case2:y1=f(a3,x);break;}if(y35、lse{*a=a1;*b=a3;}printf("N=%da=%fb=%f",N,a,b);}}doublefun2(doublex,doublea,doubleb,intN){doublea1,a2,y1,y2,result;a1=a+0.382*(b-a);a2=a+0.618*(b-a);switch(N){case1:y1=f(x,a1);y2=f(x,a2);break;case2:y1=f(a1,x);y2=f(a2,x);break;}while(fabs(b-a)>0.0001){if(y1>=y2){a=a1;a1=a2;y1=y2;a2=a+0.618*(b-a);
6、switch(N){case1:y2=f(x,a2);break;case2:y2=f(a2,x);break;}}else{b=a2;a2=a1;y2=y1;a1=a+0.382*(b-a);switch(N){case1:y1=f(x,a1);break;case2:y1=f(a1,x);break;}}return0;}