# Unable to get numerical result by using Integrate/NIntegrate

Nfunc[x_?NumericQ] :=
E^(-x^2/2)/
Sqrt[2*Pi]*(1/
2 Erf[(x - 0.256048)/Sqrt[2*1.6^2 + 0.231313^2]/Sqrt[2]] -
1/2 Erf[-Infinity/Sqrt[2]])
nume = N[NIntegrate[x*Nfunc[x], {x, -Infinity, Infinity},
Method -> {Automatic, "SymbolicProcessing" -> 0},
PrecisionGoal -> 3, AccuracyGoal -> 3], 4]
den = N[Normal[
Integrate[Nfunc[x], {x, -Infinity, Infinity},
GenerateConditions -> False]], 4]
nume/den


I couldn't get numerator nor denominator. It keeps "executing" for hours. Is there a mistake or isn't mathematica able to perform this calculation?

I used functions with NumericQ for speeding up calculation, but no luck.

EDIT: After restarting mathematica

• Unchanged, your code executes in less than 1 second for me on MMA 12.1.0. Maybe you need to start with a fresh kernel, or restart MMA. – LouisB Jul 19 at 4:42
• @LouisB Now I got those errors i.imgur.com/HFAWZ1I.png – NeDark Jul 19 at 5:16
• From the error message, it certainly looks like Nfunc already has a definition before its SetDelayed ( := ) definition. Notice that the variable name is black instead of blue. With a fresh kernel all undefined variable names are blue. Two thoughts: First, we usually don't begin a function name with a capital letter, but that is not the problem here. Second, before defining a function we often execute a Clear[ Nfunc ] or ClearAll[ Nfunc ] to eliminate conflicts like the one shown in the error message. – LouisB Jul 19 at 5:57

## 1 Answer

While Erf can be difficult to work with, a simple, straightforward approach seems to work well:

ClearAll[Nfunc];
Nfunc[x_] :=
E^(-x^2/2)/
Sqrt[2*Pi]*(1/
2 Erf[(x - 0.256048)/Sqrt[2*1.6^2 + 0.231313^2]/Sqrt[2]] -
1/2 Erf[-Infinity/Sqrt[2]]); (* why not -1 instead of Erf[]? *)

nume = NIntegrate[x*Nfunc[x], {x, -Infinity, Infinity}]

(*  0.159712  *)

den = NIntegrate[Nfunc[x], {x, -Infinity, Infinity}]

(*  0.458961  *)

nume/den

(*  0.347986  *)


Looks right:

Plot[Nfunc[x], {x, -3, 3.7}, GridLines -> {{nume/den}, None}]