get_min.c

来自「speech signal process tools」· C语言 代码 · 共 65 行

C
65
字号
/*Given the range xmin to xmax and the number of points N on the range( including the end points ) and the function name for computing thevalue of the function, find the minimum value of the function and thex that produces that minimum.Assume only one minimum in range and no maxima except at the ends of the range.*/get_min(xmin,xmax,N,function,min_x,min_y,level,flag)int N,level,flag;double xmin,xmax,*min_x,*min_y;double (*function)();{	double x,del_x,y;	double new_xmin,new_xmax;	double n_min_x,n_min_y;	int i,s_i;	del_x=(xmax-xmin)/(N-1);	*min_y= 1e30;	for(i=0;i<N;i++)		{		x=xmin+i*del_x;		y=(*function)(x);		printf("x=%g y=%g\n",x,y);		if(*min_y>y)			{			/* function decreasing */			*min_y=y;			*min_x=x;			s_i=i;/* save position in table of minimum */			}		else			{			if(i && !flag)				/* function increasing */				break;			}		}	if(level>1)		{		if(s_i==0)			{			new_xmin=xmin;			new_xmax=xmin+del_x;			}		else if(s_i==(N-1))			{			new_xmin=xmax-del_x;			new_xmax=xmax;			}		else			{			new_xmin=xmin+(s_i-1)*del_x;			new_xmax=xmin+(s_i+1)*del_x;			}	get_min(new_xmin,new_xmax,N,function,&n_min_x,&n_min_y,level-1,flag);		if(n_min_y<*min_y)			{			*min_y=n_min_y;			*min_x=n_min_x;			}		}	return;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?