您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 机械/模具设计 > 机械优化设计黄金分割法-外推法
郑州大学机械优化设计部分程序1.外推法2.黄金分割法3.二次插值法4.坐标轮换法5.随机方向法6.四杆机构优化设计1.外推法源程序:#includestdio.h#includemath.h#defineR0.01doublefun(doublex){doublem;m=x*x-10*x+36;returnm;}voidmain(){doubleh0=R,y1,y2,y3,x1,x2,x3,h;x1=0;h=h0;x2=h;y1=fun(x1);y2=fun(x2);if(y2y1){h=-h;x3=x1;y3=y1;x1=x2;y1=y2;x2=x3;y2=y3;}x3=x2+h;y3=fun(x3);while(y3y2){h*=2.0;x1=x2;y1=y2;x2=x3;y2=y3;x3=x2+h;y3=fun(x3);}printf(fun(%f)=%f,fun(%f)=%f,fun(%f)=%f\n,x1,y1,x2,y2,x3,y3);}运行过程及结果:fun(2.560000)=16.953600,fun(5.120000)=11.014400,fun(10.240000)=38.4576002.黄金分割法源程序:#includestdio.h#includemath.h#definef(x)x*x*x*x-5*x*x*x+4*x*x-6*x+60doublehj(double*a,double*b,doublee,int*n){doublex1,x2,s;if(fabs((*b-*a)/(*b))=e)s=f((*b+*a)/2);else{x1=*b-0.618*(*b-*a);x2=*a+0.618*(*b-*a);if(f(x1)f(x2))*a=x1;else*b=x2;*n=*n+1;s=hj(a,b,e,n);}returns;}voidmain(){doubles,a,b,e,m;intn=0;printf(输入a,b值和精度e值\n);scanf(%lf%lf%lf,&a,&b,&e);s=hj(&a,&b,e,&n);m=(a+b)/2;printf(a=%lf,b=%lf,s=%lf,m=%lf,n=%d\n,a,b,s,m,n);}机械优化设计编程报告2运行过程及结果:输入a,b值和精度e值-350.0001a=3.279466,b=3.279793,s=22.659008,m=3.279629,n=213.二次插值法源程序:#includestdio.h#includemath.hintmain(void){doublea1,a2,a3,ap,y1,y2,y3,yp,c1,c2,m;doublej[3];inti,h=1;voidfinding(doublea[3]);finding(j);a1=j[0];a2=j[1];a3=j[2];m=0.001;doublef(doublex);y1=f(a1);y2=f(a2);y3=f(a3);for(i=1;1=1;i++){c1=(y3-y1)/(a3-a1);c2=((y2-y1)/(a2-a1)-c1)/(a2-a3);ap=0.5*(a1+a3-c1/c2);yp=f(ap);if(fabs((y2-yp)/y2)m)break;elseif((ap-a2)*h0){if(y2=yp){a1=a2;y1=y2;a2=ap;y2=yp;}else{a3=ap;y3=yp;}}elseif(y2=yp){a3=a2;y3=y2;a2=ap;y2=yp;}else{a1=ap;y1=yp;}}doublex,y;if(y2=yp){x=a2;y=y2;}else{x=ap;y=yp;}printf(a*=%f\n,x);printf(y*=%f\n,y);return0;}doublef(doublex){doubley;y=x*x-10*x+36;returny;}voidfinding(doublea[3]){inth,i;doubley[3];a[0]=0;h=1;a[1]=h;y[0]=f(a[0]);y[1]=f(a[1]);if(y[1]y[0]){h=-h;a[2]=a[0];y[2]=y[0];do{a[0]=a[1];a[1]=a[2];y[0]=y[1];y[1]=y[2];a[2]=a[1]+h;y[2]=f(a[2]);h=2*h;}while(y[2]y[1]);}else{for(i=1;i=1;i++){a[2]=a[1]+h;y[2]=f(a[2]);if(y[2]=y[1])break;机械优化设计编程报告3h=2*h;a[0]=a[1];y[0]=y[1];a[1]=a[2];y[1]=y[2];}}return;}运行过程及结果:a*=5.000000y*=11.0000004.坐标轮换法源程序:#includestdio.h#includemath.h#includeconio.hfloatfun1(floatx,floata,floatb){floaty;y=x+a*b;returny;}floatfun2(floatx,floaty){floatz;z=4*(x-5)*(x-5)+(y-6)*(y-6);returnz;}main(){floatd[100][3],x[100][3],xx[3],ax[100][3];floata1,a2,a3,h,t,y1,y2,y3,e,a,b,l,fi;inti,k;printf(输入初始点坐标\n);scanf(%f%f,&x[0][1],&x[0][2]);e=0.000001;l=0.618;x[2][1]=x[0][1];x[2][2]=x[0][2];k=0;k--;do{x[0][1]=x[2][1];x[0][2]=x[2][2];k++;for(i=1;i=2;i++){if(i==1){d[i][1]=1;d[i][2]=0;}else{d[i][1]=0;d[i][2]=1;}h=0.1;a1=0;a2=h;x[i][1]=fun1(x[i-1][1],d[i][1],a1);x[i][2]=fun1(x[i-1][2],d[i][2],a1);y1=fun2(x[i][1],x[i][2]);x[i][1]=fun1(x[i-1][1],d[i][1],a2);x[i][2]=fun1(x[i-1][2],d[i][2],a2);y2=fun2(x[i][1],x[i][2]);if(y2y1){h=-h;a3=a1;y3=y1;a1=a2;a2=a3;y1=y2;y2=y3;}a3=a2+h;x[i][1]=fun1(x[i-1][1],d[i][1],a3);x[i][2]=fun1(x[i-1][2],d[i][2],a3);y3=fun2(x[i][1],x[i][2]);do{a1=a2;y1=y2;a2=a3;y2=y3;a3=a2+h;x[i][1]=fun1(x[i-1][1],d[i][1],a3);x[i][2]=fun1(x[i-1][2],d[i][2],a3);y3=fun2(x[i][1],x[i][2]);}while(y3y2);for(;a1a3;){t=a3;a3=a1;a1=t;t=y1;机械优化设计编程报告4y3=y1;y1=t;}a=a1;b=a3;a1=b-l*(b-a);a2=a+l*(b-a);x[i][1]=fun1(x[i-1][1],d[i][1],a1);x[i][2]=fun1(x[i-1][2],d[i][2],a1);y1=fun2(x[i][1],x[i][2]);x[i][1]=fun1(x[i-1][1],d[i][1],a2);x[i][2]=fun1(x[i-1][2],d[i][2],a2);y2=fun2(x[i][1],x[i][2]);if(b1e-3){for(;fabs(b-a)e;){if(y1=y2){a=a1;a1=a2;y1=y2;a2=a+l*(b-a);x[i][1]=fun1(x[i-1][1],d[i][1],a2);x[i][2]=fun1(x[i-1][2],d[i][2],a2);y2=fun2(x[i][1],x[i][2]);}else{b=a2;a2=a1;y2=y1;a1=b-l*(b-a);x[i][1]=fun1(x[i-1][1],d[i][1],a1);x[i][2]=fun1(x[i-1][2],d[i][2],a1);y1=fun2(x[i][1],x[i][2]);}}}else{for(;fabs((b-a)/b)=e||fabs((y2-y1)/y2)=e;){if(y1=y2){a=a1;a1=a2;y1=y2;a2=a+l*(b-a);x[i][1]=fun1(x[i-1][1],d[i][1],a2);x[i][2]=fun1(x[i-1][2],d[i][2],a2);y2=fun2(x[i][1],x[i][2]);}else{b=a2;a2=a1;y2=y1;a1=b-l*(b-a);x[i][1]=fun1(x[i-1][1],d[i][1],a1);x[i][2]=fun1(x[i-1][2],d[i][2],a1);y1=fun2(x[i][1],x[i][2]);}}}ax[k][i]=0.5*(a+b);x[i][1]=fun1(x[i-1][1],d[i][1],ax[k][i]);x[i][2]=fun1(x[i-1][2],d[i][2],ax[k][i]);}}while(sqrt(pow((x[2][1]-x[0][1]),2)+pow((x[2][2]-x[0][2]),2))=1e-6);xx[1]=x[2][1];xx[2]=x[2][2];fi=fun2(xx[1],xx[2]);printf(最优解为\nx1*=%f\nx2*=%f\nf*=%f\nk=%d\n,xx[1],xx[2],fi,k);}运行过程及结果:输入初始点坐标89最优解为x1*=5.000000x2*=6.000000f*=0.000000k=25.随机方向法源程序:机械优化设计编程报告5#includemath.h#includestdio.h#includestdlib.hfloatf(floatx,floaty){floatz;z=(x-2)*(x-2)+(y-1)*(y-1);returnz;}floatg1(floatx,floaty){floatz;z=x*x-y;returnz;}floatg2(floatx,floaty){floatz;z=x+y-2;returnz;}voidmain(){inti,j;floatk=8,c=0.000001,a0=-3,b0=3,a1=-3,b1=3;floatx[10],x0[10],xl[10],e[10],r[10],d[10],h,fl,f0,fx;while(g1(x0[0],x0[1])0||g2(x0[0],x0[1])0){x0[0]=a0+(rand()/32767.00)*(b0-a0);x0[1]=a1+(rand()/32767.00)*(b1-a1);}fl=f(x0[0],x0[1]);f0=f(x0[0],x0[1]);while(1){h=0.01;j=1;r[0]=-1+(rand()/32767.00)*(1-(-1));r[1]=-1+(rand()/32767.00)*(1-(-1));e[0]=r[0]/sqrt(r[0]*r[0]+r[1]*r[1]);e[1]=r[1]/sqrt(r[0]*r[0]+r[1]*r[1]);x[0]=x0[0]+h*e[0];x[1]=x0[1]
本文标题:机械优化设计黄金分割法-外推法
链接地址:https://www.777doc.com/doc-4777100 .html