1
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 error

NeDark
  • 113
  • 4
  • 2
    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 '20 at 04:42
  • @LouisB Now I got those errors https://i.imgur.com/HFAWZ1I.png – NeDark Jul 19 '20 at 05:16
  • 2
    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 '20 at 05:57

1 Answers1

1

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}]
Michael E2
  • 235,386
  • 17
  • 334
  • 747