资源描述
机械优化设计上机源程序与结果 机械优化设计上机源程序与结果 4第 8 页 共 8 页程序一:外推法源程序:#include#include#define B 0.001double fun(double x) double min;min=x*x-10*x+36;return min;void main()double h0=B,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; x1=x2; x2=x3; y3=y1; y1=y2; y2=y3;x3=x2+h;y3=fun(x3);while(y3y2)h*=1.5;x1=x2; x2=x3; x3=x2+h;y1=y2; y2=y3; y3=fun(x3);printf(fun(%f)=%f,nfun(%f)=%f,nfun(%f)=%fn,x1,y1,x2,y2,x3,y3);运行过程及结果:fun(2.954784)=15.182909,fun(4.432676)=11.321857,fun(6.649513)=13.720895程序二:黄金分割法源程序:#include#include#define f(x) x*x-10*x+36 double gi(double *a,double *b,double e,int *n) double x1,x2,min; if(fabs(*b-*a)/(*b)f(x2) *a=x1; else *b=x2; *n=*n+1; min=gi(a,b,e,n); return min; void main() double min,a,b,e,m; int n=0; printf(输入搜索区间a,b值和精度en);scanf(%lf %lf %lf,&a,&b,&e); min=gi(&a,&b,e,&n); m=(a+b)/2; printf(a=%lf,b=%lf,min=%lf,m=%lf,n=%dn,a,b,min,m,n); Press any key to continue 运行过程及结果:a*=5.000000y*=11.000000程序三:二次插值法 源程序:#include#includefloat f(float x) float min; min=x*x-10*x+36; return min;void main()float e=0.001,c1,c2,ap,yp,a,y; float h0=0.03,h=h0,a1=0,a2=h,y1,y2,a3,y3; y1=f(a1); y2=f(a2); if(y2y1) h=-h;a3=a1;y3=y1; a1=a2;y1=y2;a2=a3;y2=y3; a3=a2+h;y3=f(a3);while(y3=e) if(ap-a2)*h0)if(y2=yp)a1=a2;y1=y2;a2=ap;y2=yp; elsea3=ap;y3=yp; elseif(y2=yp)a3=a2;y3=y2;a2=ap;y2=yp; elsea1=ap;y1=yp; if(y2yp)a=a2;y=y2; elsea=ap;y=yp;printf(nthe best result:na=%f,y=%f,a,y);getch(); 运行过程及结果:输入搜索区间a,b值和精度e-7 9 0.00001a=4.999983,b=5.000019,min=11.000000,m=5.000001,n=27程序四:坐标轮换法源程序:#include #include #include float fun1(float x,float a,float b) float y; y=x+a*b; return y; float fun2(float x,float y) float z; z=4*(x-5)*(x-5)+(y-6)*(y-6); return z; main() float d1003,x1003,xx3,ax1003; float a1,a2,a3,h,t,y1,y2,y3,e,a,b,l,fi; int i,k; printf(输入初始点坐标n); scanf(%f%f,&x01,&x02); e=0.000001; l=0.618; x21=x01; x22=x02; k=0; k-; do x01=x21; x02=x22; k+; for(i=1;iy1) h=-h; a3=a1; y3=y1; a1=a2; a2=a3; y1=y2; y2=y3; a3=a2+h; xi1=fun1(xi-11,di1,a3); xi2=fun1(xi-12,di2,a3); y3=fun2(xi1,xi2); doa1=a2; y1=y2; a2=a3; y2=y3; a3=a2+h; xi1=fun1(xi-11,di1,a3); xi2=fun1(xi-12,di2,a3); y3=fun2(xi1,xi2); while(y3a3;) t=a3; a3=a1; a1=t; t=y1; y3=y1; y1=t; a=a1; b=a3; a1=b-l*(b-a); a2=a+l*(b-a); xi1=fun1(xi-11,di1,a1); xi2=fun1(xi-12,di2,a1); y1=fun2(xi1,xi2); xi1=fun1(xi-11,di1,a2); xi2=fun1(xi-12,di2,a2); y2=fun2(xi1,xi2); if(be;) if(y1=y2) a=a1; a1=a2; y1=y2; a2=a+l*(b-a); xi1=fun1(xi-11,di1,a2); xi2=fun1(xi-12,di2,a2); y2=fun2(xi1,xi2); elseb=a2; a2=a1; y2=y1; a1=b-l*(b-a); xi1=fun1(xi-11,di1,a1); xi2=fun1(xi-12,di2,a1); y1=fun2(xi1,xi2); elsefor(;fabs(b-a)/b)=e|fabs(y2-y1)/y2)=e;)if(y1=y2)a=a1; a1=a2; y1=y2; a2=a+l*(b-a); xi1=fun1(xi-11,di1,a2); xi2=fun1(xi-12,di2,a2); y2=fun2(xi1,xi2); elseb=a2; a2=a1; y2=y1; a1=b-l*(b-a); xi1=fun1(xi-11,di1,a1); xi2=fun1(xi-12,di2,a1); y1=fun2(xi1,xi2); axki=0.5*(a+b); xi1=fun1(xi-11,di1,axki); xi2=fun1(xi-12,di2,axki); while(sqrt(pow(x21-x01),2)+pow(x22-x02),2)=1e-6); xx1=x21; xx2=x22; fi=fun2(xx1,xx2);printf(最优解为nx1*=%fnx2*=%fnf*=%fnk=%dn,xx1,xx2,fi,k); 运行过程及结果:输入初始点坐标1 9最优解为x1*=5.000000x2*=6.000000f*=0.000000k=2Press any key to continue程序五:随机方向法源程序:#include#include#includefloat f(float x,float y) float z; z=(x-2)*(x-2)+(y-1)*(y-1); return z;float g1(float x,float y) float z; z=x*x-y; return z;float g2(float x,float y) float z; z=x+y-2; return z; void main() int i,j;float k=8,c=0.000001,a0=-3,b0=3,a1=-3,b1=3; float x10,x010,xl10,e10,r10,d10,h,fl,f0,fx; while(g1(x00,x01)0|g2(x00,x01)0) x00=a0+(rand()/32767.00)*(b0-a0); x01=a1+(rand()/32767.00)*(b1-a1); fl=f(x00,x01);f0=f(x00,x01); while(1) h=0.01; j=1; r0=-1+(rand()/32767.00)*(1-(-1); r1=-1+(rand()/32767.00)*(1-(-1); e0=r0/sqrt(r0*r0+r1*r1); e1=r1/sqrt(r0*r0+r1*r1); x0=x00+h*e0; x1=x01+h*e1; if(g1(x0,x1)=0&g2(x0,x1)=0) fx=f(x0,x1); if(fxfl) fl=fx; for(i=0;i2;i+)di=ei;xli=xi; while(j=k) j+; r0=-1+(rand()/32767.00)*(1-(-1); r1=-1+(rand()/32767.00)*(1-(-1); e0=r0/sqrt(r0*r0+r1*r1); e1=r1/sqrt(r0*r0+r1*r1); x0=x00+h*e0; x1=x01+h*e1; if(g1(x0,x1)=0&g2(x0,x1)=0) fx=f(x0,x1); if(fxfl) fl=fx; for(i=0;i0|g2(x0,x1)0) break; fx=f(x0,x1); if(fxfl) fl=fx; else break; do x0=x0-h*d0; x1=x1-h*d1; h=0.7*h; if(h0|g2(x0,x1)0) continue; fx=f(x0,x1); while(fx=fl); if(fabs(f0-fx)/f0)=c) x00=x0; x01=x1; fl=fx; f0=fx; else break;printf(输出最优解为nx1*=%f,x2*=%f,y*=%fn,x0,x1,fx);输出最优解为x1*=0.995421,x2*=1.004521,y*=1.009200Press any key to continue程序六:四杆机构源程序:#include #include #include #define Pai 3.1415926 int g(float l1,float l2) if(1-l1=0)&(1-l2=0)&(6-l1-l2=0)&(1-l2-4=0)&(l2-l1-4=0)&(l1*l1+l2*l2-1.414*l1*l2-16=0)&(36-l1*l1-l2*l2-1.414*l1*l2=0)return (1);elsereturn (0);float fun(float x02)float f,a31,b31,r31,p31,q31,w31,x12; int i;p0=acos(1+x00)*(1+x00)-x01*x01+25)/(10+10*x00);q0=acos(1+x00)*(1+x00)-x01*x01-25)/(10*x01);f=0;for(i=1;i=30;i+)pi=p0+(Pai/60)*i;ri=sqrt(26-10*cos(pi);ai=acos(ri*ri+x01*x01-x00*x00)/(2*ri*x01);bi=acos(ri*ri+24)/(10*ri);qi=Pai-ai-bi;wi=q0+(2*(pi-p0)*(pi-p0)/(3*Pai);f=f+(Pai/60)*(qi-wi)*(qi-wi)*(pi-pi-1);return f; void main() float a,q,f,fl,f0,l2,z2,d0100,d1100,x2,xi2,fx,m0,m1,e;int i,j,n,k;printf(请输入收敛精度e:);scanf(%f,&e);do z0=0+5*(rand()/32767.00); z1=0+5*(rand()/32767.00);while(g(z0,z1)=0);for(i=0;i=99;i+) d0i=-1+2*(rand()/32767.00);for(j=0;j=99;j+) d1j=-1+2*(rand()/32767.00);f0=fun(z);fl=fun(z);ss:a=0.01;for(i=0,j=0;i=99&j=99;i+,j+)n=1/sqrt(d0i)*(d0i)+d1j*d1j);d0i=d0i/sqrt(d0i)*(d0i)+d1j*d1j);d1j=d1j/sqrt(d0i)*(d0i)+d1j*d1j);x0=z0+a*d0i;x1=z1+a*d1j;if(g(x0,x1)=1)f=fun(x);if(ffl)fl=f;m0=d0i;m1=d1j;l0=x0;l1=x1;x0=l0;x1=l1;doa=1.3*a;x0=x0+a*m0;x1=x1+a*m1;if(g(z0,z1)=0)break;f=fun(x);if(ffl)fl=f;else break;while(g(z0,z1)=1);dox0=x0-a*m0;x1=x1-a*m1;a=0.7*a; if(a=fl);if(fabs(f0-f)/f0)e)xi0=x0;xi1=x1;fx=f;printf(最优解为nx1*=%fnx2*=%fnfx=%fn,xi0,xi1,fx);elsef0=f;fl=f;z0=x0;z1=x1;goto ss; 运行过程及结果:请输入收敛精度e 0.000001最优解为x1*=4.114643x2*=3.723041fx=0.000039
展开阅读全文