我正在尝试使用割线方法来查找bessel函数的根。我首先编写了一些代码,通过使用gsl中的直接bessel函数来解决这个问题。我现在正在尝试将其更改为错误代码,以便我可以评估何时停止代码。 此代码适用于原始函数:
secant_method(gsl_sf_bessel_J0, bessel_function_roots[0], 0);
在此,我将要区分的函数声明为:
double f (double x)
{
return fnc (x);
}
当我尝试修改它以包含错误的bessel函数
时sectant_method(gsl_sf_bessel_J0_e, bessel_function_roots[0], 0);
它停止工作。我所做的主要改变是:
double f (double x , gsl_sf_result *result)
{
int fnc(x, result);
return result->val;
}
所以我的功能现在读取
void secant_method(int fnc(double x , gsl_sf_result *result) , Root *bounds , int counter ){
//finding the derivative to start with
//setting up the derivative method for gsl library
double new_root, c;
double f (double x , gsl_sf_result *result)
{
return result->val;
}
gsl_function F;
F.function = &f;
F.params = 0;
long double gradient;
double abserr,error;
gsl_sf_result *upper = malloc(sizeof(gsl_sf_result));
gsl_sf_result *lower = malloc(sizeof(gsl_sf_result));
fnc(bounds->upper_bound, upper);
fnc(bounds->lower_bound , lower);
printf ("x = %.16f -- %.16f\n" ,bounds->lower_bound , bounds->upper_bound);
printf ("f(x) = %g\n",lower->val);
printf ("f(x) = %g\n",upper->val);
gsl_deriv_central (&F, bounds->lower_bound, 1e-8, &gradient, &abserr);
printf ("f'(x) = %.10f +/- %.10f\n", gradient, abserr);
一旦我实现了gsl_deriv_central,它就会返回一个分段核心转储。 我认为问题在于我如何在我提出的代码中声明函数。
如果有人能帮助我,我会非常感激:)