1、机械优化设计C语言程序一、 黄金分割法二、 二次插值法三、 最速下降法(阶梯法)四、 变尺度法五、 鲍威尔法一、黄金分割法#include#include #define r 0.618 #define f(x) x*x+2*x golden(float,float,float);main()float a,b,e;printf(n 请输入区间和收敛精度 :a,b,en); scanf(%f,%f,%f,&a,&b,&e);golden(a,b,e);golden(float a,float b,float e)float y1,y2,a1,a2,A,Y;int n=0;a1=b-r*(b-a
2、); a2=a+r*(b-a);y1=f(a1);y2=f(a2);printf( 黄金分割法的搜索过程 :);do%d printf(n a=%f,b=%f,a1=%f,a2=%f,y1=%f,y2=%f,n,a,b,a1,a2,y1,y2);if(y1=y2)a=a1; a1=a2; y1=y2; a2=a+r*(b-a); y2=f(a2); elseb=a2; a2=a1; y2=y1; a1=b-r*(b-a); y1=f(a1);n+;while(fabs(b-a)/b)=e|fabs(y2-y1)/y2)=e);A=(a+b)/2;Y=f(A);printf(n %d a=%f,
3、b=%f,a1=%f,a2=%f,n,a,b,a1,a2);printf(n 结果:n 极值点及其函数值:A=%f,Y=%fn, A,Y); 二、二次插值法#includestdio.h#includemath.h #includeconio.h void main() float*area(float a1,float p,float a3);float f(float x);float ar,fr;float a1=10,p=0.01,e=0.000001;float pa3;area(a1,p,pa);a1=pa0;float a2=pa1;float a3=pa2;float f1=f
4、(a1);float f2=f(a2);float f3=f(a3);do ar=(a3*a3-a2*a2)*f1+(a1*a1-a3*a3)*f2+(a2*a2-a1*a1)*f3); ar=ar/2/(a3-a2)*f1+(a1-a3)*f2+(a2-a1)*f3);fr=f(ar);if(ara2) if(frf2) a3=ar; f3=fr; else if(frf2) a1=a2; f1=f2;a2=ar; f2=fr; else a3=ar; a1=a2; a2=(a1+a3)/2;f1=f2; f3=fr; f2=f(a2); else if(arf2) a1=ar; f1=fr
5、; else if(frf2) a3=a2; f3=f2;a2=ar; f2=fr; else a1=ar; a3=a2; a2=(a1+a3)/2; f1=fr; f3=f2; f2=f(a2); if (fabs(a1-a3)=e) break; while(1); if(f2=f1) if(fabs(f2-f1)acc) p=p/2;else p=-p;else break; while(1) a3=a2+p; f3=f(a3); if(f2a3) temp=a1; a1=a3; a3=temp; a0=a1;a1=a2;a2=a3;return a;float f(float x)fl
6、oat y=pow(fabs(x-1),1.5)+pow(fabs(x-1),2.7);return y;三、最速下降法#include #include float fun1(float x1,float x2)float a;a=2*x1;return a;float fun2(float x1,float x2)float b;b=50*x2;return b;float fun3(float x1,float x2)float y;y=x1*x1+25*x2*x2;return y;main() float t, e=0.01, x1=0, x2=1, a, b, y, m;a=fun
7、1(x1,x2);b=fun2(x1,x2);m=sqrt(a*a+b*b);while(me) t=(a*a+b*b)/(2*a*a+50*b*b); x1=x1-a*t; x2=x2-b*t; a=fun1(x1,x2); b=fun2(x1,x2); m=sqrt(a*a+b*b);y=fun3(x1,x2);printf(The min is %f,y);四、DFP变尺度法#include#include#include#include #define tt 0.01#define ff 1.0e-6#define ac 1.0e-6#define ad 1.0e-6#define n
8、 2double ia;double fny(double *x)double x1=x0,x2=x1;double f; f=x1*x1+2*x2*x2-4*x1-2*x1*x2;return f;double * iterate(double *x,double a,double *s) double *x1;int i;x1=(double *)malloc(n*sizeof(double); for(i=0;in;i+)x1i=xi+a*si;return x1;double func(double *x,double a,double *s) double *x1; double f
9、; x1=iterate(x,a,s); f=fny(x1);return f;void finding(double a3,double f3,double *xk,double *s) double t=tt;int i;double a1,f1;a0=0;f0=func(xk,a0,s);for(i=0;i+) a1=a0+t; f1=func(xk,a1,s);if(f1=ad)t=-t;a0=a1;f0=f1;elseif(ia=1) return; /breakt=t/2;ia=1;for(i=0;i+) a2=a1+t;f2=func(xk,a2,s);if(f2f1) brea
10、k;t=2*t;a0=a1;f0=f1;a1=a2;f1=f2;if(a0a2)a1=a0;f1=f0;a0=a2;f0=f2;a2=a1;f2=f1;return;double lagrange(double *xk,double *ft,double *s)int i;double a3,f3;double b,c,d,aa;finding(a,f,xk,s);for(i=0;i+)if(ia=1) aa=a1; *ft=f1; break; d=(pow(a0,2)-pow(a2,2)*(a0-a1)-(pow(a0,2)-pow(a1,2)*(a0-a2); if(fabs(d)=0)
11、 break;c=(f0-f2)*(a0-a1)-(f0-f1)*(a0-a2)/d;if(fabs(c)=0) break;b=(f0-f1)-c*(pow(a0,2)-pow(a1,2)/(a0-a1);aa=-b/(2*c);*ft=func(xk,aa,s);if(fabs(aa-a1)f1) aa=a1;break;if(aaa1)if(*ftf1) a2=aa;f2=*ft;else if(*ftf1) a0=aa;f0=*ft;else if(*ftf1) *ft=f1;aa=a1;return aa;double *gradient(double *xk)double *g,f
12、1,f2,q;int i;g=(double*)malloc(n*sizeof(double);f1=fny(xk);for(i=0;in;i+)q=ff;xki=xki+q; f2=fny(xk); gi=(f2-f1)/q; xki=xki-q;return g;double * bfgs(double *xk)double un,vn,hnn,dxn,dgn,sn;double aa,ib;double *ft,*xk1,*g1,*g2,*xx,*x0=xk;double fi;int i,j,k;ft=(double *)malloc(sizeof(double);xk1=(doubl
13、e *)malloc(n*sizeof(double);for(i=0;in;i+)si=0;for(j=0;jn;j+)hij=0;if(j=i) hij=1;g1=gradient(xk);fi=fny(xk); x0=xk;for(k=0;kn;k+) ib=0;if(ia=1) xx=xk; break; ib=0; for(i=0;in;i+) si=0; for(i=0;in;i+) for(j=0;jn;j+)si+= -hij*g1j; aa=lagrange(xk,ft,s); xk1=iterate(xk,aa,s); g2=gradient(xk1);for(i=0;i=
14、ac)&(fabs(g2i-g1i)=ac) ib=ib+1;if(ib=0) xx=xk1; break; fi=*ft;if(k=n-1) int j;xk=xk1; for(i=0;in;i+) for(j=0;jn;j+) hij=0;if(j=i) hij=1; g1=g2; k=-1;elseint j;double a1=0,a2=0; for(i=0;in;i+) dgi=g2i-g1i; dxi=xk1i-xki;for(i=0;in;i+) int j;ui=0;vi=0; for(j=0;jn;j+) ui=ui+dgj*hji; vi=vi+dgj*hij;for(j=
15、0;jn;j+) a1+=dxj*dgj; a2+=vj*dgj; if(fabs(a1)!=0) a2=1+a2/a1; for(i=0;in;i+)for(j=0;jfi) *ft=fi; xx=xk; xk=x0;return xx;void main ()int k;double *xx,f;double xkn=1,1; xx=bfgs(xk);f=fny(xx); printf(nnThe Optimal Design Result Is:n); for(k=0;kn;k+)printf(ntx%d*=%f,k+1,xxk); printf(ntf*=%f,f);getch();五
16、、鲍威尔法#include stdio.h#include stdlib.h#include math.hdouble objf(double x)double ff; ff=x0*x0+2*x1*x1-4*x0-2*x0*x1; return(ff);n,double a ,double b )void jtf(double x0 ,double h0,double s ,int int i;double *x3,h,f1,f2,f3;for (i=0;i3;i+)xi=(double *)malloc (n*sizeof(double);h=h0; for(i=0;in;i+) *(x0+
17、i)=x0i;f1=objf(x0); for(i=0;i=f1)h= -h0;for (i=0;in;i+)*(x2+i)=*(x0+i);f3=f1; for(i=0;in;i+)*(x0+i)= *(x1+i);*(x1+i)= *(x2+i);f1=f2;f2=f3;for(;)h=2. *h;for(i=0;in;i+)*(x2+i)=* (x1+i) +h*si;f3= objf(x2); if(f2f3)break;else for(i=0;in;i+)*(x0+i)= *(x1+i);*(x1+i)= *(x2+i);f1=f2;f2=f3;if(h0. )for(i=0;in
18、;i+)ai=*(x2+i);bi=*(x0+i);elsefor(i=0;in;i+)ai=*(x0+i);bi=*(x2+i); for(i=0;i3;i+) free(xi);double gold(double a,double b,double eps,int n,double xx) int i;double f1,f2,*x2,ff,q,w; for(i=0;i2;i+)xi=(double*)malloc (n*sizeof(double); for(i=0;if2)for(i=0;in;i+) bi=*(x0+i);*(x0+i)=*(x1+i); f1=f2;for(i=0
19、;in;i+)*(x1+i)=ai+0.382*(bi-ai);f2=objf(x1);elsefor(i=0;in;i+)ai=*(x1+i);*(x1+i)=*(x0+i);f2=f1;for(i=0;in;i+)*(x0+i)=ai+0.618*(bi-ai); f1=objf(x0);q=0;for(i=0;ieps);for(i=0;in;i+)xxi=0.5*(ai+bi);ff=objf(xx);for(i=0;i2;i+)free(xi);return(ff);double oneoptim(double x0,double s,double h0,double epsg,in
20、t n,double x) double *a,*b,ff;a=(double *)malloc(n*sizeof(double);b=(double *)malloc(n*sizeof(double); jtf(x0,h0,s,n,a,b);ff=gold(a,b,epsg,n,x);free(a);free(b); return(ff);double powell(double p,double h0,double eps,double epsg,int n,double x) int i,j,m;double *xx4,*ss,*s;double f,f0,f1,f2,f3,fx,dlt
21、,df,sdx,q,d;ss=(double *)malloc(n*(n+1)*sizeof(double); s=(double *)malloc(n*sizeof(double);for (i=0;in;i+)for (j=0;j=n;j+)*(ss+i*(n+1)+j)=0;*(ss+i*(n+1)+i)=1;for (i=0;i4;i+)xxi=(double *)malloc(n*sizeof(double); for (i=0;in;i+)*(xx0+i)=pi;for(;)for (i=0;in;i+) *(xx1+i)=*(xx0+i);xi=*(xx1+i); f0=f1=o
22、bjf(x); dlt=-1;for (j=0;jn;j+)for (i=0;idlt)dlt=df;m=j;sdx=0.;for (i=0;in;i+) sdx=sdx+fabs(xi-(*(xx1+i); if(sdxeps) free(ss);free(s);for (i=0;i4;i+) free(xxi); return(f);for (i=0;in;i+)*(xx2+i)=xi;f2=f;for (i=0;in;i+) *(xx3+i)=2.*(*(xx2+i)-(*(xx1+i); xi=*(xx3+i);fx=objf(x);f3=fx; q=(f1-2*f2+f3)*(f1-
23、f2-dlt)*(f1-f2-dlt); d=0.5*dlt*(f1-f3)*(f1-f3);if(f3f1)|(qd)if(f2=f3)for (i=0;in;i+)*(xx0+i)=*(xx2+i);elsefor (i=0;in;i+) *(xx0+i)=*(xx3+i);elsefor (i=0;in;i+) *(ss+(i+1)*(n+1)=xi-(*(xx1+i); *(s+i)=*(ss+(i+1)*(n+1); f=oneoptim(xx0,s,h0,epsg,n,x); for(i=0;in;i+) *(xx0+i)=xi;for (j=m+1;j=n;j+)for (i=0;in;i+) *(ss+i*(n+1)+j-1)=*(ss+i*(n+1)+j);void main()double p=1,1;double ff,x2; ff=powell(p,0.3,0.001,0.0001,2,x);printf( 输出最优点及其目标函数值: n); printf(x0=%f,x1=%f,f
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1