3、0.240000)=38.4576002.黃金分割法源程序:#include#include#definef(x)x*x*x*x-5*x*x*x+4*x*x-6*x+60doublehj(double*a,double*b,doublee,int*n){doublex1,x2,s;if(fabs((*b-*a)/(*b))<=e)s=f((*b+*a)/2);else{x1=*b-0.618*(*b-*a);x2=*a+0.618*(*b-*a);if(f(x1)>f(x
4、2))*a=x1;else*b=x2;*n=*n+1;s=hj(a,b,e,n);}returns;}voidmain(){doubles,a,b,e,m;intn=0;printf("輸入a,b值和精度e值");scanf("%lf%lf%lf",&a,&b,&e);s=hj(&a,&b,e,&n);m=(a+b)/2;printf("a=%lf,b=%lf,s=%lf,m=%lf,n=%d",a,b,s,m,n);}8機(jī)械優(yōu)化設(shè)計(jì)編程報(bào)告運(yùn)行過程及結(jié)果:輸入a,b值和精度e值-350.0001
5、a=3.279466,b=3.279793,s=22.659008,m=3.279629,n=213.二次插值法源程序:#include#includeintmain(void){doublea1,a2,a3,ap,y1,y2,y3,yp,c1,c2,m;doublej[3];inti,h=1;voidfinding(doublea[3]);finding(j);a1=j[0];a2=j[1];a3=j[2];m=0.001;doublef(doublex);y1=f(a
6、1);y2=f(a2);y3=f(a3);for(i=1;1>=1;i++){c1=(y3-y1)/(a3-a1);c2=((y2-y1)/(a2-a1)-c1)/(a2-a3);ap=0.5*(a1+a3-c1/c2);yp=f(ap);if(fabs((y2-yp)/y2)0){if(y2>=yp){a1=a2;y1=y2;a2=ap;y2=yp;}else{a3=ap;y3=yp;}}elseif(y2>=yp){a3=a2;y3=y2;a2=ap
7、;y2=yp;}else{a1=ap;y1=yp;}}doublex,y;if(y2<=yp){x=a2;y=y2;}else{x=ap;y=yp;}printf("a*=%f",x);printf("y*=%f",y);return0;}doublef(doublex){doubley;y=x*x-10*x+36;returny;}voidfinding(doublea[3]){inth,i;doubley[3];a[0]=0;h=1;a[1]=h;y[0]=f(a[0]);y[1]=f(a[1
8、]);if(y[1]>y[0]){h=-h;a[2]=a[0];y[2]=y[0];do{a[0]=a[1];a[1]=a[2];y[0]=y[1];y[1]=y[2];a[2]=a[1]+h;y[2]=f(a[2]);h=2*h;}while(y[2]=1;i++){a[2]=a[1]+h;y[2]=f(a[2]);if(y[2]>=y[1])break;8機(jī)械優(yōu)化設(shè)計(jì)編程報(bào)告h