|
Further Examples: NIntegrate
One Dimension
NIntegrate will often give the numerical value of a definite integral without parameters faster than finding the definite integral and then using N.
In[1]:= 
Out[1]= 
In[2]:= 
Out[2]= 
In[3]:= 
Out[3]= 
Functions that cannot be integrated using algebraic routines can nonetheless be integrated numerically.
In[4]:= 
Out[4]= 
In[5]:= 
Out[5]= 
NIntegrate's default option values sometimes lead to a wrong answer.
In[6]:= 

Out[6]= 
Here are the options of NIntegrate. The default value of Automatic corresponds to GaussKronrod in one dimension and to MultiDimensional in several dimensions.
In[7]:= 
Out[7]= 
Increasing the number of recursive bisections by setting the option MaxRecursion gives a more reliable result.
In[8]:= 

Out[8]= 
The previous result agrees with that obtained by Integrate.
In[9]:= 
Out[9]= 
In[10]:= 
Out[10]= 
Here is a function that NIntegrate suspects has a singularity.
In[11]:= 
Here is the plot of ff.
In[12]:= 

NIntegrate does the integration correctly.
In[13]:= 
Out[13]= 
In[14]:= 
Out[14]= 
In[15]:= 
Out[15]= 
This calculates the integral numerically to different precision goals, and accumulates the sampling points used for the integral approximations with the option EvaluationMonitor.
In[16]:= 
This plots the points that were accumulated in samplePoints. The function is sampled at the x coordinates in the order of the y coordinates.
In[17]:= 



Here is a function that oscillates rapidly.
In[18]:= 
Here is its plot.
In[19]:= 

Using NIntegrate with its default options settings gives an incorrect result.
In[20]:= 


Out[20]= 
NIntegrate gives the correct result using .
In[21]:= 
Out[21]= 
It happens that an exact result is possible and taking N verifies the previous result.
In[22]:= 
Out[22]= 
In[23]:= 
Out[23]= 
In[24]:= 
Multidimensional
Low-dimensional integration can be done either with genuine space integration rules or with a Cartesian product of one dimensional rules. We compare the two using the very simple two-dimensional function .
Here are the sample points that come from using MultiDimensional, the default for NIntegrate.
In[25]:= 

Here are the sample points from using the Cartesian product setting GaussKronrod.
In[26]:= 

Here we will use NIntegrate on a more complicated function. Here is its plot.
In[27]:= 

Here is a plot of the sample points for the numerical integration using .
In[28]:= 


For comparison, here is a plot of the sample points for the numerical integration using .
In[29]:= 

In[30]:= 
MonteCarlo
is occasionally advantageous in dimensions greater than .
In[31]:= 
Out[31]= 
Setting the option Method to MonteCarlo[24] is equivalent to using SeedRandom[24] to reset the random number generator.
In[32]:= 
Out[32]= 
You can specify how many function evaluations are used in the methods MonteCarlo and QuasiMonteCarlo by giving a value to MaxPoints (the default being ).
In[33]:= 

Out[33]= 
If the AccuracyGoal and PrecisionGoal are set to Automatic, they will be set to in the methods MonteCarlo and QuasiMonteCarlo, and to in all other methods.
|