资源描述:
《最速下降法和牛顿法求最小值点的算法及结果.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1.最速下降法计算:f=(x-1)+5(x-5)(1)程序代码如下:#include#includedoublef1(doublex,doubley){doubler;r=(pow(x-1,2)+5*pow((y-5),2));returnr;}//最速下降法求最优解voidmain(){doubleh=3,x0=3,x1,y0=6,y1,s,r0,r1;doublee0=0.,e1=0.;intk=0;s=sqrt(pow(2*x0-2,2)+pow(10*y0-50,2));printf("
2、%dx1=%fx2=%fs=%f",k,x0,y0,s);while(s>e1){x1=x0;y1=y0;r0=f1(x0,y0);h=3;//一维搜索,成功失败法while(fabs(h)>e0){r1=f1((x1-h*2*(x1-1)),(y1-h*10*(y1-5)));if(r1e0)h=(-1)*h/4;}s=sqrt(pow(2*x0-2,2)+pow(10*y0-50,2))
3、;k++;printf("%dx1=%fx2=%fs=%f",k,x0,y0,s);}printf("x1=%fx2=%f",x0,y0);}(1)初始值设为x1=3,x2=6时,运行结果如下图1-1:图1-1(2)初始值设为x1=30000,x2=60000时,运行结果如下图1-2:图1-21.牛顿法计算:f=(x-1)+5(x-5)(1)程序代码如下:#include#includedoublef1(doublex,doubley){return(pow(x-1,2)+5*pow(y-5
4、,2));}//牛顿法求最优解voidmain(){doubleh=3,x0=3,x1,y0=6,y1,s,r0,r1;doublee0=0.,e1=0.;intk=0;s=sqrt(pow(2*x0-2,2)+pow(10*y0-50,2));printf("%dx=%fy=%fs=%f",k,x0,y0,s);while(s>e1){x1=x0;y1=y0;r0=f1(x0,y0);h=3;//一维搜索while(fabs(h)>e0){r1=f1((x1-h*(x1-1)),(y1-h*(y1-5)));if(r15、0){x0=x1-h*(x1-1);y0=y1-h*(y1-5);r0=r1;h=h+h;}elseif(fabs(h)>e0)h=(-1)*h/4;}s=sqrt(pow(2*x0-2,2)+pow(10*y0-50,2));k++;printf("%dx=%fy=%fs=%f",k,x0,y0,s);}printf("x=%fy=%f",x0,y0);}(1)初始值设为x1=3,x2=6时,运行结果如下图2-1:图2-1(2)初始值设为x1=30000,x2=60000时,运行结果如下图2-2:图2-21.最速下降法求:
6、f=(x-1)+(x-1)+5(x-5)+5(x-5)(1)程序代码如下:#include#includedoublef1(doublex,doublex1,doubley,doubley1){doubler;r=(pow(x-1,2)+pow(x1-1,2)+5*pow(y-5,2)+5*pow(y1-5,2));returnr;}//最速下降法求最优解voidmain(){//x1,x2,x3,x4分别为多项式函数中四个分量的初始值//x5,x6,x7,x8分别为对应的下一个值doubled
7、=3,x1=3,x5,x2=4,x6,x3=8,x7,x4=6,x8,s,r0,r1;doublee0=0.,e1=0.;intk=0;s=sqrt(pow(2*x1-2,2)+pow(2*x2-2,2)+pow(10*x3-50,2)+pow(10*x4-50,2));printf("%dx1=%fx2=%fx3=%fx4=%fs=%f",k,x1,x2,x3,x4,s);while(s>e1){x5=x1;x6=x2;x7=x3;x8=x4;r0=f1(x1,x2,x3,x4);d=3;//一维搜索while(fabs(
8、d)>e0){r1=f1((x5-d*2*(x5-1)),(x6-d*2*(x6-1)),(x7-d*10*(x7-5)),(x8-d*10*(x8-5)));if(r1