Interpolating Functions

It is often useful to use a set of data or even a theoretical calculation as a set of ordered pairs (or triplets, or quadruplets, or ...). Mathematica has the capacity to use interpolating functions where a set of ordered pairs can be handled the same way you would use an analytical function. Some of the basic commands are shown below.

1. Setting Up The Function

This section demonstrates the commands needed to create an interpolating function. First get the necessary packages. Enter this command once and only before using any other fitting commands.

Needs["FunctionApproximations`"]

Now get a table of ordered pairs. You can do this with the Table command below or read in a file.

data1 = Table[{x,(1/x)},{x,1,20,1.0}];

Create the interpolating function.

func1 = Interpolation[data1]

To see the value of the function at some point x you need to treat the interpolating function just like a standard function.

func1[2.2]

2. Using It

To display the data2 array and a curve on a single plot use the commands shown below. Notice the DisplayFunction->Identity option turns off plotting for the first two plots that have the data and curve. The last, combined-curve-and-data plot is displayed with the Show command using the DisplayFunction->$DisplayFunction option.

plot1 = ListPlot[data1,
PlotStyle->{RGBColor[1,0,0],PointSize[0.03]},
DisplayFunction->Identity]
plot2 = Plot[func1[x],{x,1,20},
PlotStyle->{Thickness[0.01],RGBColor[0,0,0]},
PlotRange->{0,1.5},
DisplayFunction->Identity,
Frame->True,
FrameLabel->{"x","f(x)","Interpolating Functions",""}]
Show[{plot2,plot1},DisplayFunction->$DisplayFunction]

Notice what happens when you go outside the range of the original set of ordered pairs. You get a bunch of warning messages, but Mathematica will still extrapolate out to the points requested. This extrapolation can be dangerous!!

plot1 = ListPlot[data1,
PlotStyle->{RGBColor[1,0,0],PointSize[0.03]},
DisplayFunction->Identity]
plot2 = Plot[func1[x],{x,0.2,2},
PlotStyle->{Thickness[0.01],RGBColor[0,0,0]},
PlotRange->{{0.0,2.5},{0,2.5}},
DisplayFunction->Identity,
Frame->True,
FrameLabel->{"x","f(x)","Interpolating Functions",""}]
Show[{plot2,plot1},DisplayFunction->$DisplayFunction]

You cal also integrate the function and do all the usual stuff.

Integrate[func1[x],{x,3,10}]