You are on page 1of 6

ECM6 Computational Methods :

Slide 1 of 5

Lecture 14c
Interpolating Data with Splines
Brian G. Higgins
Department of Chemical Engineering & Materials Science
University of California, Davis
April 2014, Hanoi, Vietnam

ECM6Lecture14cVietnam_2014.nb

Background
The following data represents the diffusivity of copper compounds in an ion exchange resin for various
concentrations expressed in terms of normality N (Data taken from Bradie, 2006)
In[111]:=

Out[112]=

data = 880.0521, 1.65<, 80.1028, 2.10<, 80.2036, 2.27<,


80.4946, 2.76<, 80.9863, 3.12<, 82.443, 2.92<, 85.06, 2.07<<;
Grid@Prepend@data, 8"N", "D"<D, Frame AllD
N
0.0521
0.1028
0.2036
0.4946
0.9863
2.443
5.06

D
1.65
2.1
2.27
2.76
3.12
2.92
2.07

Here is a plot of the data


In[113]:=

plt1 = ListPlot@data, PlotStyle 8PointSize@LargeD, Blue<,


PlotRange 880, 6<, 81.6`, 3.2`<<, AxesOrigin 80, 1.6`<, Frame True,
FrameLabel 8Style@"Normality H N L", 16D, Style@"Diffusivity H D L", 16D<,
AspectRatio 1, ImageSize 300D

Out[113]=

Diffusivity H D L

3.0

2.5

2.0

Normality H N L

ECM6Lecture14cVietnam_2014.nb

Analysis of the Data


Suppose we want to determine the maximum diffusivity and also measure the rate of change of diffusivity with respect to concentration. Let see if we can use Mathematicas Interpolation function.
Recall Interpolation works by fitting polynomial curves between successive data points.
In[114]:=
Out[114]=

In[115]:=

myFunc = Interpolation@dataD
InterpolatingFunction@880.0521, 5.06<<, <>D
plt2 = Plot@myFunc@xD, 8x, Min@dataD, Max@dataD<, PlotStyle 8Thick, Red<,
PlotRange 880, 6<, 81.6`, 3.6`<<, AxesOrigin 80, 1.6`<, Frame True,
FrameLabel 8Style@"Normality H N L", 16D, Style@"Diffusivity H D L", 16D<,
AspectRatio 1D;
Show@plt2, plt1, ImageSize 300D

Out[116]=

Diffusivity H D L

3.5

3.0

2.5

2.0

Normality H N L
Here is a comparison of the data with the interpolation function. Recall Interpolation works by fitting
polynomial curves between successive data points, but as the plot illustrates, there is no continuity of
the derivatives.
Recall the default value for Interpolation fits a piecewise-cubic polynomial through the data. The result is
that the derivatives of the piecewise cubic are not continuous at the data points. To summarize: the
interpolation has gone through the data but it is not a smooth curve. We can try to change the interpolation order but that will not help matters. An alternative possibility is to use a spline interpolation function.

ECM6Lecture14cVietnam_2014.nb

Mathematicas Spline Function


A spline interpolation is a form of interpolation where the interpolant is a special type of piecewise
polynomial called a spline.
Spline interpolation is preferred over polynomial interpolation because the interpolation error can be
made small even when using low degree polynomials for the spline.
Spline interpolation avoids the problem of Runge's phenomenon (oscillation at edges of a domain),
which occurs when interpolating using high degree polynomials.
Here is our data
In[82]:=

data = 880.0521, 1.65<, 80.1028, 2.10<, 80.2036, 2.27<,


80.4946, 2.76<, 80.9863, 3.12<, 82.443, 2.92<, 85.06, 2.07<<;

We use the function SplineFit to interpolate the data, using cubic splines
In[91]:=
Out[91]=

cub = SplineFit@data, CubicD


SplineFunction@Cubic, 80., 6.<, <>D

The interval of the spline is parameterized so that all the data lies between t=0 and t=6. Thus the data is
parameterized in terms of the new variable t. Hence we can plot the 2-D data using ParametricPlot
In[117]:=

ParametricPlot@cub@tD, 8t, 0, 6<, Frame True,


FrameLabel 8Style@"Normality H N L", 16D, Style@"Diffusivity H D L", 16D<,
PlotRange All, AspectRatio 1, PlotStyle 8Red, Thick<,
Epilog 8PointSize@LargeD, Blue, Map@Point@D &, dataD<,
ImageSize 300, Axes FalseD

3.0

Out[117]=

Diffusivity H D L

2.8

2.6

2.4

2.2

2.0

1.8

Normality H N L
Note that the data in the plot is the original data at the node points t=0,1,2,3,4,5,6

ECM6Lecture14cVietnam_2014.nb

In[96]:=
Out[96]=

Table@cub@iD, 8i, 0, 6<D


880.0521, 1.65<, 80.1028, 2.1<, 80.2036, 2.27<,
80.4946, 2.76<, 80.9863, 3.12<, 82.443, 2.92<, 85.06, 2.07<<

Analyzing Spline Data


From the plot we observe that the diffusivity D is a maximum near a normality of N=1.2. We can extract
out the maximum value of the diffusivity D by searching for all values of diffusivity between 4t5
In[97]:=
Out[97]=

maxValue = Max@Table@Last@cub@iDD, 8i, 4, 5, 0.005<DD


3.13436

Having the maximum value we can now determine the t value by using FindRoot on the following
function
In[98]:=

eqn@t_ ? NumericQD := Last@cub@tDD - maxValue

That is, we want to find the value of t such that D=3.13436. Here is the result
In[99]:=
Out[99]=

cub@t . FindRoot@eqn@tD, 8t, 4, 5<DD


81.1945, 3.13436<

ECM6Lecture14cVietnam_2014.nb

References
These notes are based on the topics taken from
B. Bradie, A friendly Introduction to Numerical Analysis, Pearson/Prentice Hall, 2006