# Integrate vs. NIntegrate in Mathematica

• Mathematica
• nnnrocks
In summary, the conversation highlights an issue with the accuracy of NIntegrate in Mathematica when dealing with large values of x. It is suggested to increase the working precision to ensure accurate results.
nnnrocks
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)

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:
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:
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:
With[{x = 10},
NIntegrate[Exp[-I*t*x - t^2/2], {t, -\[Infinity], \[Infinity]},
WorkingPrecision -> 40, PrecisionGoal -> 6]]

This gives
Code:
 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).

## 1. What is the difference between Integrate and NIntegrate in Mathematica?

Integrate is a built-in function in Mathematica that performs symbolic integration, while NIntegrate is a numerical integration function. This means that Integrate computes exact solutions to integrals using algebraic and trigonometric techniques, while NIntegrate approximates the integral using numerical methods.

## 2. When should I use Integrate and when should I use NIntegrate?

You should use Integrate when you want an exact solution to an integral, such as when working with mathematical equations or for symbolic manipulation. NIntegrate should be used when you need a numerical approximation of the integral, such as when dealing with complex functions or large datasets.

## 3. Can Integrate and NIntegrate give different results for the same integral?

Yes, Integrate and NIntegrate can give different results for the same integral. This is because Integrate uses algebraic and trigonometric techniques to compute exact solutions, while NIntegrate uses numerical methods which can introduce small errors in the approximation.

## 4. Are there any limitations to using Integrate and NIntegrate?

Yes, there are some limitations to using Integrate and NIntegrate. Integrate can only handle a limited set of functions and may not be able to find an exact solution for more complex integrals. NIntegrate may also struggle with highly oscillatory or singular functions, and may require specific options to be set in order to obtain accurate results.

## 5. Can I use both Integrate and NIntegrate in the same Mathematica code?

Yes, you can use both Integrate and NIntegrate in the same Mathematica code. In fact, it can be useful to use Integrate to find an exact solution to an integral, and then use NIntegrate to verify the result or to obtain a more accurate numerical approximation. However, it is important to keep in mind the limitations of each function and choose the appropriate one for the task at hand.

Replies
13
Views
2K
Replies
1
Views
726
Replies
34
Views
4K
Replies
12
Views
1K
Replies
58
Views
6K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
5
Views
2K
Replies
1
Views
1K
Replies
4
Views
2K