-2

How can I integrate an equation including bessel functions numerically from "0" to "infinity" in Fortran or/and C? I did in matlab, but it's not true for larger inputs and after a specific values , the bessel functions give completely wrong results(there is a restriction in Matlab)

U3F
  • 3
  • 4
  • yes , I tried in matlab, but I can't reach the exact results in matlab, I have an equation, but the website do not allow me to upload the image of that. – U3F Mar 19 '15 at 13:36
  • Is the question about the integration or about the bessel function? You made sure that there is no analytic solution of the integral, didn´t you? – DrSvanHay Aug 30 '17 at 09:19
  • @DrSvanHay There is no analytical solution to my knowledge, so the question would be about the integration. Of course, if any of you is aware of analytical solutions, that would be awesome – johnhenry Aug 30 '17 at 09:51
  • @johnhenry So what if F(x) ? – DrSvanHay Aug 30 '17 at 10:01
  • @DrSvanHay I did not write explicitly the form of F(x) because it is complicated to explain here its origins, very much related to the specific problem I have. What I can say is that F(x) is not oscillatory in itself, it's a rather mild function – johnhenry Aug 30 '17 at 10:04
  • @johnhenry I just asked cause I wanted to check for an analytic solution but of course the would require an analytic form of F(x) :-) Anyway I would strongly recommend disclosure of F(x). The probability of getting a useful answer is close to zero anyway but without F(x) my guess would be that said probability is even closer to zero. – DrSvanHay Aug 30 '17 at 10:22
  • Have you looked at GSL integration and Bessel functions libraries? https://www.gnu.org/software/gsl/manual/html_node/Numerical-Integration.html and https://www.gnu.org/software/gsl/manual/html_node/Bessel-Functions.html. Furthermore, if your function can be expressed as F(x) = G(x) x and a->inf, you can change it into Hankel transform. – Firman Aug 30 '17 at 10:22
  • @Firman It is indeed true that my function can be expressed in such a way that the whole integral is a Hankel transform. What would you recommend then to use? – johnhenry Aug 30 '17 at 10:44
  • p.s. part of F(x) comes a previous numerical integration, I only have it as an histogram – johnhenry Aug 30 '17 at 10:45
  • You can see this link: https://www.gnu.org/software/gsl/manual/html_node/Discrete-Hankel-Transform-Functions.html#Discrete-Hankel-Transform-Functions – Firman Aug 30 '17 at 12:05

3 Answers3

1

There's a large number of analytic results for various integrals of the Bessel functions (see DLMF, Sect. 10.22), including definite integrals over precisely this range. You'd be much better off, and almost certainly faster and more accurate, trying hard to recast your expression into something that's integrable and using an exact result.

Norman Gray
  • 11,978
  • 2
  • 33
  • 56
1

Last time I had to do with such things, it was state of the art to do simple integration of the intervals defined by the zero crossings. That is in most cases relatively stable and if the integrand is approaching zero reasonable fast easy to do.

As a starting point for playing around I´ve included a bit of code. Of course you need to work on the convergence detection and error checking. This is no production code but I thought maybe it provides a starting point for you. Its using gsl.

On my iMac this code takes about 2 µs per iteration. It will not become faster by including a hardcoded table for the intervals.

I hope this is of some use for you.

#include <iostream>
#include <vector>
#include <gsl/gsl_sf_bessel.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_sf.h>


double f (double x, void * params) {
    double y = 1.0 / (1.0 + x) * gsl_sf_bessel_J0 (x);
    return y;
}

int main(int argc, const char * argv[]) {

    double sum = 0;
    double delta = 0.00001;
    int max_steps = 1000;
    gsl_integration_workspace * w = gsl_integration_workspace_alloc (max_steps);

    gsl_function F;
    F.function = &f;
    F.params = 0;

    double result, error;
    double a,b;
    for(int n=0; n < max_steps; n++)
    {
        if(n==0)
        {
            a = 0.0;
            b = gsl_sf_bessel_zero_J0(1);
        }
        else
        {
            a = n;
            b = gsl_sf_bessel_zero_J0(n+1);
        }
        gsl_integration_qag (&F,  // function
                              besselj0_intervals[n],   // from
                              besselj0_intervals[n+1],   // to
                              0,   // eps absolute
                              1e-4,// eps relative
                              max_steps,
                              GSL_INTEG_GAUSS15,
                              w,
                              &result,
                              &error);
        sum += result;

        std::cout << n << " " << result << " " << sum << "\n";

        if(abs(result) < delta)
            break;
    }
    return 0;
}
DrSvanHay
  • 1,170
  • 6
  • 16
0

You can pretty much google and find lots of Bessel functions implemented in C already.

http://www.atnf.csiro.au/computing/software/gipsy/sub/bessel.c

http://jean-pierre.moreau.pagesperso-orange.fr/c_bessel.html

https://msdn.microsoft.com/en-us/library/h7zkk1bz.aspx

In the end, these use the built in types and will be limited to the ranges they can represent (just as MATLAB is). At best, expect 15 digits of precision using double precision floating point representation. So, for large numbers, they will appear to be rounded. eg. 1237846464123450000000000.00000

And, of course, others on Stack Overflow have looked into it.

C++ Bessel function for complex numbers

Community
  • 1
  • 1
LawfulEvil
  • 2,267
  • 24
  • 46
  • No need for external libraries. They are part of standard C and standard Fortran. – Vladimir F Героям слава Mar 19 '15 at 13:39
  • The request is vague but talked about the exact results being wrong. I thought he might be seeking to implement/use arbitrary precision numbers (like https://gmplib.org/ ) and utilize them in a homespun bessel functions to achieve "exact" results and would need source for some of the built in functions. – LawfulEvil Mar 19 '15 at 13:46
  • after a specific larger inputs in matlab , the bessel functions can't converge to a constant value, and the results are going to be completely wrong after that specific value. – U3F Mar 19 '15 at 13:51
  • Bessel functions don't converge to constant values. – agentp Mar 23 '15 at 13:36
  • if you know how to write you own, do it. don't use external libs, you have no idea about the convergence or numerical stability. grab Ab & Stegun and start hacking. – μολὼν.λαβέ Feb 26 '16 at 05:27