0

I'm trying to create a numerical root finder in C, to find the zeros of Bessel Functions. Mathematical functions, F(x), can be deflated, by dividing the function by (x-x0), where x0 is a known root. The resulting function G(x) = F(x)/(x-x0), has all roots that F(x) had except at x0. This is useful as it prevents the same root being found twice by the solver.

When I find a root using my solver it is written to the array Roots[]. The Bessel Function is defined in math.h as double j0(double x).

I want to iteratively modify double j0(double x) so that I can have:

double my_j0(double x) = j0(x)/(x-Roots[n]);

I can then call this new, deflated function in my solver.

The above syntax doesn't make sense, but I don't know how to represent it.

I attempted to use a function pointer and defined

double (*my_j0)(double); my_j0 = &j0;

But now I don't know how to iteratively modify the function, each time dividing by the root I found with my solving algorithm.

Thanks for the help!

Rohan
  • 507
  • 1
  • 4
  • 15
  • The function `F(x)/(x - x0)` has a removable singularity at `x0`. What you really want to solve is the analytic continuation of that function across the singularity. – Kerrek SB Oct 21 '14 at 23:37
  • In essence I think what I was trying ask was this: http://stackoverflow.com/questions/1839965/dynamically-creating-functions-in-c And I think the result is what I'm trying to do is effectively impossible/very difficult in C. – Rohan Oct 22 '14 at 15:35

2 Answers2

0

you could pass it as an argument in a recursive function with a starting case. Something like this might work if your trying to solve iterativley.

double my_j0(double x, double my_root){
    if(//some base case){
        //final calculation
        return calculation
    }
    if(my_root == some_initialization){
    //do initial calculation work
        my_root = calculated_root;
    }
    else{
    //do calculations using your root
        my_root = calculated_root;
    }
    return my_j0(x, my_root)
ragingSloth
  • 1,094
  • 8
  • 22
0

After more experience, I've realized what I was really looking for was an Anonymous Function or lambda function. A great introduction as to what a lambda function is can be found on this SO question.

Unfortunately, C does not support lambda functions, however, there are language extensions supported by both GCC (using macros) and Clang compilers for implementing lambda functions which can be found on Wikipedia.

Community
  • 1
  • 1
Rohan
  • 507
  • 1
  • 4
  • 15