Approximate Functions and Interpolation

In many kinds of numerical computations, it is convenient to introduce approximate functions. Approximate functions can be thought of as generalizations of ordinary approximate real numbers. While an approximate real number gives the value to a certain precision of a single numerical quantity, an approximate function gives the value to a certain precision of a quantity which depends on one or more parameters. Mathematica uses approximate functions, for example, to represent numerical solutions to differential equations obtained with NDSolve, as discussed in "Numerical Differential Equations".

Approximate functions in Mathematica are represented by InterpolatingFunction objects. These objects work like the pure functions discussed in "Pure Functions". The basic idea is that when given a particular argument, an InterpolatingFunction object finds the approximate function value that corresponds to that argument.

The InterpolatingFunction object contains a representation of the approximate function based on interpolation. Typically it contains values and possibly derivatives at a sequence of points. It effectively assumes that the function varies smoothly between these points. As a result, when you ask for the value of the function with a particular argument, the InterpolatingFunction object can interpolate to find an approximation to the value you want.

Interpolation[{f1,f2,...}]construct an approximate function with values at successive integers
Interpolation[{{x1,f1},{x2,f2},...}]
construct an approximate function with values at points

Constructing approximate functions.

Here is a table of the values of the sine function.
In[1]:=
Click for copyable input
Out[1]=
This constructs an approximate function which represents these values.
In[2]:=
Click for copyable input
Out[2]=
The approximate function reproduces each of the values in the original table.
In[3]:=
Click for copyable input
Out[3]=
It also allows you to get approximate values at other points.
In[4]:=
Click for copyable input
Out[4]=
In this case the interpolation is a fairly good approximation to the true sine function.
In[5]:=
Click for copyable input
Out[5]=

You can work with approximate functions much as you would with any other Mathematica functions. You can plot approximate functions, or perform numerical operations such as integration or root finding.

If you give a non-numerical argument, the approximate function is left in symbolic form.
In[6]:=
Click for copyable input
Out[6]=
Here is a numerical integral of the approximate function.
In[7]:=
Click for copyable input
Out[7]=
Here is the same numerical integral for the true sine function.
In[8]:=
Click for copyable input
Out[8]=
A plot of the approximate function is essentially indistinguishable from the true sine function.
In[9]:=
Click for copyable input
Out[9]=

If you differentiate an approximate function, Mathematica will return another approximate function that represents the derivative.

This finds the derivative of the approximate sine function, and evaluates it at .
In[10]:=
Click for copyable input
Out[10]=
The result is close to the exact one.
In[11]:=
Click for copyable input
Out[11]=

InterpolatingFunction objects contain all the information Mathematica needs about approximate functions. In standard Mathematica output format, however, only the part that gives the domain of the InterpolatingFunction object is printed explicitly. The lists of actual parameters used in the InterpolatingFunction object are shown only in iconic form.

In standard output format, the only part of an InterpolatingFunction object printed explicitly is its domain.
In[12]:=
Click for copyable input
Out[12]=
If you ask for a value outside of the domain, Mathematica prints a warning, then uses extrapolation to find a result.
In[13]:=
Click for copyable input
Out[13]=

The more information you give about the function you are trying to approximate, the better the approximation Mathematica constructs can be. You can, for example, specify not only values of the function at a sequence of points, but also derivatives.

Interpolation[{{{x1},f1,df1,ddf1,...},...}]
construct an approximate function with specified derivatives at points

Constructing approximate functions with specified derivatives.

This interpolates through the values of the sine function and its first derivative.
In[14]:=
Click for copyable input
Out[14]=
This finds a better approximation to the derivative than the previous interpolation.
In[15]:=
Click for copyable input
Out[15]=

Interpolation works by fitting polynomial curves between the points you specify. You can use the option InterpolationOrder to specify the degree of these polynomial curves. The default setting is InterpolationOrder->3, yielding cubic curves.

This makes a table of values of the cosine function.
In[16]:=
Click for copyable input
This creates an approximate function using linear interpolation between the values in the table.
In[17]:=
Click for copyable input
Out[17]=
The approximate function consists of a collection of straight-line segments.
In[18]:=
Click for copyable input
Out[18]=
With the default setting InterpolationOrder->3, cubic curves are used, and the function looks smooth.
In[19]:=
Click for copyable input
Out[19]=

Increasing the setting for InterpolationOrder typically leads to smoother approximate functions. However, if you increase the setting too much, spurious wiggles may develop.

ListInterpolation[{{f11,f12,...},{f21,...},...}]
construct an approximate function from a two-dimensional grid of values at integer points
ListInterpolation[list,{{xmin,xmax},{ymin,ymax}}]
assume the values are from an evenly spaced grid with the specified domain
ListInterpolation[list,{{x1,x2,...},{y1,y2,...}}]
assume the values are from a grid with the specified grid lines

Interpolating multidimensional arrays of data.

This interpolates an array of values from integer grid points.
In[20]:=
Click for copyable input
Out[20]=
Here is the value at a particular position.
In[21]:=
Click for copyable input
Out[21]=
Here is another array of values.
In[22]:=
Click for copyable input
To interpolate this array you explicitly have to tell Mathematica the domain it covers.
In[23]:=
Click for copyable input
Out[23]=

ListInterpolation works for arrays of any dimension, and in each case it produces an InterpolatingFunction object which takes the appropriate number of arguments.

This interpolates a three-dimensional array.
In[24]:=
Click for copyable input
The resulting InterpolatingFunction object takes three arguments.
In[25]:=
Click for copyable input
Out[25]=

Mathematica can handle not only purely numerical approximate functions, but also ones which involve symbolic parameters.

This generates an InterpolatingFunction that depends on the parameters and .
In[26]:=
Click for copyable input
Out[26]=
This shows how the interpolated value at 2.2 depends on the parameters.
In[27]:=
Click for copyable input
Out[27]=
With the default setting for InterpolationOrder used, the value at this point no longer depends on .
In[28]:=
Click for copyable input
Out[28]=

In working with approximate functions, you can quite often end up with complicated combinations of InterpolatingFunction objects. You can always tell Mathematica to produce a single InterpolatingFunction object valid over a particular domain by using FunctionInterpolation.

This generates a new InterpolatingFunction object valid in the domain 0 to 1.
In[29]:=
Click for copyable input
Out[29]=
This generates a nested InterpolatingFunction object.
In[30]:=
Click for copyable input
Out[30]=
This produces a pure two-dimensional InterpolatingFunction object.
In[31]:=
Click for copyable input
Out[31]=
FunctionInterpolation[expr,{x,xmin,xmax}]
construct an approximate function by evaluating expr with x ranging from to
FunctionInterpolation[expr,{x,xmin,xmax},{y,ymin,ymax},...]
construct a higher-dimensional approximate function

Constructing approximate functions by evaluating expressions.

New to Mathematica? Find your learning path »
Have a question? Ask support »