# How to force Mathematica to throw an error for NIntegrate

Consider this:

NIntegrate[BesselJ[2, x], {x, 0, Infinity}]


0.9999999999904574

This is the correct answer.

Now:

NIntegrate[BesselJ[2, x], {x, 0, 40000}]


NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in x near {x} = {78.125}. NIntegrate obtained -0.03826923022638286 and 0.03996030542184427 for the integral and error estimates. >>

Out= -0.03826923022638286

The answer is not correct but it's fine because MMA throws an error.

Now this:

NIntegrate[BesselJ[2, x], {x, 0, 50000}]


0.002477644409437118

Incorrect answer and no error. How can I get an error in these situations?

## 3 Answers

To get the warning

Raise the PrecisionGoal:

NIntegrate[BesselJ[2, x], {x, 0, 50000}, PrecisionGoal -> 10]


NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in x near {x} = {97.6563}. NIntegrate obtained -0.00247946 and 0.004932015473174114 for the integral and error estimates. >>

(*  -0.00247946  *)


Or raise the number of "Points":

NIntegrate[BesselJ[2, x], {x, 0, 50000}, Method -> {"LevinRule", "Points" -> 11}]


NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in x near {x} = {97.6563}. NIntegrate obtained 0.029764272686778833and 0.029476870687760864 for the integral and error estimates. >>

(*  0.0297643  *)


Some fixes

Raise recursion:

NIntegrate[BesselJ[2, x], {x, 0, 50000}, PrecisionGoal -> 10, MaxRecursion -> 20]

(*  1.00248  *)


Or raise the number of "Points" further:

NIntegrate[BesselJ[2, x], {x, 0, 50000}, Method -> {"LevinRule", "Points" -> 101}]

(*  1.00248  *)


Or use infinite intervals:

NIntegrate[BesselJ[2, x], {x, 0, Infinity}] -
NIntegrate[BesselJ[2, x], {x, 50000, Infinity}]

(*  1.00248  *)


How to detect

Users are warned that the error estimator can be tricked in tutorial/NIntegrateIntegrationRules#81420002. Some possible checks one might try are: The default PrecisionGoal seems to be about 6. Raising this to 8 or 10 might lead to a significantly different result when the numerical integration has not yet converged. I usually first try WorkingPrecision -> 20, which simultaneously raises PrecisionGoal to 10. In a highly oscillatory integral like this one (many periods over a finite interval), one has to worry about the sampling being sufficiently fine. Note that infinite intervals are handled better in this case; I'm not sure what NIntegrate does differently, but integrals to infinity probably require a different sort of handling.

This looks like a bug. May be another branch cut bug. So, Mathematica can't throw an error, since it does not know that it is a bug.

sol = Integrate[BesselJ[2, x], x];
N[Limit[sol, x -> 50000] - Limit[sol, x -> 0]]
(* 1.00248   *)


The problem is in the NIntegrate when it evaluated the limits.

 sol = N[Integrate[BesselJ[2, x], {x, 0, 50000}]]
(*1.00248*)

sol = NIntegrate[BesselJ[2, x], {x, 0, 50000}]
(*0.00247764*)


Version 10.02, windows.

Please report this to support@wolfram.com

Just devide it into two integration ranges.

NIntegrate[BesselJ[2, x], {x, 0, 100, 50000}]

(*   1.00248   *)