# My NIntegrate expression returns a wildly inaccurate value

I am trying to integrate a function using NIntegrate:

a1 = 0.00221856
b = 0.0990293
h = 100.0
a1 NIntegrate[Exp[-b * (z-h)^2]* z, {z, 0, Infinity}, AccuracyGoal -> 4]

(* Out: 6.202e-25 *)


I have also computed the same integral analytically and the answer is 1.25. In Mathematica, if I replace the limits of integration as {z, 0, 200} in place of {z, 0, Infinity}, then I get the right answer. I am confused as to what is going on.

• please post valid Mathematica code. The above hard to read. What is h = 100.0 i11 = a1xNintegrate? and a1xNintegrate ? – Nasser Mar 22 '16 at 7:19
• Please also format your code and check your spelling (Nintegrate is misspelled; a1xNintegrate should be a1*NIntegrate, etc). – MarcoB Mar 22 '16 at 10:15

Here is the function you are integrating:

Plot[Exp[-b*(z - h)^2]*z, {z, 0, 200}, PlotRange -> All] Again for reference, your integral can be calculated analytically, as you mentioned yourself:

a1 = Rationalize[0.00221856, 0];
b = Rationalize[0.0990293, 0];
h = 100;
a1 Integrate[Exp[-b*(z - h)^2]*z, {z, 0, Infinity}]
N[%]

(* Out:
-(13866/25) Sqrt[(2 Pi)/4951465] (-2 + GammaRegularized[-(1/2), 990293/1000])
1.24958
*)


Numerically, once you remove the AccuracyGoal option you have in your code, NIntegrate works, but returns a warning:

a1 = 0.00221856;
b = 0.0990293;
h = 100.0;
a1 NIntegrate[Exp[-b*(z - h)^2]*z, {z, 0, Infinity}] (* Out: 1.24958 *)


The warning suggests that the routine needs to perform more bisections than the default value of MaxRecursion will allow.

The natural approach will then be to increase the value of MaxRecursion. The following, for example, executes immediately and without warnings to yield the correct value:

a1 NIntegrate[Exp[-b*(z - h)^2]*z, {z, 0, Infinity}, MaxRecursion -> 20]

(* Out: 1.24958 *)


Notice that AccuracyGoal can be difficult to use; for instance, the "Details and Options" section of the documentation for AccuracyGoal mentions that "In most cases, you must set WorkingPrecision to be at least as large as AccuracyGoal." In this case, even setting a much higher working precision does not solve the problem.