2

我正在尝试实现割线方法来查找贝塞尔函数的根。我首先编写了一些代码,通过使用 gsl 中的直接贝塞尔函数来解决它。我现在正在尝试将其更改为错误代码,以便我可以评估何时停止代码。此代码适用于原始功能:

    secant_method(gsl_sf_bessel_J0, bessel_function_roots[0], 0);

在此我声明要区分的功能为:

     double f (double x)
{
  return fnc (x);
}

当我尝试修改它以包含错误的贝塞尔函数时

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,它就会返回一个分段核心转储。我认为问题在于我如何在我提交的代码中声明函数。

如果有人可以帮助我解决这个问题,我将不胜感激:)

4

0 回答 0