最速下降法C语言.docx
- 文档编号:13662722
- 上传时间:2023-06-16
- 格式:DOCX
- 页数:16
- 大小:228.59KB
最速下降法C语言.docx
《最速下降法C语言.docx》由会员分享,可在线阅读,更多相关《最速下降法C语言.docx(16页珍藏版)》请在冰点文库上搜索。
最速下降法C语言
1.最速下降法
#include""
#include""
doublefun1(doublex1,doublex2)/*定义函数fun1为目标函数*/
{doubley;
y=x1*x1-2*x1*x2+4*x2*x2+x1-3*x2;
returny;
}
voidmain()
{doublet,x1=1,x2=1,e=,g[2],y,m;
intk=1;/*定义起始点为x1=0,x2=1,并定义精度为e=*/
g[0]=2*x1-2*x2+1;/*目标函数对x1求偏导*/
g[1]=(-2)*x1+8*x2-3;/*目标函数对x2求偏导*/
m=(sqrt(g[0]*g[0]+g[1]*g[1]));/*对g[0]*g[0]+g[1]*g[1]求开方,将值赋给m*/
while(m>e&&k<=200)/*判断,当m>e时进行以下循环*/
{t=((2*g[1]-2*g[0])*x1+(2*g[0]-8*g[1])*x2-g[0]+3*g[1])/(4*g[0]*g[1]-2*g[0]*g[0]-8*g[1]*g[1]);/*根据梯度法(最速下降法),利用梯度和海赛矩阵*/
x1=x1-g[0]*t;/*求步长t。
根据步长和梯度方向求出新的x1,x2*/
x2=x2-g[1]*t;
printf("迭代次数%d\n",k);
printf("搜索方向-%f,-%f,负梯度的模%f,步长%f\n",g[0],g[1],m,t);
printf("x的值%f,%f\n",x1,x2);
g[0]=2*x1-2*x2+1;
g[1]=(-2)*x1+8*x2-3;
m=(sqrt(g[0]*g[0]+g[1]*g[1]));/*计算新的m*/
printf("新的负梯度的模%f\n",m);
k++;
}
y=fun1(x1,x2);/*当m不满足m>e的时候退出循环,并计算fun1,*/
printf("分别输出x1,x2%f,%f\n",x1,x2);/*将值赋给y,并输出。
*/
printf("极小值y%f",y);
}
共轭梯度法
#include""
#include""
doublefun1(doublex1,doublex2)/*定义函数fun1为目标函数*/
{doubley;
y=x1*x1-2*x1*x2+4*x2*x2+x1-3*x2;
returny;
}
doublefun2(doubleg[],doubled[])/*定义函数fun2为求步长的函数*/
{
doublebuchang;
buchang=-(g[0]*d[0]+g[1]*d[1])/(d[0]*(2*d[0]-2*d[1])+d[1]*((-2)*d[0]+8*d[1]));
returnbuchang;
}
voidmain()
{doublet,beta,x1=1,x2=1,d[2],g[4],y,m,e=;
/*定义起始点为x1=0,x2=1,并定义精度为e=*/
intk=1;
g[0]=2*x1-2*x2+1;/*目标函数对x1求偏导*/
g[1]=(-2)*x1+8*x2-3;/*目标函数对x2求偏导,求梯度*/
m=(sqrt(g[0]*g[0]+g[1]*g[1]));/*对g[0]*g[0]+g[1]*g[1]求开方,将值赋给m*/
while(m>e&&k<=200)/*判断,当m>e时进行以下循环*/
{if(k==1)
{
d[0]=-g[0];
d[1]=-g[1];
beta=0;}/*计算因子beta*/
else
{
beta=(g[0]*g[0]+g[1]*g[1])/(g[2]*g[2]+g[3]*g[3]);/*计算因子beta*/
d[0]=-g[0]+beta*d[0];
d[1]=-g[1]+beta*d[1];
}
t=fun2(g,d);/*计算步长*/
x1=x1+d[0]*t;/*根据步长和搜索方向求出新的x1,x2*/
x2=x2+d[1]*t;
printf("迭代次数为%d\n",k);
printf("梯度%f,%f,梯度的模%f\n",g[0],g[1],m);
printf("搜索方向%f,%f,计算因子%f,步长%f\n",d[0],d[1],beta,t);
printf("x的值%f,%f\n",x1,x2);
g[2]=g[0];
g[3]=g[1];
g[0]=2*x1-2*x2+1;/*根据得到的x1,x2求出新的梯度,并将值*/
g[1]=(-2)*x1+8*x2-3;/*赋给g[0],g[1],*/
m=double(sqrt(g[0]*g[0]+g[1]*g[1]));/*计算新的m*/
printf("新的负梯度的模%f\n",m);
k++;
}
y=fun1(x1,x2);/*当m不满足m>e的时候退出循环,并计算fun1,*/
printf("分别输出x1,x2%f,%f\n",x1,x2);/*将值赋给y,并输出。
*/
printf("极小值y%f",y);
}
法
#include<>
#include<>
#include<>
#definee
doublefun1(doublex[])/*定义目标函数为fun1函数*/
{
return(double)pow(x[0],2)+4*pow(x[1],2)-2*x[0]*x[1]+x[0]-3*x[1];
}
voidfun2(doublex[],doubleg_x[])/*定义求梯度函数为fun2函数*/
{
g_x[0]=(double)2*x[0]-2*x[1]+1;
g_x[1]=(double)8*x[1]-2*x[0]-3;
}
voidfun3(doubleh[],doubleg[],doubled[])/*定义求搜索方向函数为fun3函数*/
{
d[0]=-(h[0]*g[0]+h[1]*g[1]);
d[1]=-(h[2]*g[0]+h[3]*g[1]);
}
doublefun4(doublex[],doubled[])/*定义求步长函数为fun4函数*/
{
return
-(2*x[0]*d[0]+8*x[1]*d[1]-2*x[0]*d[1]-2*x[1]*d[0]+d[0]-3*d[1])
/
(2*d[0]*d[0]+8*d[1]*d[1]-4*d[0]*d[1]);
}
voidfun5(doubleh[],doublex[],doubletx[],doubleg_x[],doubletg[])
/*定义求H矩阵函数为fun5函数*/
{
doubleq[2],p[2],temp[2],th[4];
q[0]=tg[0]-g_x[0];/*第k+1个点梯度与第k个点处的梯度之差*/
q[1]=tg[1]-g_x[1];
p[0]=tx[0]-x[0];/*p(k),第k+1个点与第k个点的点差*/
p[1]=tx[1]-x[1];
th[0]=h[0];/*用临时变量储存原H矩阵的值*/
th[1]=h[1];
th[2]=h[2];
th[3]=h[3];
temp[0]=p[0]*q[0]+p[1]*q[1];
temp[1]=(q[0]*h[0]+q[1]*h[2])*q[0]
+(q[0]*h[1]+q[1]*h[3])*q[1];
h[0]=th[0]+pow(p[0],2)/temp[0]
-pow((th[0]*q[0]+th[1]*q[1]),2)/temp[1];
h[1]=th[1]+(p[0]*p[1])/temp[0]
-(th[0]*q[0]+th[1]*q[1])*(th[2]*q[0]
+th[3]*q[1])/temp[1];
h[2]=th[2]+(p[0]*p[1])/temp[0]
-(th[0]*q[0]+th[1]*q[1])*(th[2]*q[0]
+th[3]*q[1])/temp[1];
h[3]=th[3]+pow(p[1],2)/temp[0]
-pow((th[2]*q[0]+th[3]*q[1]),2)/temp[1];
}
voidmain(){
intk=1;
doublex[2]={1,1},/*计算初始点*/
tx[2]={1,1},/*临时存储计算点*/
g_x[2],/*梯度*/
tg[2],/*临时梯度*/
t,/*步长*/
d[2],/*搜索方向*/
h[4]={1,0,0,1};/*初始H矩阵*/
doublefx=0;
fun2(x,g_x);/*求梯度*/
tg[0]=g_x[0];
tg[1]=g_x[1];
fun3(h,g_x,d);/*求搜索方向*/
intcounter=1;
while(sqrt(pow(g_x[0],2)+pow(g_x[1],2))-e>0){
do{
fun3(h,tg,d);/*求搜索方向*/
t=fun4(x,d);/*求步长*/
printf("\n迭代次数为%d\n",counter++);
printf("梯度%f,%f,梯度的模%f\n",g_x[0],g_x[1],
sqrt(pow(g_x[0],2)+pow(g_x[1],2)));
printf("搜索方向%f,%f,步长%f\n",d[0],d[1],t);
printf("H矩阵\n%f,%f\n%f,%f",h[0],h[1],h[2],h[3]);
if(k=2){
tx[0]=x[0]+t*d[0];
tx[1]=x[1]+t*d[1];
}else{
tx[0]=tx[0]+t*d[0];
tx[1]=tx[1]+t*d[1];
}
fun2(tx,tg);/*求梯度*/
fun5(h,x,tx,g_x,tg);/*H矩阵*/
if(k==2){
x[0]=tx[0];
x[1]=tx[1];
}
printf("\nx[1]=%f\tx[2]=%f",x[0],x[1]);
fun2(x,g_x);
printf("\n新梯度的模:
%f",sqrt(pow(g_x[0],2)+pow(g_x[1],2)));
k++;
}while(k==2);
k=1;
}
fx=fun1(x);
printf("\n最优解为:
\n");
printf("\nx1=%f,x2=%f\n",x[0],x[1]);
printf("\n最优值为:
\n");
printf("\nf(x)=%f\n",fx);
}
法
#include<>
#include<>
#include<>
#definee
doublefun1(doublex[])/*定义目标函数为fun1函数*/
{
return(double)pow(x[0],2)+4*pow(x[1],2)-2*x[0]*x[1]+x[0]-3*x[1];
}
voidfun2(doublex[],doubleg_x[])/*定义求梯度函数为fun2函数*/
{
g_x[0]=(double)2*x[0]-2*x[1]+1;
g_x[1]=(double)8*x[1]-2*x[0]-3;
}
voidfun3(doubleh[],doubleg[],doubled[])/*定义求搜索方向函数为fun3函数*/
{
d[0]=-(h[0]*g[0]+h[1]*g[1]);
d[1]=-(h[2]*g[0]+h[3]*g[1]);
}
doublefun4(doublex[],doubled[])/*定义求步长函数为fun4函数*/
{
return
-(2*x[0]*d[0]+8*x[1]*d[1]-2*x[0]*d[1]-2*x[1]*d[0]+d[0]-3*d[1])
/
(2*d[0]*d[0]+8*d[1]*d[1]-4*d[0]*d[1]);
}
voidfun5(doubleh[],doublex[],doubletx[],doubleg_x[],doubletg[])
/*定义求H矩阵函数为fun5函数*/
{
doubleq[2],p[2],temp[2],th[4];
q[0]=tg[0]-g_x[0];/*第k+1个点梯度与第k个点处的梯度之差*/
q[1]=tg[1]-g_x[1];
p[0]=tx[0]-x[0];/*p(k),第k+1个点与第k个点的点差*/
p[1]=tx[1]-x[1];
th[0]=h[0];/*用临时变量储存原H矩阵的值*/
th[1]=h[1];
th[2]=h[2];
th[3]=h[3];
temp[0]=p[0]*q[0]+p[1]*q[1];//pt*q
temp[1]=(q[0]*h[0]+q[1]*h[2])*q[0]
+(q[0]*h[1]+q[1]*h[3])*q[1];//qt*H*q
h[0]=th[0]+(1+temp[1]/temp[0])*(pow(p[0],2)/temp[0])
-((p[0]*q[0]*h[0]+p[0]*q[1]*h[2])+(h[0]*q[0]*p[0]+h[1]*q[1]*p[0]))/temp[0];
h[1]=th[1]+(1+temp[1]/temp[0])*((p[0]*p[1])/temp[0])
-((p[0]*q[0]*h[1]+p[0]*q[1]*h[3])+(h[0]*q[0]*p[1]+h[1]*q[1]*p[1]))/temp[0];
h[2]=th[2]+(1+temp[1]/temp[0])*((p[1]*p[0])/temp[0])
-((p[1]*q[0]*h[0]+p[1]*q[1]*h[2])+(h[2]*q[0]*p[0]+h[3]*q[1]*p[0]))/temp[0];
h[3]=th[3]+(1+temp[1]/temp[0])*((p[1]*p[1])/temp[0])
-((p[1]*q[0]*h[1]+p[1]*q[1]*h[3])+(h[2]*q[0]*p[1]+h[3]*q[1]*p[1]))/temp[0];
}
voidmain(){
intk=1;
doublex[2]={1,1},/*计算初始点*/
tx[2]={1,1},/*临时存储计算点*/
g_x[2],/*梯度*/
tg[2],/*临时梯度*/
t,/*步长*/
d[2],/*搜索方向*/
h[4]={1,0,0,1};/*初始H矩阵*/
doublefx=0;
fun2(x,g_x);/*求梯度*/
tg[0]=g_x[0];
tg[1]=g_x[1];
fun3(h,g_x,d);/*求搜索方向*/
intcounter=1;
intj=0;
while(sqrt(pow(g_x[0],2)+pow(g_x[1],2))-e>0&&j<2){
do{
printf("\n迭代次数为%d\n",counter++);
fun3(h,tg,d);/*求搜索方向*/
t=fun4(x,d);/*求步长*/
printf("梯度%f,%f,梯度的模%f\n",g_x[0],g_x[1],
sqrt(pow(g_x[0],2)+pow(g_x[1],2)));
printf("搜索方向%f,%f,步长%f\n",d[0],d[1],t);
printf("H矩阵\n%f,%f\n%f,%f",h[0],h[1],h[2],h[3]);
if(k=2){
tx[0]=x[0]+t*d[0];
tx[1]=x[1]+t*d[1];
}
else{
tx[0]=tx[0]+t*d[0];
tx[1]=tx[1]+t*d[1];
}
fun2(tx,tg);/*求梯度*/
fun5(h,x,tx,g_x,tg);/*求H矩阵*/
if(k==2){
x[0]=tx[0];
x[1]=tx[1];
}
printf("\nx[1]=%f\tx[2]=%f",x[0],x[1]);
fun2(x,g_x);
printf("\n新梯度的模:
%f",sqrt(pow(g_x[0],2)+pow(g_x[1],2)));
k++;
}while(k==2);
k=1;
j++;
}
fx=fun1(x);
printf("\n最优解为:
\n");
printf("\nx1=%f,x2=%f\n",x[0],x[1]);
printf("\n最优值为:
\n");
printf("\nf(x)=%f\n",fx);
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 下降 语言