收藏 分享(赏)

最速下降法 C语言.doc

上传人:精品资料 文档编号:8328115 上传时间:2019-06-20 格式:DOC 页数:11 大小:285KB
下载 相关 举报
最速下降法 C语言.doc_第1页
第1页 / 共11页
最速下降法 C语言.doc_第2页
第2页 / 共11页
最速下降法 C语言.doc_第3页
第3页 / 共11页
最速下降法 C语言.doc_第4页
第4页 / 共11页
最速下降法 C语言.doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

1、1.最速下降法#include “stdio.h“#include “math.h“double fun1(double x1,double x2) /*定义函数 fun1 为目标函数*/double y;y=x1*x1-2*x1*x2+4*x2*x2+x1-3*x2; return y;void main() double t, x1=1, x2=1,e=0.01, g2, y, m; int k=1; /*定义起始点为 x1=0,x2=1 ,并定义精度为e=0.01*/g0=2*x1-2*x2+1; /*目标函数对 x1 求偏导*/g1=(-2)*x1+8*x2-3; /*目标函数对 x2

2、 求偏导*/m=(sqrt(g0*g0+g1*g1); /*对 g0*g0+g1*g1求开方,将值赋给 m*/while(me /*根据梯度法(最速下降法) ,利用梯度和海赛矩阵 */x1=x1-g0*t; /*求步长 t。 根据步长和梯度方向求出新的x1,x2*/x2=x2-g1*t;printf(“迭代次数%dn“,k);printf(“搜索方向-%f,-%f,负梯度的模%f,步长%fn“,g0,g1,m,t);printf(“x 的值%f,%fn“,x1,x2); g0=2*x1-2*x2+1; g1=(-2)*x1+8*x2-3; m=(sqrt(g0*g0+g1*g1); /*计算新

3、的 m*/printf(“新的负梯度的模%fn“,m); k+;y=fun1(x1,x2); /*当 m 不满足 me 的时候退出循环,并计算fun1,*/printf(“分别输出 x1,x2 %f,%fn“,x1,x2); /*将值赋给 y,并输出。*/printf(“极小值 y%f“,y);2.FR 共轭梯度法#include “stdio.h“#include “math.h“double fun1(double x1,double x2) /*定义函数 fun1 为目标函数*/double y;y=x1*x1-2*x1*x2+4*x2*x2+x1-3*x2; return y;doub

4、le fun2(double g,double d) /*定义函数 fun2 为求步长的函数*/double buchang;buchang=-(g0*d0+g1*d1)/(d0*(2*d0-2*d1)+d1*(-2)*d0+8*d1); return buchang;void main() double t, beta, x1=1, x2=1, d2,g4, y, m,e=0.01; /*定义起始点为 x1=0,x2=1,并定义精度为 e=0.01*/int k=1; g0=2*x1-2*x2+1; /*目标函数对 x1 求偏导*/g1=(-2)*x1+8*x2-3; /*目标函数对 x2

5、求偏导,求梯度*/m=(sqrt(g0*g0+g1*g1); /*对 g0*g0+g1*g1求开方,将值赋给 m*/while(med1=-g1;beta=0; /*计算因子 beta*/elsebeta=(g0*g0+g1*g1)/(g2*g2+g3*g3); /*计算因子 beta*/ d0=-g0+beta*d0;d1=-g1+beta*d1;t=fun2(g,d); /*计算步长*/x1=x1+d0*t; /*根据步长和搜索方向求出新的 x1,x2*/x2=x2+d1*t;printf(“迭代次数为%dn“,k);printf(“梯度%f,%f,梯度的模 %fn“,g0,g1,m);p

6、rintf(“搜索方向%f,%f,计算因子 %f,步长%fn“,d0,d1,beta,t);printf(“x 的值%f,%fn“,x1,x2); g2=g0;g3=g1;g0=2*x1-2*x2+1; /*根据得到的 x1,x2 求出新的梯度,并将值*/g1=(-2)*x1+8*x2-3; /*赋给 g0,g1,*/m=double(sqrt(g0*g0+g1*g1); /*计算新的 m*/ printf(“新的负梯度的模%fn“,m);k+;y=fun1(x1,x2); /*当 m 不满足 me 的时候退出循环,并计算fun1,*/printf(“分别输出 x1,x2 %f,%fn“,x1

7、,x2); /*将值赋给 y,并输出。*/printf(“极小值 y %f“,y); 3.DFP 法#include#include#include# define e 0.01double fun1 (double x) /*定义目标函数为 fun1 函数*/return (double)pow(x0,2)+4*pow(x1,2)-2*x0*x1+x0-3*x1;void fun2 (double x,double g_x) /*定义求梯度函数为 fun2 函数*/g_x0 = (double)2*x0-2*x1+1;g_x1 = (double)8*x1-2*x0-3;void fun3

8、(double h, double g, double d) /*定义求搜索方向函数为 fun3 函数*/d0 = -(h0*g0+h1*g1);d1 = -(h2*g0+h3*g1);double fun4 ( double x, double d) /*定义求步长函数为 fun4 函数*/return-(2*x0*d0+8*x1*d1-2*x0*d1-2*x1*d0+d0-3*d1)/(2*d0*d0+8*d1*d1-4*d0*d1);void fun5(double h,double x,double tx,double g_x,double tg )/*定义求 H 矩阵函数为 fun5

9、 函数*/double q2,p2,temp2,th4; q0 = tg0 - g_x0; /*第 k+1 个点梯度与第 k 个点处的梯度之差 */ q1 = tg1 - g_x1; p0 = tx0 - x0; /*p(k),第 k+1 个点与第 k 个点的点差 */p1 = tx1 - x1; th0 = h0; /*用临时变量储存原 H 矩阵的值*/th1 = h1;th2 = h2;th3 = h3;temp0 = p0 * q0 + p1 * q1;temp1 = (q0 * h0 + q1 * h2) * q0+ (q0 * h1 + q1 * h3) * q1;h0 = th0

10、+ pow(p0,2) / temp0- pow(th0 * q0 + th1 * q1),2) / temp1;h1 = th1 + (p0 * p1) / temp0- (th0 * q0 + th1 * q1) * (th2 * q0+ th3 * q1) / temp1;h2 = th2 + (p0 * p1) / temp0- (th0 * q0 + th1 * q1) * (th2 * q0+ th3 * q1) / temp1;h3 = th3 + pow(p1,2) / temp0- pow(th2 * q0 + th3 * q1),2) / temp1;void main()

11、int k=1;double x2 = 1 , 1, /*计算初始点*/tx2 = 1 , 1, /*临时存储计算点*/g_x2, /*梯度*/tg2, /*临时梯度*/t, /*步长*/d2, /*搜索方向*/h4 = 1, 0, 0, 1; /*初始 H 矩阵*/double fx = 0;fun2(x,g_x); /*求梯度*/tg0=g_x0;tg1=g_x1;fun3(h, g_x, d); /*求搜索方向*/int counter=1;while(sqrt(pow(g_x0,2)+pow(g_x1,2)-e0)dofun3(h, tg, d); /*求搜索方向*/t = fun4(

12、 x, d); /*求步长*/printf(“n 迭代次数为%dn“,counter+);printf(“梯度%f,%f, 梯度的模%fn“,g_x0,g_x1, sqrt(pow(g_x0,2)+pow(g_x1,2);printf(“搜索方向%f,%f, 步长%fn“,d0,d1,t);printf(“H 矩阵n%f,%fn%f,%f“,h0,h1,h2,h3);if(k=2)tx0=x0+t*d0;tx1=x1+t*d1; else tx0=tx0+t*d0;tx1=tx1+t*d1;fun2( tx, tg); /*求梯度 */fun5(h, x, tx, g_x, tg);/*H 矩

13、阵*/if(k = 2) x0 = tx0;x1 = tx1;printf(“nx1=%ftx2=%f“,x0,x1);fun2( x, g_x);printf(“n 新梯度的模:%f“,sqrt(pow(g_x0,2)+pow(g_x1,2);k+;while(k=2);k = 1;fx = fun1( x);printf(“n 最优解为:n“);printf(“nx1=%f, x2=%fn“,x0,x1);printf(“n 最优值为:n“);printf(“nf(x)=%f n“,fx);4.BFGS 法#include#include#include# define e 0.01dou

14、ble fun1 (double x) /*定义目标函数为 fun1 函数 */return (double)pow(x0,2)+4*pow(x1,2)-2*x0*x1+x0-3*x1;void fun2 (double x,double g_x) /*定义求梯度函数为 fun2 函数*/g_x0 = (double)2*x0-2*x1+1;g_x1 = (double)8*x1-2*x0-3;void fun3 (double h, double g, double d) /*定义求搜索方向函数为 fun3 函数*/d0 = -(h0*g0+h1*g1);d1 = -(h2*g0+h3*g1

15、);double fun4 ( double x, double d) /*定义求步长函数为 fun4 函数*/return-(2*x0*d0+8*x1*d1-2*x0*d1-2*x1*d0+d0-3*d1)/(2*d0*d0+8*d1*d1-4*d0*d1);void fun5(double h,double x,double tx,double g_x,double tg )/*定义求 H 矩阵函数为 fun5 函数*/double q2,p2,temp2,th4; q0 = tg0 - g_x0; /*第 k+1 个点梯度与第 k 个点处的梯度之差 */ q1 = tg1 - g_x1;

16、 p0 = tx0 - x0; /*p(k),第 k+1 个点与第 k 个点的点差 */p1 = tx1 - x1; th0 = h0; /*用临时变量储存原 H 矩阵的值*/th1 = h1;th2 = h2;th3 = h3;temp0 = p0 * q0 + p1 * q1;/pt*q temp1 = (q0 * h0 + q1 * h2) * q0+ (q0 * h1 + q1 * h3) * q1;/qt*H*q h0 =th0+ (1 + temp1/temp0)*(pow(p0,2) / temp0) - (p0*q0*h0+p0*q1*h2)+(h0*q0*p0+h1*q1*p

17、0) /temp0;h1 =th1+ (1 + temp1/temp0)*(p0 * p1) / temp0)- (p0*q0*h1+p0*q1*h3)+(h0*q0*p1+h1*q1*p1) /temp0;h2 =th2+ (1 + temp1/temp0)*(p1 * p0) / temp0)- (p1*q0*h0+p1*q1*h2)+(h2*q0*p0+h3*q1*p0) /temp0;h3 =th3+ (1 + temp1/temp0)*(p1 * p1) / temp0)- (p1*q0*h1+p1*q1*h3)+(h2*q0*p1+h3*q1*p1) /temp0;void mai

18、n()int k=1;double x2 = 1 , 1, /*计算初始点*/tx2 = 1 , 1, /*临时存储计算点*/g_x2, /*梯度*/tg2, /*临时梯度*/t, /*步长*/d2, /*搜索方向*/h4 = 1, 0, 0, 1; /*初始 H 矩阵*/double fx = 0;fun2( x, g_x); /*求梯度*/tg0=g_x0;tg1=g_x1;fun3(h, g_x, d); /*求搜索方向*/int counter=1;int j=0; while(sqrt(pow(g_x0,2)+pow(g_x1,2)-e0 fun3(h, tg, d); /*求搜索方

19、向*/t = fun4( x, d); /*求步长*/printf(“梯度%f,%f, 梯度的模%fn“,g_x0,g_x1, sqrt(pow(g_x0,2)+pow(g_x1,2);printf(“搜索方向%f,%f, 步长%fn“,d0,d1,t);printf(“H 矩阵n%f,%fn%f,%f“,h0,h1,h2,h3);if(k=2)tx0=x0+t*d0;tx1=x1+t*d1; else tx0=tx0+t*d0;tx1=tx1+t*d1;fun2(tx, tg); /*求梯度*/fun5(h, x, tx, g_x, tg);/*求 H 矩阵*/if(k = 2) x0 = tx0;x1 = tx1;printf(“nx1=%ftx2=%f“,x0,x1);fun2( x, g_x);printf(“n 新梯度的模:%f“,sqrt(pow(g_x0,2)+pow(g_x1,2); k+;while(k=2);k = 1;j+;fx = fun1( x);printf(“n 最优解为:n“);printf(“nx1=%f, x2=%fn“,x0,x1);printf(“n 最优值为:n“);printf(“nf(x)=%f n“,fx);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报