工程优化一维搜索算法C程序实现

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

说明本文档用C程序实现了工程优化里面几种一维搜索算法,有理插值法针对函数1()()2xxfxee来进行(参见陈开周老师课本的66页3.9题),其余各算法都针对函数3()342fxxx来进行。各种算法的初始点、停止误差、步长等参数的选取具体参见程序里面变量的初始化值,可以进行适当调整以达到更加精确或者快速的效果。同时通过修改宏定义里面的函数表达式可以求解其它同类问题。C语言里面的计时函数精度不高可以尝试循环多次用平均值来近似代替单次执行时间,也可以使用别的计时函数来求解运行时间。对于本文档中存在的问题欢迎大家批评指正,如果有需要可以通过QQ邮箱联系我:1007824817@qq.com1成功失败法#includestdio.h#includestdlib.h#includemath.h#includetime.h#defineF(x)3*x*x*x-4*x+2intmain(void){doubleh=1.0,e=0.2,x=0;doubley1,y2,minx,t;time_tstart,end;start=clock();y1=F(x);t=x+h;y2=F(t);l:while(y1y2){printf(%.3f,%.3f;%.3f,%.3f;\n,x,y1,x+h,y2);x=x+h;y1=y2;h=2*h;t=x+h;y2=F(t);}if(abs(h)=e){minx=x;}else{h=-h/4;t=x+h;y2=F(t);gotol;}printf(%.3f\n,minx);end=clock();printf(Ittakes%.3fm.,(double(end-start))/CLOCKS_PER_SEC);system(pause);}20.618法#includestdio.h#includestdlib.h#includemath.h#includestring.h#includetime.h#defineF(x)3*x*x*x-4*x+2intmain(void){doublee,a=0,b=2;doublex1,x2,y1,y2,minx,miny;time_tstart,end;start=clock();x1=a+0.382*(b-a);x2=a+0.618*(b-a);y1=F(x1);y2=F(x2);e=b-a;while(e0.2){if(y1y2){b=x2;y2=y1;x2=x1;x1=a+0.382*(b-a);y1=F(x1);}else{a=x1;x1=x2;y1=y2;x2=a+0.618*(b-a);y2=F(x2);}e=b-a;}minx=(a+b)/2;miny=F(minx);printf(%.3f,%.3f\n,minx,miny);end=clock();printf(thetimeis%.3f,(double(end-start))/CLOCKS_PER_SEC);system(pause);}3平分法#includestdio.h#includestdlib.h#includemath.h#includestring.h#includetime.h#defineF(x)3*x*x*x-4*x+2#definef(x)9*x*x-4intmain(void){doublea=0,b=2,c,e;doubley,result;time_tstart,end;start=clock();e=b-a;c=(a+b)/2;while(e0.2){y=f(c);if(y==0){gotol;}elseif(y0){b=c;e=b-a;c=(a+b)/2;}else{a=c;e=b-a;c=(a+b)/2;}}result=F(c);l:printf(%.3f,%.3f\n,c,result);end=clock();printf(thetimeis%.3fm\n,(double(end-start))/CLOCKS_PER_SEC);system(pause);}4牛顿法#includestdio.h#includestdlib.h#includemath.h#includestring.h#includetime.h#defineF(x)3*x*x*x-4*x+2#defineF1(x)9*x*x-4#defineF2(x)18*xintmain(void){intk=0;doublex=1.0,y,m,n,t;time_tstart,end;start=clock();while(abs(F1(x))0.2){m=F1(x);n=F2(x);t=m/n;x=x-t;k++;}y=F(x);printf(%.3f,%.3f\n,x,y);printf(thecomputationtimeis%d\n,k);end=clock();printf(thetimeis%.3fm\n,(double(end-start))/CLOCKS_PER_SEC);system(pause);}5抛物线法#includestdio.h#includestdlib.h#includemath.h#includestring.h#includetime.h#defineF(x)3*x*x*x-4*x+2intmain(void){doublex1,x2,x3,x;doubley1,y2,y3,y;doublenum,den,factor;time_tstart,end;start=clock();x1=0;x2=1;x3=2;y1=F(x1);y2=F(x2);y3=F(x3);num=(pow(x2,2)-pow(x3,2))*y1+(pow(x3,2)-pow(x1,2))*y2+(pow(x1,2)-pow(x2,2))*y3;den=(x2-x3)*y1+(x3-x1)*y2+(x1-x2)*y3;factor=num/den;x=factor/2;y=F(x);while(abs(y-y2)0.2){if(xx2){x1=x2;x2=x;y1=y2;y2=y;}elseif(xx2){x3=x2;x2=x;y3=y2;y2=y;}num=(pow(x2,2)-pow(x3,2))*y1+(pow(x3,2)-pow(x1,2))*y2+(pow(x1,2)-pow(x2,2))*y3;den=(x2-x3)*y1+(x3-x1)*y2+(x1-x2)*y3;factor=num/den;x=factor/2;y=F(x);}printf(%.3f,%.3f\n,x,y);end=clock();printf(thetimeis%.3fm\n,(double(end-start))/CLOCKS_PER_SEC);system(pause);}6简化的抛物线法#includestdio.h#includestdlib.h#includemath.h#includestring.h#includetime.h#defineF(x)3*x*x*x-4*x+2intmain(void){doublex1,x2,x3,x;doubley1,y2,y3,y;doublenum,den;time_tstart,end;start=clock();x1=0;x2=1;x3=2;y1=F(x1);y2=F(x2);y3=F(x3);num=(y3-y1)/(x3-x1);den=((y2-y1)/(x2-x1)-num)/(x2-x3);x=0.5*(x1+x3-num/den);y=F(x);while(abs(y-y2)0.2){if(xx2){x1=x2;x2=x;y1=y2;y2=y;}elseif(xx2){x3=x2;x2=x;y3=y2;y2=y;}num=(y3-y1)/(x3-x1);den=((y2-y1)/(x2-x1)-num)/(x2-x3);x=0.5*(x1+x3-num/den);y=F(x);}printf(%.3f,%.3f\n,x,y);end=clock();printf(thetimeis%.3fm\n,(double(end-start))/CLOCKS_PER_SEC);system(pause);}7三次插值法#includestdio.h#includestdlib.h#includemath.h#includestring.h#includetime.h#defineF(x)3*x*x*x-4*x+2#defineF1(x)9*x*x-4intmain(void){doublea=0,b,h=1,x;doublev,u,s,z,w;time_tstart,end;start=clock();v=F1(a);x=a;while(abs(v)0.2){if(v0){h=abs(h);}else{h=-abs(h);}l:b=a+h;u=F1(b);if(abs(u)0.2){x=b;gotoend;}while(u*v0){h=2*h;v=u;a=b;gotol;}s=3*(F(a)-F(b))/(b-a);z=s-u-v;w=sqrt(pow(z,2)-u*v);if(v0){a=a-(b-a)*v/(z-w-v);}else{a=a-(b-a)*v/(z+w-v);}v=F1(a);h=h/10;x=a;}end:printf(%.3f,%.3f\n,x,F(x));end=clock();printf(thetimeis%.3fm\n,double(end-start)/CLOCKS_PER_SEC);system(pause);}8D.S.C法#includestdio.h#includestdlib.h#includemath.h#includestring.h#includetime.h#defineF(x)3*x*x*x-4*x+2intmain(void){doublex0=0,x1,x2,x3,y0,y1,y2,y3,h=1,num,den;time_tstart,end;start=clock();l:y0=F(x0);x1=x0+h;y1=F(x1);if(y0y1){h=-h;x1=x0+h;y1=F(x1);}while(y1y0){x0=x1;y0=F(x0);x1=x0+h;y1=F(x1);h=h+h;}h=h/2;x3=x1;x2=x3-h;x1=x0;x0=x1-h;y3=F(x3);y2=F(x2);y1=F(x1);y0=F(x0);if(y1y2){num=(y1-y3)*h;den=2*(y1-2*y2+y3);x0=x2+num/den;if(abs((y1-y3)/y1)0.1){h=h/4;gotol;}}else{num=(y0-y2)*h;den=2*(y0-2*y1+y2);x0=x1+num/den;if(abs((y0-y2)/y0)0.1){h=h/4;gotol;}}printf(%.3f,%.3f\n,x0,F(x0));end=clock();printf(thetimeis%.3fm\n,double(end-start)/CLOCKS_PER_SEC);system(pause);}9Powell法#includestdio.h#includestdlib.h#includemath.h#includestring.h#includetime.h#defineF(x)3*x*x*x-4*x+2intmain(void){time_tstart,end;start=clock();doublex0,x1,x2,x3,xmin,y0,y1

1 / 15
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功