资源描述:
《机械优化设计实验指导书》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、实验一一维搜索方法本实验求函数f(x)=(x-3)2以及f(x)=-(x-3)2的搜索区间[a,b]。并用黄金分割法和插值法分别求最优解。进退法:#include#include/函数f(x)=(x-3)2/doublef(doublex){return(x-3)(x-3);}/求搜索区间[a,b]的函数,x0---初始点;h0---初始步长/voidfind_ab(doublex0,doubleh0,doublea,doubleb){doubleh,x1,y1,x2,y
2、2,x3,y3;h=h0;x1=x0;y1=f(x1);x2=x1+h;y2=f(x2);if(y2>=y1){}for(;;){h=2.0;x3=x2+h;y3=f(x3);if(y20){}else{}}/黄金分割法a,b---搜索区间[a,b];e---精度x,y---最优解X,F/voidsearch_gold(doublea,doubleb,doublee,doublex,doubley){doublex1,x2,y1,y2;x1=;y1=f(x1);x2=;y2
3、=f(x2);do{if(y1e);}/二次插值法a,b---搜索区间[a,b];e---精度xpt,ypt---最优解X,F/voidsearch_insert(doublea,doubleb,doublee,doublexpt,doublefpt){doublex1,x2,f1,f2,x3,f3,xp,fp,xp0,c1,c2;intk=1;x1=a;x3=b;x2=0.5(a+b);f1=f(x1);f2=f(x2);f3=f(x3);xp0=0;for
4、(;;){c1=(f3-f1)/(x3-x1);c2=((f2-f1)/(x2-x1)-c1)/(x2-x3);if(c2==0.0){,break;}xp=0.5(x1+x3-c1/c2);fp=f(xp);if((xp-x1)(x3-xp)<=0.0){break;}if(k!=1)if(fabs(xp0-xp)<=e){break;}if(xp>x2)if(f25、-鲍威尔方法本实验用鲍威尔方法求函数f(x)=(x1-5)2+(x2-6)2的最优解。#include#include#includeconstMAXN=10;doublexkk[MAXN],xk[MAXN],sk[MAXN];intN;doubleF(doublex){return4pow(x[0]-5,2.0)+pow(x[1]-6,2.0);}doublef(doublex){for(inti=0;i6、[i];returnF(xkk);}/无约束坐标轮换法x0--初始点e1--一维搜索精度e2--求解精度/doublenc_trans(doublex0,doublee1,doublee2){inti,j,k=1;doublea,b,ax,ay,d;for(;;){for(j=0;j7、b,e2,&ax,&ay);for(j=0;j8、++)x0[j]=xkk[j];returnF(xkk);}/鲍威尔法x0--初始点e1--一维搜索精度e2--求解精度/doublenc_powell(doublex0,doublee1,doublee2){inti,j,k=1,m;doublea,b,ax,ay,d;doubless[MAXN][MAXN],s1[MAXN],ff[MAXN],x[MAXN],xn[MAXN],xn1[MAXN],f0,f1,f2,f3;f