NIntegrate
NIntegrate[
f
,
x
,
xmin
,
xmax
] gives a numerical approximation to the integral .
Multidimensional integrals can be specified, as in Integrate. NIntegrate tests for singularities at the end points of the integration range. NIntegrate[
f
,
x
,
,
, ... ,
] tests for singularities at each of the intermediate points . If there are no singularities, the result is equivalent to an integral from to . You can use complex numbers to specify an integration contour in the complex plane. The following options can be given: NIntegrate usually uses an adaptive algorithm, which recursively subdivides the integration region as needed. In one dimension, GaussPoints specifies the number of initial points to choose. The default setting for GaussPoints is Floor[WorkingPrecision/3]. In any number of dimensions, MinRecursion specifies the minimum number of recursive subdivisions to try. MaxRecursion gives the maximum number. NIntegrate usually continues doing subdivisions until the error estimate it gets implies that the final result achieves either the AccuracyGoal or the PrecisionGoal specified. The default setting for PrecisionGoal is usually equal to the setting for WorkingPrecision minus 10 digits. If an explicit setting for MaxPoints is given, NIntegrate uses quasi Monte Carlo methods to get an estimate of the result, sampling at most the number of points specified. The default setting for PrecisionGoal is taken to be 2 in this case. You should realize that with sufficiently pathological functions, the algorithms used by NIntegrate can give wrong answers. In most cases, you can test the answer by looking at its sensitivity to changes in the setting of options for NIntegrate. N[Integrate[ ... ]] calls NIntegrate for integrals that cannot be done symbolically. NIntegrate has attribute HoldAll. Possible settings for Method are GaussKronrod, DoubleExponential, Trapezoidal, Oscillatory, MultiDimensional, MonteCarlo, and QuasiMonteCarlo. GaussKronrod and MultiDimensional are adaptive methods. MonteCarlo and QuasiMonteCarlo are randomized methods, appropriate for highdimensional integrals. See the Mathematica book: Section 1.6.2, Section 3.9.1, Section 3.9.2, Section 3.9.3. See also Implementation NotesA.9.44.17MainBookLinkOldButtonDataA.9.44.17. See also: NDSolve, NSum. Related packages: NumericalMath`ListIntegrate`, NumericalMath`CauchyPrincipalValue`, NumericalMath`GaussianQuadrature`.
Further Examples
Functions that cannot be integrated using algebraic routines can nonetheless be integrated numerically.
In[1]:=
Out[1]=
There are some functions that will give NIntegrate trouble, using the default values.
In[2]:=
NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 7 recursive bisections in x near x = 0.00750411.
Out[2]=
In[3]:=
Out[3]=
In this case, increasing the number of recursive bisections that can be performed will help to give a more reliable result. (The warning message here can be disregarded; although the integrand is oscillatory, the Oscillatory setting for the Method option is inappropriate because it is meant for integrands that are products of a certain type.)
In[4]:=
NIntegrate::slwcon: Numerical integration converging too slowly; suspect singularity, value of the integration is 0, oscillatory integrand, or insufficient WorkingPrecision. If your integrand is oscillatory try using the option Method>Oscillatory in NIntegrate.
NIntegrate::slwcon: Numerical integration converging too slowly; suspect singularity, value of the integration is 0, oscillatory integrand, or insufficient WorkingPrecision. If your integrand is oscillatory try using the option Method>Oscillatory in NIntegrate.
Out[4]=
With the default settings for all options, NIntegrate misses the peak in near x = 0, and gives the wrong answer for the integral.
In[5]:=
NIntegrate::ploss: Numerical integration stopping due to loss of precision. Achieved neither the requested PrecisionGoal nor AccuracyGoal; suspect highly oscillatory integrand, or the true value of the integral is 0. If your integrand is oscillatory try using the option Method>Oscillatory in NIntegrate.
Out[5]=
With the option MinRecursion
>
3, NIntegrate samples enough points to detect the peak around . With the default setting of MaxRecursion, however, NIntegrate cannot use enough sample points to be able to expect an accurate answer.
In[6]:=
NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 7 recursive bisections in x near x = 7.8125.
Out[6]=
With this setting of MaxRecursion, NIntegrate can get an accurate answer for the integral.
In[7]:=
Out[7]=
Another way to solve the problem is to make NIntegrate break the integration region into several pieces, with a small piece that explicitly covers the neighborhood of the peak.
In[8]:=
Out[8]=
The Method option lets you choose the method used to perform the integration. The methods implemented are GaussKronrod, DoubleExponential, Trapezoidal, MultiDimensional, MonteCarlo and QuasiMonteCarlo; the default value Automatic corresponds to GaussKronrod in one dimension and to MultiDimensional in several. Here we use the MonteCarlo method, which is occasionally advantageous in dimensions greater than 1.
In[9]:=
Out[9]=
Saying Method>MonteCarlo[24] is equivalent to using SeedRandom[24] to reset the random number generator, then running the integral as in the preceding example.
In[10]:=
Out[10]=
You can specify how many function evaluations are used in the methods MonteCarlo and QuasiMonteCarlo by giving a value to MaxPoints (the default being 50000).
In[11]:=
NIntegrate::mccnv: The integral failed to converge after 5000 iterations.
Out[11]=
If the AccuracyGoal and PrecisionGoal are set to Automatic, they will be set to 2 in the methods MonteCarlo and QuasiMonteCarlo, and to 6 in all the other methods.






