《机械优化设计黄金分割法 外推法.doc》由会员分享,可在线阅读,更多相关《机械优化设计黄金分割法 外推法.doc(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精品文档,仅供学习与交流,如有侵权请联系网站删除郑州大学机械优化设计部分程序1.外推法2.黄金分割法3.二次插值法4.坐标轮换法5.随机方向法6.四杆机构优化设计【精品文档】第 7 页1.外推法源程序:#include#include#define R 0.01double fun(double x) double m;m=x*x-10*x+36;return m;void main()double h0=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
2、;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)=%fn,x1,y1,x2,y2,x3,y3);运行过程及结果:fun(2.560000)=16.953600,fun(5.120000)=11.014400,fun(10.240000)=38.4576002.黄金分割法源程序:#include#include#define f(x) x*x*x*x-5*x*x*x+4*x*x-6*x+6
3、0 double hj(double *a,double *b,double e,int *n) double x1,x2,s; if(fabs(*b-*a)/(*b)f(x2) *a=x1; else *b=x2; *n=*n+1; s=hj(a,b,e,n); return s; void main() double s,a,b,e,m; int n=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=%dn,a,
4、b,s,m,n); 运行过程及结果:输入a,b值和精度e值-350.0001a=3.279466,b=3.279793,s=22.659008,m=3.279629,n=213.二次插值法源程序:#include#includeint main(void) double a1,a2,a3,ap,y1,y2,y3,yp,c1,c2,m; double j3; int i,h=1; void finding(double a3); finding(j); a1=j0; a2=j1; a3=j2; m=0.001; double f(double x); y1=f(a1); y2=f(a2); y3
5、=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)0) if(y2=yp) a1=a2;y1=y2; a2=ap;y2=yp; else a3=ap;y3=yp; else if(y2=yp) a3=a2;y3=y2; a2=ap;y2=yp; elsea1=ap;y1=yp; double x,y; if(y2y0) h=-h; a2=a0;y2=y0; do a0=a1;a1=a2; y0=
6、y1;y1=y2; a2=a1+h;y2=f(a2); h=2*h; while(y2=1;i+) a2=a1+h;y2=f(a2); if(y2=y1) break; h=2*h; a0=a1;y0=y1; a1=a2;y1=y2; return;运行过程及结果:a*=5.000000y*=11.0000004.坐标轮换法源程序:#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*(
7、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
8、+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(x
9、i-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)
10、; 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
11、(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);运行过程及结果:输入初始点坐标89最优解为x1*=5.000000x2*=6.000000f*=0.000000k=25.随机方向法源程序:#include#include#includefloat f(float x,float y)
12、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+
13、(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(fx
14、fl) 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(f
15、xfl) 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.0092006.四杆机构优化设计源程序:#include#incl
16、ude#include#define Pai 3.1415926int g(float l1,float l2) if(-l1=0)&(-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);else return (0);float fun(float x02)floatf, a31,b31,r31,p31,q31,w31,x12;int i;p0=acos(1+x00)*(1+x00)-x01*x01+25)/(10+1
17、0*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,f
18、0,l2,z2,d0100,d1100,x2,xi2,fx,m0,m1,e;int i,j,n,k;printf(输入精度); scanf(%f,&e);doz0=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
19、(d0i)*(d0i)+d1j*d1j);d0i=n*d0i;d1j=n*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); else f0=f; fl=f; z0=x0; z1=x1; goto ss;运行过程及结果:输入精度0.001最优解为x1*=4.161386x2*=2.311257fx=0.000021
限制150内