全文预览

罚函数法MATLAB程序

上传者:梦溪 |  格式:doc  |  页数:5 |  大小:0KB

文档介绍
tf(“[a,b]=[%.2f,%f]\n”,a,b);printf(“n=%d\n”,n);}运行情况如下:(1)t,h:0,0.05[a,b]=[0.35,1.55]n=4(2)t,h:-1.0,0.02[a,b]=[0.26,4.10]n=7(3)t,h:-100,0.01[a,b]=[-59.05,63.83]n=13例2用0.618法求f(x)=8x^3-2*x^2-7*x+3的局部最优解.允许误差ε=0.0001,初始点设为x0=0,初始步长h=1.#include“math.h”floatf(x)floatx;floaty;{y=((8.0*x-2.0)*x-7.0)*x+3.0;return(y);}main(){intn=0;floatt,h,a,b,c,d,x0,f0,f1,f2,fc,fd;printf(“t,h:”);scanf(“%f,%f”,&t,&h);f1=f(t);if(f(t+h)<=f1)c=t+h;else{h=-h;c=t+h;}f2=f(c);while(f2<=f1){d=t;t=c;f1=f2;c=t+h;f2=f(c);}if(c<=d){a=c;b=d;}else{a=d;b=c;}c=a+0.382*(b-a);fc=f(c);d=a+0.618*(b-a);fd=f(d);do{if(fc>=fd){a=c;c=d;fc=fd;d=a+0.618*(b-a);fd=f(d);}else{b=d;d=c;fd=fc;c=a+0.382*(b-a);fc=f(c);}n++;}while(b-a>0.0001);x0=(a+b)/2.0;f0=f(x0);pintf(“n=%d,x0=%7.4f,f0=%7.4f\n”,n,x0,f0);}运行情况如下:t,h:0,1.0n=21,xo=0.6298,f0=-0.2034.

收藏

分享

举报
下载此文档