资源描述:
《机械优化设计c语言程序》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、目录一、黄金分割法二、二次插值法三、最速下降法(阶梯法)四、变尺度法五、鲍威尔法一、黄金分割法#include#include#definer0.618#definef(x)x*x+2*xgolden(float,float,float);main(){floata,b,e;printf("请输入区间和收敛精度:a,b,e");scanf("%f,%f,%f",&a,&b,&e);golden(a,b,e);}golden(floata,floatb,floate){floaty1,y2,a1,a2,A,
2、Y;intn=0;a1=b-r*(b-a);a2=a+r*(b-a);y1=f(a1);y2=f(a2);printf("黄金分割法的搜索过程:");do{printf("%da=%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);}else{b=a2;a2=a1;y2=y1;a1=b-r*(b-a);y1=f(a1);}n++;}while(fabs((b-a)/b)>=e
3、
4、fabs((y
5、2-y1)/y2)>=e);A=(a+b)/2;Y=f(A);printf("%da=%f,b=%f,a1=%f,a2=%f",n,a,b,a1,a2);printf("结果:极值点及其函数值:A=%f,Y=%f",A,Y);}二、二次插值法#include"stdio.h"#include"math.h"#include"conio.h"voidmain(){float*area(floata1,floatp,floata[3]);floatf(floatx);floatar,fr;floata1=10,p=0.01,e=0.0
6、00001;floatpa[3];area(a1,p,pa);a1=pa[0];floata2=pa[1];floata3=pa[2];floatf1=f(a1);floatf2=f(a2);floatf3=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(ar>a2){if(fr>f2){a3=ar;f3=fr;}elseif(fr7、=f2;a2=ar;f2=fr;}else{a3=ar;a1=a2;a2=(a1+a3)/2;f1=f2;f3=fr;f2=f(a2);}}elseif(arf2){a1=ar;f1=fr;}elseif(fr8、)=%f",ar,fr);}float*area(floata1,floatp,floata[3]){floatf(floatx);floata2,f2,a3,f3,temp;floatacc=0.00001;floatf1=f(a1);floatstorep=p;while(1){a2=a1+p;f2=f(a2);if(f2>=f1){if(fabs(f2-f1)9、a2=a3;f2=f3;}if(a1>a3){temp=a1;a1=a3;a3=temp;}a[0]=a1;a[1]=a2;a[2]=a3;returna;}floatf(floatx){floaty=pow(fabs(x-1),1.5)+pow(fabs(x-1),2.7);returny;}三、最速下降法#include#includefloatfun1(floatx1,floatx2){floata;a=2*x1;returna;}floatfun2(floatx1,floatx2){floatb;b=50
10、*x2;returnb;}floatfun3(floatx1,floatx2){floaty;y=x1*x1+25*x2*x2;retu