www.pudn.com > Climbing-algorithm_C_swf.zip > 爬山算法.cpp, change:2011-05-31,size:763b


#include<stdio.h> 
 
#define R 0.001   //R步长 
  
double f(double x,double y) 
{ 
	return(1/(x*x+y*y+2)); 
} 
     
double fx(double x,double y) 
{ 
	return(-2*x/((x*x+y*y+2)*(x*x+y*y+2))); 
} 
     
double fy(double x,double y) 
{ 
	return(-2*y/((x*x+y*y+2)*(x*x+y*y+2))); 
} 
     
double fxy(double x,double y) 
{ 
	return((4*(x*x+y*y))/((x*x+y*y+2)*(x*x+y*y+2)*(x*x+y*y+2)*(x*x+y*y+2))); 
} 
 
int main()  
{ 
	double x,y,z,e;    //z当前最大值,e精度 
	printf("输入起始点(x,y):"); 
	scanf("%lf%lf",&x,&y);//(x,y)为起始点 
	printf("输入精度e:"); 
	scanf("%lf",&e);//e为精度 
	z=f(x,y); 
	while(fxy(x,y)>e) 
	{ 
		x=x+R*fx(x,y); 
		y=y+R*fy(x,y); 
		z=f(x,y); 
	} 
	printf("\n最大值对应点为:(%lf,%lf)\n",x,y); 
	printf("最大值为:%lf\n",z); 
	return 0; 
}