MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: NIntegrate and speed

> 1) I would like to make the following double numerical integral
> converge without errors
> R = 8000; Z = 1; rd = 3500; NIntegrate[Exp[-k Abs[Z]]/(1 + (k
> rd)^2)^1.5 (NIntegrate[Cos[k R Sin[\[Theta]]], {\[Theta], 0,
> \[Pi]}]), {k, 0, \[Infinity]}]
> It tells non numerical values present and I don't understand why,
> since it evaluates finally a numerical value? 0.000424067

many numeric functions in Mathematica try to evaluate their "body"
symbolically. In your case that will call NIntegrate with a symbolic k
which results in the messages you see. Actually these can be ignored in
that case, but of course it is nicer to avoid them, e.g. like this:

R = 8000; Z = 1; rd = 3500;
f[k_?NumericQ] :=
 NIntegrate[Cos[k R Sin[\[Theta]]], {\[Theta], 0, \[Pi]}]
 Exp[-k Abs[Z]]/(1 + (k rd)^2)^1.5 (f[k]), {k, 0, \[Infinity]}]

> 2) Isn't the second integrand a cylindrical Bessel function of order
> 0? So, I expected that NIntegrate[Exp[-k Abs[Z]]/(1 + (k rd)^2)^1.5
> BesselJZero[0, k R], {k, 0, \[Infinity]}] doing the same job. But it
> fails to converge and gives 0.00185584- i4.96939*10^-18. Trying with
> WorkingPrecision didn't make things better. How can this be fixed?

If I use the result Mathematica gives me for:

Integrate[Cos[k R Sin[\[Theta]]], {\[Theta], 0, \[Pi]}]

the result is correct:

 Exp[-k Abs[Z]]/(1 + (k rd)^2)^1.5 (\[Pi] BesselJ[0, R k]), {k,
  0, \[Infinity]}]

so I guess you just didn't choose the correct Bessel function. Of course
that approach is much faster than the nested NIntegrate...

> 3) The above Nintegrals will go into a loop and should be evaluated
> as fast as possible. How? With Compile, CompilationTarget -> "C",
> Paralleization, etc.?

NIntegrate itself can't be compiled but the integrand will usually be
compiled automatically (see the Compiled option for NIntegrate). I think
ParallelDo will be your friend, but you have of course to take care that
there are no dependencies in the evaluations of the body for different
values of the iteration variable...



  • Prev by Date: Re: Filtering data from numerical minimization
  • Next by Date: Re: NIntegrate and speed
  • Previous by thread: Re: NIntegrate and speed
  • Next by thread: Re: NIntegrate and speed