# NIntegrate gives two results for two forms of the same function. Which one to trust?

I am interested in evaluation the following integral numerically (since apparently there is no analytical solution)

$$\int dx \,x^3 \left(e^{2 i c x }-i \text{erfi}\left(\frac{x +i c }{\sqrt{2}}\right)-i e^{2 i c x} \text{erfi}\left(\frac{x -i c}{\sqrt{2}}\right)+1\right) (j_0(b x)+j_2(b x))\frac{e^{-\frac{a^2}{2}-i c x -\frac{x^2}{2}}}{2 \left(4d^2 x^2+9\right)^6}$$

And the following code

NIntegrate[x^3 (SphericalBesselJ[0, b x] + SphericalBesselJ[2, b x])/
(4 d^2 x^2 + 9)^6  1/2 E^(-(1/2) x^2 - I x c - 1/2 a^2)
(1 + E^(2 I x c) - I E^(2 I x c)
Erfi[(x - I c)/Sqrt[2]] - I Erfi[(x + I c)/Sqrt[2]]) ,
{x, 0, 8}, MaxRecursion -> 22, AccuracyGoal -> 10]


gives me, for a=3, c=6, d=0.00033, the following plot fo the value of the integral as a function of b

However, if I write $j_0(b x)+j_2(b x)=\frac{1}{b^3x^3}(\sin b x-b x \cos b x)$ and use the following code

NIntegrate[(3/b^3 (Sin[bx] - bx Cos[bx]))/(4 d^2 x^2 + 9)^6
1/2 E^(-(1/2) x^2 - I x c - 1/2 a^2)
(1 + E^(2 I x c) - I E^(2 I x c) Erfi[(x - I c)/Sqrt[2]] - I Erfi[(x + I c)/Sqrt[2]]) ,
{x, 0, 8}, MaxRecursion -> 22, AccuracyGoal -> 10]


then, for the same values a=3, c=6, d=0.00033 the plot I get is the following

In principle they look the same, but clearly the second one grows slower than the first one. I have therefore two questions: why the two integrations are giving me different results? and which one should I trust as the correct one?

Thank you very much for your advice.

EDIT: Here the comparison between the two results, namely the first one divided by the second one.

• Your code has typos/missing definitions. Otherwise I would try them with WorkingPrecision -> 20 to see if it's a numerics issue. – Michael E2 Apr 24 '16 at 22:12
• the axis are different, I cannot see that "clearly" one is different from the other. Maybe plot them together? or their ratio? – tsuresuregusa Apr 25 '16 at 3:01
• I think this is an internal issue. For example, N[SphericalBesselJ[0, 7] + SphericalBesselJ[2, 7] == (3/7^3 (Sin[7] - 7 Cos[7]))] outputs False, while N[SphericalBesselJ[0, 7] + SphericalBesselJ[2, 7] == (3/7^3 (Sin[7] - 7 Cos[7])), 5000000] outputs True. Something is definitely wrong here. – JungHwan Min Apr 25 '16 at 6:28

the culprit here is AccuracyGoal:

a = 3; c = 6; d = 0.00033; b = 2;
NIntegrate[
x^3 (SphericalBesselJ[0, b x] +
SphericalBesselJ[2, b x])/(4 d^2 x^2 + 9)^6 1/
2 E^(-(1/2) x^2 - I x c - 1/2 a^2) (1 + E^(2 I x c) -
I E^(2 I x c) Erfi[(x - I c)/Sqrt[2]] -
I Erfi[(x + I c)/Sqrt[2]]), {x, 0, 8}, MaxRecursion -> 22,
AccuracyGoal -> 10]
NIntegrate[
x^3 (SphericalBesselJ[0, b x] +
SphericalBesselJ[2, b x])/(4 d^2 x^2 + 9)^6 1/
2 E^(-(1/2) x^2 - I x c - 1/2 a^2) (1 + E^(2 I x c) -
I E^(2 I x c) Erfi[(x - I c)/Sqrt[2]] -
I Erfi[(x + I c)/Sqrt[2]]), {x, 0, 8}, MaxRecursion -> 22]


2.69768*10^-10 + 3.29575*10^-11 I

2.06439*10^-10 + 1.15352*10^-11 I

The later result agrees with your trigonometric form.

Why this happens is not clear. Interestingly AccuracyGoal->Automatic also gives the wrong result. AccuracyGoal->Infinity or any number greater than 10 yields the correct one.