1
$\begingroup$
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

$\endgroup$
3
  • 2
    $\begingroup$ 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. $\endgroup$
    – LouisB
    Commented Jul 19, 2020 at 4:42
  • $\begingroup$ @LouisB Now I got those errors i.imgur.com/HFAWZ1I.png $\endgroup$
    – NeDark
    Commented Jul 19, 2020 at 5:16
  • 2
    $\begingroup$ 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. $\endgroup$
    – LouisB
    Commented Jul 19, 2020 at 5:57

1 Answer 1

1
$\begingroup$

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}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.