Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Integrate vs. NIntegrate in Mathematica

  1. Mar 18, 2014 #1
    I'm trying to integrate the function Exp[-I*t*x - t^2/2] from -infinity to infinity using NIntegrate in Mathematica; the value that I get is accurate when x is small, but as x gets larger, the output from NIntegrate does not match the value I get when I use Integrate -- it gets less and less accurate.
    Does anyone know why this happens and what I can do to make sure NIntegrate is giving me accurate answers? (I'm ultimately going to apply NIntegrate to a much more complicated function and I'm using this test function to figure out how to deal with the oscillatory behavior)
  2. jcsd
  3. Mar 21, 2014 #2


    User Avatar
    Gold Member

    The exact result is $$ e^{-\frac{x^2}{2}} \left(\sqrt{2 \pi }\right) $$

    Which for ##x == 10# is 4.83466*10^-22

    So you need HIGH precision to recreate that with NIntegrate. You can do that, and set a precision goal by : (In the following the first example is NOT ENOUGH PRECISION, the second is enough )

    Code (Text):
    With[{x = 10},
     NIntegrate[Exp[-I*t*x - t^2/2], {t, -\[Infinity], \[Infinity]},
      WorkingPrecision -> 20, PrecisionGoal -> 6]]
    This is not enough, and gives
    Code (Text):
    NIntegrate::ncvb: "\!\(\*
    StyleBox[\"\\\"NIntegrate failed to converge to prescribed accuracy after \\\"\", \"MT\"]\)\!\(\* StyleBox[\"9\", \"MT\"]\)\!\(\* StyleBox[\"\\\" recursive bisections in \\\"\", \"MT\"]\)\!\(\* StyleBox[\"t\", \"MT\"]\)\!\(\* StyleBox[\"\\\" near \\\"\", \"MT\"]\)\!\(\* StyleBox[ RowBox[{\"{\", \"t\", \"}\"}], \"MT\"]\)\!\(\* StyleBox[\"\\\" = \\\"\", \"MT\"]\)\!\(\* StyleBox[ RowBox[{\"{\", \"1.255585675845388`\", \"}\"}], \"MT\"]\)\!\(\* StyleBox[\"\\\". NIntegrate obtained \\\"\", \"MT\"]\)\!\(\* StyleBox[ RowBox[{ RowBox[{\"-\", \"1.0581813203458523`*^-16\"1.0581813203458523`*}], \"+\",  RowBox[{\"3.469446951953614`*^-17\"3.469446951953614`*, \" \", \"I\"}]}], \"MT\"]\)\!\(\* StyleBox[\"\\\" and \\\"\", \"MT\"]\)\!\(\* StyleBox[\"6.409147557108418`*^-16\"6.409147557108418`*, \"MT\"]\)\!\(\* StyleBox[\"\"\", \"MT\"]\) for the integral and error estimates.
    Code (Text):
    With[{x = 10},
     NIntegrate[Exp[-I*t*x - t^2/2], {t, -\[Infinity], \[Infinity]},
      WorkingPrecision -> 40, PrecisionGoal -> 6]]
    This gives
    Code (Text):
     4.834658903596599769813455011618390931572*10^{-22} +
     1.783512275559820938759410037056409076459*10^{-51} i

    So youll have to increase your working precision depending on how high "x" is.

    For x = 50, you'll have to have the working precision greater than 87 (90 works).
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook