Numerical Calculus Package

The functions defined in the context provide support for finding numerical solutions to calculus-related problems.

This loads the package.
In[1]:=
Click for copyable input

Numerical Calculation of Limits

The built-in function Limit computes limits using symbolic and analytic methods. The function NLimit contained in the NumericalCalculus package works by numerically evaluating a short sequence of function values as the argument approaches the specified point. The result of this calculation is passed to a routine that uses either Wynn's -algorithm or a generalized Euler transformation to find an approximation to the limit.

NLimit[expr,x->x0]numerically finds the limit as x approaches

Computing numerical limits.

Here is the numerical computation of a limit.
In[2]:=
Click for copyable input
Out[2]=
The limit gives the number known as Euler's gamma.
In[3]:=
Click for copyable input
Out[3]=
You can also compute limits as approaches infinity.
In[4]:=
Click for copyable input
Out[4]=
option name
default value
WorkingPrecisionMachinePrecisionnumber of digits of precision to be used
Scale1initial step size
Terms7total number of terms generated in the sequence
MethodEulerSummethod of evaluation, either EulerSum or
WynnDegree1degree to use in the Wynn method

Options for NLimit.

There are many specialized techniques for accelerating the convergence of a series. Faster convergence means that in a numerical evaluation fewer terms are needed to give a result of the desired accuracy. One such technique, Euler's transformation, is used by EulerSum.

EulerSum is particularly good for working with alternating series and series whose terms have the form where is a polynomial in . It is a useful adjunct to the built-in NSum, which is designed to be an effective general case algorithm.

EulerSum[f,{i,imin,Infinity}]uses Euler's transformation to numerically evaluate

Numerical evaluation of sums using Euler's transformation.

option name
default value
WorkingPrecisionMachinePrecisionnumber of digits of precision to be used
Terms5total number of terms generated before extrapolation
ExtraTerms7number of terms to be used in the extrapolation process; must be at least 2
EulerRatioAutomaticthe fixed ratio to be used in the transformation

Options for EulerSum.

This uses EulerSum to compute a numerical approximation to the sum of an alternating series.
In[5]:=
Click for copyable input
Out[5]=
Increasing the precision and the number of terms does not change the answer in the first six decimal places. This provides a check on the accuracy of the numerically computed result.
In[6]:=
Click for copyable input
Out[6]=
The result is also in good agreement with the known exact result.
In[7]:=
Click for copyable input
Out[7]=

There are occasionally instances in which it is difficult or impossible to analytically compute the derivative of a function. In these cases you can compute the derivative using the function ND instead of the built-in function D.

ND[f,x,x0]gives a numerical approximation to evaluated at the point 
ND[f,{x,n},x0]gives the derivative

Numerical computation of derivatives.

Here is a numerical approximation of the derivative of at .
In[8]:=
Click for copyable input
Out[8]=
After increasing the setting of the option Terms, the difference between the symbolically computed result and the numerical result for a third derivative is quite small.
In[9]:=
Click for copyable input
Out[9]=
This is the error in the numerical derivative using Cauchy's integral formula.
In[10]:=
Click for copyable input
Out[10]=
option name
default value
WorkingPrecisionMachinePrecisionnumber of digits of precision to be used
Scale1size of steps in the evaluation
Terms7total number of terms generated in the sequence
MethodEulerSummethod of evaluation, either EulerSum (numerical limit of difference quotients) or NIntegrate (Cauchy's integral formula)

Options for ND.

You should realize that with sufficiently pathological examples, the algorithms used for EulerSum, NLimit, or ND can give wrong answers. In most cases, you can test your answer by looking at the sensitivity to changes in the setting of options.

Numerical Calculation of Residues

The Mathematica function Residue symbolically finds the residue of an expression at a point in the complex plane. Because it is symbolic in nature, it is sometimes unable to get a result.

NResidue is the numerical version of Residue. It works by numerically integrating around a small circle centered at the point at which the residue is being sought. The obvious problem with this approach is that it in fact finds the sum of the residues at all of the points contained within the circle. By making the radius of the circle sufficiently small, you can exclude all singularities but the one in question.

NResidue[expr,{x,x0}]numerically find the residue of expr at the point

Numerical evaluation of residues.

Radiusspecifies the radius of the circle around which the integration is performed
PrecisionGoalthe PrecisionGoal to be used by NIntegrate
WorkingPrecisionthe WorkingPrecision to be used by NIntegrate

Options for NResidue.

Find the residue of at the origin.
In[2]:=
Click for copyable input
Out[2]=
Define an expression whose residue you will find.
In[3]:=
Click for copyable input
Out[3]=
Find the residue. Strictly speaking, has no singularity at , but it has one very near to 1.7.
In[4]:=
Click for copyable input
Out[4]=
Numerically find the residue.
In[5]:=
Click for copyable input
Out[5]=
This is another way to find the residue.
In[6]:=
Click for copyable input
Out[6]=

Numerical Calculation of Series Expansions

The Mathematica function Series finds a power series expansion of a function about a point by evaluating derivatives. However, sometimes you only want a numerical approximation to a series and the derivatives are difficult to evaluate. You could use ND to evaluate the derivatives, but that would involve repeating most of the work (i.e., evaluating the function itself at several points) for each derivative. If the function is analytic and can be evaluated in the complex plane, the solution is to use the NSeries.

NSeries[f,{x,x0,n}]gives a numerical approximation to the series expansion of f about including the terms through

Numerically finding series expansions.

NSeries works by evaluating the function at discrete points on a circle centered at the expansion point. It then uses InverseFourier to express the function as a sum of orthogonal basis functions, each of which is trivial to integrate around the circle. By Cauchy's integral formula it is seen that InverseFourier essentially gives us the required series coefficients; very little additional work is required and NSeries does it all for you. Because it is based on Cauchy's integral formula, care should be taken to ensure that no singularities of the function occur inside the circle of sample points.

option name
default value
WorkingPrecisionMachinePrecisionprecision of the arithmetic to use in calculations
Radius1radius of the circle on which the function is sampled

Options for NSeries.

This gives an approximation to the series expansion of about the origin. Note that NSeries is unable to recognize small numbers that should in fact be zero.
In[2]:=
Click for copyable input
Out[2]=
Chop and Rationalize can often clean up the result.
In[3]:=
Click for copyable input
Out[3]=
If the radius is too small, cancellation becomes a problem.
In[4]:=
Click for copyable input
Out[4]=
Increasing the WorkingPrecision will usually solve this problem.
In[5]:=
Click for copyable input
Out[5]=
If the radius is too large, the sample points become too scattered and it starts looking like poles exist.
In[6]:=
Click for copyable input
Out[6]=
NSeries also recognizes poles.
In[7]:=
Click for copyable input
Out[7]=
New to Mathematica? Find your learning path »
Have a question? Ask support »