2

I am trying to implement the secant method to find the roots of the bessel function. I first wrote some code that works it out by just using the straight up bessel function in gsl. I am now trying to change it to the error code so I can evaluate when to stop the code. This code works well for the original function:

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

In this i declare the function to be differentiated as:

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

When i try to modify it to include the bessel function with error

sectant_method(gsl_sf_bessel_J0_e, bessel_function_roots[0], 0);

it stops working. The main change i have made is:

double f (double x , gsl_sf_result *result)
{
    int fnc(x, result);
    return result->val;
}

so my function now reads

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);

Once I have implemented gsl_deriv_central it returns a segmentation core dump. I think the problem lies in how i am declaring the function in the code I have put up.

If anyone could help me out on this i would be most grateful :)

Chris U
  • 53
  • 6

0 Answers0