Re: NIntegrate and speed
- To: mathgroup at smc.vnet.net
- Subject: [mg116784] Re: NIntegrate and speed
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Mon, 28 Feb 2011 04:59:22 -0500 (EST)
- References: <ikd5v4$pt1$1@smc.vnet.net>
Hi, > > 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]}] NIntegrate[ 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: NIntegrate[ 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... hth, albert