机械优化设计上机实践报告.docx

上传人:牧羊曲112 文档编号:2065527 上传时间:2023-01-06 格式:DOCX 页数:45 大小:498.84KB
返回 下载 相关 举报
机械优化设计上机实践报告.docx_第1页
第1页 / 共45页
机械优化设计上机实践报告.docx_第2页
第2页 / 共45页
机械优化设计上机实践报告.docx_第3页
第3页 / 共45页
机械优化设计上机实践报告.docx_第4页
第4页 / 共45页
机械优化设计上机实践报告.docx_第5页
第5页 / 共45页
点击查看更多>>
资源描述

《机械优化设计上机实践报告.docx》由会员分享,可在线阅读,更多相关《机械优化设计上机实践报告.docx(45页珍藏版)》请在三一办公上搜索。

1、机械优化设计上机实践报告班 级 : 机械(茅以升)101姓 名 : 学 号 : 1004010510 成 绩 : 指导教师 : 张 迎 辉 日 期 : 2013.11.20 1 一维搜索方法上机实践报告1、写出所选择的一维搜索算法的基本过程、原理(可附流程图说明)。 (一)进退法1. 算法原理进退法是用来确定搜索区间(包含极小值点的区间)的算法,其理论依据是:为单谷函数(只有一个极值点),且为其极小值点的一个搜索区间,对于任意,如果,则为极小值的搜索区间,如果,则为极小值的搜索区间。因此,在给定初始点,及初始搜索步长的情况下,首先以初始步长向前搜索一步,计算。(1) 如果则可知搜索区间为,其中

2、待求,为确定,后退一步计算,为缩小系数,且,直接找到合适的,使得,从而确定搜索区间。(2) 如果则可知搜索区间为,其中待求,为确定,前进一步计算,为放大系数,且,知道找到合适的,使得,从而确定搜索区间。2. 算法步骤用进退法求一维无约束问题的搜索区间(包含极小值点的区间)的基本算法步骤如下:(1) 给定初始点,初始步长,令,;(2) 令,置;(3) 若,则转步骤(4),否则转步骤(5);(4) 令,令,转步骤(2);(5) 若,则转步骤(6)否则转步骤(7);(6) 令,转步骤(2);(7) 令,停止计算,极小值点包含于区间 (二)黄金分割法1、黄金分割法基本思路:黄金分割法适用于a,b区间上

3、的任何单股函数求极小值问题,对函数除要求“单谷”外不做其他要求,甚至可以不连续。因此,这种方法的适应面非常广。黄金分割法也是建立在区间消去法原理基础上的试探方法,即在搜索区间a,b内适当插入两点a1,a2,并计算其函数值。a1,a2将区间分成三段,应用函数的单谷性质,通过函数值大小的比较,删去其中一段,是搜索区间得以缩小。然后再在保留下来的区间上作同样的处理,如此迭代下去,是搜索区间无限缩小,从而得到极小点的数值近似解。2 黄金分割法的基本原理一维搜索是解函数极小值的方法之一,其解法思想为沿某一已知方向求目标函数的极小值点。一维搜索的解法很多,这里主要采用黄金分割法(0.618法)。该方法用不

4、变的区间缩短率0.618代替斐波那契法每次不同的缩短率,从而可以看成是斐波那契法的近似,实现起来比较容易,也易于人们所接受。图1 黄金分割法是用于一元函数f(x)在给定初始区间a,b内搜索极小点*的一种方法。它是优化计算中的经典算法,以算法简单、收敛速度均匀、效果较好而著称,是许多优化算法的基础,但它只适用于一维区间上的凸函数6,即只在单峰区间内才能进行一维寻优,其收敛效率较低。其基本原理是:依照“去劣存优”原则、对称原则、以及等比收缩原则来逐步缩小搜索区间7。具体步骤是:在区间a,b内取点:a1 ,a2 把a,b分为三段。如果f(a1)f(a2),令a=a1,a1=a2,a2=a+r*(b-

5、a);如果f(a1)=y2a1=b-r*(b-a) y1=f(a1)a2=a+r*(b-a) y2=f(a2)否是(b-a)/b和 (y2-y1)/y2?否是算例1:minf(x)= x*x+2*x(1)C+程序如下:#include #include #define f(x) x*x+2*xdouble calc(double *a,double *b,double e,int *n) double x1,x2,s; if(fabs(*b-*a)f(x2) *a=x1; else *b=x2; *n=*n+1; s=calc(a,b,e,n); return s; main() double

6、 s,a,b,e; int n=0; scanf(%lf %lf %lf,&a,&b,&e); s=calc(&a,&b,e,&n); printf(a=%lf,b=%lf,s=%lf,n=%dn,a,b,s,n); 2、程序运行结果:算例2:minf=x2-10*x+36 理论最优解:x*=5.0,f(x*)=11.0(1)MATLAB程序清单:function f=myfun_yi(x)f=x2-10*x+36 fminbnd(myfun_yi,1,12)(2)运行结果: fminbnd(myfun_yi,1,12)f = 11.0407f = 18.8309f = 12.9691f =

7、11f = 11.0000f = 11.0000ans = 5(3)结果分析:由迭代程序f=11.0,ans=5,与理论结果相等算例3:minf=x4-5*x3+4*x2-6*x+60 理论最优解:x*=3.2796,f(x*)=22.6590(1)MATLAB程序清单:function f=myfun_yi(x)f=x4-5*x3+4*x2-6*x+60 fminbnd(myfun_yi,1,12)(2)运行结果: fminbnd(myfun_yi,1,12)f = 165.3948f = 1.5836e+03f = 24.8730f = 35.9194f = 23.9089f = 22.7

8、621f = 31.7507f = 22.6673f = 22.6594f = 22.6590f = 22.6590f = 22.6590f = 22.6590ans = 3.2796(3)结果分析:由迭代程序得f =22.659,ans =3.2796,与理论最优解相等2 无约束优化搜索方法上机实践报告1、写出所选择的无约束优化搜索算法的基本过程、原理(可附流程图说明)。 鲍威尔改进方法鲍威尔(Powell)法是直接利用函数值来构造共轭方向的一种方法 在鲍威尔基本算法中,每一轮迭代都用连结始点和终点所产生出的搜索方向去替换原向量组中的第一个向量,而不管它的“好坏”,这是产生向量组线性相关的原

9、因所在。 在改进的算法中首先判断原向量组是否需要替换。如果需要替换,还要进一步判断原向量组中哪个向量最坏,然后再用新产生的向量替换这个最坏的向量,以保证逐次生成共轭方向。2、程序计算结果分析:中间各步骤的结果分析及与理论计算结果分析对比。算例1:minf=4*(x(1)-5)2+(x(2)-6)2 初始点:x0=8;9,f(x0)=45 最优解:x*=5;6,f(x*)=0(1)MATLAB程序清单:function f=myfun_wuyueshu(x)f=4*(x(1)-5)2+(x(2)-6)2 x,fval=fminunc(myfun_wuyueshu,x0)(2)运行结果:f = 4

10、5Warning: Gradient must be provided for trust-region algorithm; using line-search algorithm instead. In fminunc at 367 f = 45.0000f = 45.0000f = 23.5625f = 23.5625f = 23.5625f = 2.6958f = 2.6958f = 2.6958f = 1.3788f = 1.3788f = 1.3788f = 0.0054f = 0.0054f = 0.0054f = 6.4975e-05f = 6.4973e-05f = 6.49

11、75e-05f = 6.1579e-09f = 6.1522e-09f = 6.1443e-09f = 1.7876e-12f = 1.8627e-12f = 1.5586e-12Local minimum found.Optimization completed because the size of the gradient is less thanthe default value of the function tolerance.x = 5.0000 6.0000fval = 1.7876e-12(3)结果分析:由迭代程序得x = 5.0000; 6.0000,fval =1.787

12、6e-12,与理论最优解相等。算例2:minf=(x(1)2+x(2)-11)2+(x(1)+x(2)2-7)2 初始点:x0=1;1,f(x0)=106 最优解:x*=3;2,f(x*)=0(1)MATLAB程序清单:function f=myfun_wuyueshu(x)f=(x(1)2+x(2)-11)2+(x(1)+x(2)2-7)2 x,fval=fminunc(myfun_wuyueshu,x0)(2)运行结果: x0=1;1x0 = 1 1 x,fval=fminunc(myfun_wuyueshu,x0)f = 106Warning: Gradient must be prov

13、ided for trust-region algorithm; using line-search algorithm instead. In fminunc at 367 f = 106.0000f = 106.0000f = 29.5430f = 29.5430f = 29.5430f = 1.7450e+04f = 1.7450e+04f = 1.7450e+04f = 90.3661f = 90.3661f = 90.3661f = 0.3575f = 0.3575f = 0.3575f = 0.0179f = 0.0179f = 0.0179f = 0.0064f = 0.0064

14、f = 0.0064f = 1.0048e-06f = 1.0044e-06f = 1.0049e-06f = 4.8639e-09f = 4.8567e-09f = 4.8781e-09f = 5.2125e-12f = 5.8703e-12f = 5.7870e-12Local minimum found.Optimization completed because the size of the gradient is less thanthe default value of the function tolerance.x = 3.0000 2.0000fval = 5.2125e-

15、12(3)结果分析:由迭代程序得x=3;2,fval = 5.2125e-12,与理论最优解相等算例3:ff=x0*x0+2*x1*x1-4*x0-2*x0*x1;(1)鲍威尔改进算法C+程序清单:#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);void jtf(double x0 ,double h0,double s ,int n,double a ,double b )int i; double *

16、x3,h,f1,f2,f3; for (i=0;i3;i+) xi=(double *)malloc (n*sizeof(double); h=h0; for(i=0;in;i+)*(x0+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

17、(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;i+) ai=*(x2+i); bi=*(x0+i); else for(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=

18、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;in;i+) *(x1+i)=ai+0.382*(bi-ai); f2=objf(x1); else for(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

19、=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,int 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(

20、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,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

21、;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=objf(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;

22、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-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-(*(xx

23、1+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,x1,x2,f;ff=powell(p,0.3,0.001,0.0001,2,x);printf(shuchuzuiyoujie:n);x1=x1;x2=x2;f=ff;printf(x1=%f,x2=%f,f=%fn,x

24、1,x2,f);getchar();(2)运行结果为:3约束优化搜索方法上机实践报告1、写出所选择的约束优化搜索算法的基本过程、原理(可附流程图说明)。2、程序计算结果分析:中间各步骤的结果分析及与理论计算结果分析对比。算例1:minf=(x(1)-2)2+(x(2)-1)2; s.t g1(x)=x(1)2-x(2)=0 g2(x)=x(1)+x(2)-2 x,fval=fmincon(myfun_constrain,x0,A,b,mycon)(2) 运行结果: A=-1,0;0,-1b=0;0x0=3;3A = -1 0 0 -1b = 0 0x0 = 3 3 x,fval=fmincon

25、(myfun_constrain,x0,A,b,mycon)Warning: The default trust-region-reflective algorithm does not solve problems with the constraints you havespecified. FMINCON will use the active-set algorithm instead. For information on applicable algorithms, seeChoosing the Algorithm in the documentation. In fmincon

26、 at 486f = 5c = 6 4ceq = f = 5.0000c = 6.0000 4.0000ceq = f = 5.0000c = 6.0000 4.0000ceq = f = 2.0000c = 1.0000 -1.0000ceq = f = 2.0000c = 1.0000 -1.0000ceq = f = 2.0000c = 1.0000 -1.0000ceq = f = 1.0000c = 1.0e-15 * 0.9992 0.4441ceq = f = 1.0000c = 1.0e-07 * 0.2980 0.1490ceq = f = 1.0000c = 1.0e-07

27、 * -0.1490 0.1490ceq = Local minimum found that satisfies the constraints.Optimization completed because the objective function is non-decreasing in feasible directions, to within the default value of the function tolerance,and constraints are satisfied to within the default value of the constraint

28、tolerance.Active inequalities (to within options.TolCon = 1e-06): lower upper ineqlin ineqnonlin 1 2x = 1.0000 1.0000fval = 1.0000 (3)结果分析:由迭代程序得x =1.0000; 1.0000,fval =1.0000,与理论最优解相等 算例2.minf=1000-x(1)2-2*x(2)2-x(3)2-x(1)*x(2)-x(1)*x(3); S.t g1(x)=x(1)2+x(2)2+x(3)2-25=0 g2(x)8*x(1)+14*x(2)+7*x(3)-

29、56=0 g3(x)=-x(1)=0 g4(x)=-x(2)=0 g5(x)=-x(3) x,fval=fmincon(myfun_constrain,x0,A,b,mycon)(2)运行结果 A=-1,0,0;0,-1,0;0,0,-1b=0;0;0x0=2;2;2A = -1 0 0 0 -1 0 0 0 -1b = 0 0 0x0 = 2 2 2 x,fval=fmincon(myfun_constrain,x0,A,b,mycon)Warning: The default trust-region-reflective algorithm does not solve problems

30、 with the constraints you havespecified. FMINCON will use the active-set algorithm instead. For information on applicable algorithms, seeChoosing the Algorithm in the documentation. In fmincon at 486c = -13 2ceq = c = -13.0000 2.0000ceq = c = -13.0000 2.0000ceq = c = -13.0000 2.0000ceq = c = -5.9320 0ceq = c = -5.9320 0.0000ceq = c = -5.9320 0.0000ceq = c = -5.9320 0.0000ceq = c = 1.1562 0.0000ceq = c = 1.1562 0.0000ceq = c = 1.1562 0.0000ceq = c = 1.1562 0.0000ceq = c = 31.5713 0.0000ceq = c = 8.1851 0.0000ceq = c =

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号