You are on page 1of 26

ECE 3040

Lecture 17: Polynomial Interpolation


© Prof. Mohamad Hassoun

This lecture covers the following topics:

 Introduction
 Interpolation using a single polynomial
 Newton’s interpolation polynomials
 Matlab built-in polynomial interpolation: polyfit
 The curse of high-dimensional polynomials
 Cubic spline interpolation
 Matlab built-in cubic spline interpolation: spline
 Interpolation using rational functions
Introduction

Polynomial interpolation is a procedure for modeling a set of precise data points


using a polynomial function, 𝑝(𝑥), that fits the data exactly (passes through all
provided data points). The data points are normally obtained from a complicated
mathematical model, 𝑓 (𝑥 ), of an engineering or scientific system derived from
physical principles. Once an interpolation polynomial is computed, it can be used to
replace the complicated mathematical model for the purpose of analysis and design.
For instance, the interpolating polynomial can be used to estimate the value of the
function at a new point 𝑥′, as 𝑓(𝑥′) ≅ 𝑝(𝑥 ′ ). The solution for the coefficients of the
interpolation polynomial 𝑝(𝑥 ) can be determined by solving an associated system
of linear equations, or can be computed using formulas.

The data points used for interpolation can also be a set of very accurately measured
experimental values. In practice, if the set of data points is large, different
polynomials are used to construct a piece-wise interpolation function; this
procedure is known as spline interpolation. Rational functions may also be used for
interpolation.

This lecture will also introduces two built-in Matlab polynomial-based interpolation
functions: polyfit and spline.
Interpolation Using a Single Polynomial

A straight line can be completely defined using two points on the straight line. The
slope, 𝑎1 , and 𝑦-intercept, 𝑎0 , coefficients in the representation 𝑝(𝑥 ) = 𝑎1 𝑥 + 𝑎0
are sufficient to define the straight line. The coefficients of the straight line that
passes through the points (𝑥1 , 𝑓(𝑥1 )) and (𝑥2 , 𝑓 (𝑥2 )) are unique and are the
solution of the system of two linear equations:

𝑓 (𝑥1 ) = 𝑎1 𝑥1 + 𝑎0
𝑓 (𝑥2 ) = 𝑎1 𝑥2 + 𝑎0
or, in matrix form,
𝑥 1 𝑎1 𝑓 (𝑥1 )
[ 1 ] [𝑎 ] = [ ]
𝑥2 1 0 𝑓 (𝑥2 )

Similarly, three points are required to compute the coefficients of a parabola,


𝑝(𝑥 ) = 𝑎2 𝑥 2 + 𝑎1 𝑥 + 𝑎0 . For example, the three coefficients of the parabola that
passes through the distinct (non-collinear) points (𝑥1 , 𝑓(𝑥1 )), (𝑥2 , 𝑓(𝑥2 )) and
(𝑥3 , 𝑓 (𝑥3 )) are unique and are the solution of the system of three linear equations:

𝑓(𝑥1 ) = 𝑎2 𝑥1 2 + 𝑎1 𝑥1 + 𝑎0
𝑓(𝑥2 ) = 𝑎2 𝑥2 2 + 𝑎1 𝑥2 + 𝑎0
𝑓(𝑥3 ) = 𝑎2 𝑥3 2 + 𝑎1 𝑥3 + 𝑎0
or, in matrix form,
𝑥1 2 𝑥1 1 𝑎2 𝑓 (𝑥1 )
[𝑥2 2 𝑥2 1] [ 𝑎1 ] = [𝑓(𝑥2 )]
𝑥3 2 𝑥3 1 𝑎0 𝑓(𝑥3 )

In fact, fitting a polynomial to a set of given data points is not new to us. Recall that
we had encountered this problem in the context of parabolic interpolation-based
optimization in Lecture 14. The following is an example of second-order
polynomial interpolation.
Example. Use parabolic interpolation to approximate the function
𝑥2
𝑓 (𝑥 ) = − 2sin(𝑥)
10
from three of its points at 𝑥1 = 0, 𝑥2 = 1 and 𝑥3 = 2. Then, employ the quadratic
model to predict the value of 𝑓(𝑥 ), at 𝑥 = 1.5. Also, compute the true error.
Solution. Substituting the values of 𝑥 in the function 𝑓(𝑥) leads to the three
interpolation points: (0,0), (1, −1.583) and (2, −1.419). The coefficients of the
parabola can then be obtained (employing the above formulation) as the solution of
0 0 1 𝑎2 0
[1 1 1] [ 𝑎1 ] = [−1.583]
4 2 1 𝑎0 −1.419

The (green) plot of the interpolation parabola 𝑝(𝑥 ) = 0.874𝑥 2 − 2.457𝑥 and the
(blue) plot of 𝑓(𝑥), are shown below, within the interval [-0.5 3].

The interpolated value at 𝑥 = 1.5 is 𝑝(1.5) = −1.719, giving a true error of


𝑓(1.5) − 𝑝(1.5) = −1.770 − (−1.719) = −0.051.
The above formulation can be extended to interpolating the 𝑛 points
{ (𝑥1 , 𝑓(𝑥1 )), (𝑥2 , 𝑓(𝑥2 )), … , (𝑥𝑛 , 𝑓(𝑥𝑛 ))}
Using the an (𝑛 − 1)-order polynomial
𝑝(𝑥 ) = 𝑎𝑛−1 𝑥 𝑛−1 + ⋯ + 𝑎2 𝑥 2 + 𝑎1 𝑥 + 𝑎0
which results in the following 𝑛x𝑛 system of linear equation:
𝑎
𝑥1𝑛−1 … 𝑥12 𝑥1 1 𝑛−1 𝑓(𝑥1 )

𝑥2𝑛−1 ⋯ 𝑥22 𝑥2 1 𝑎 =[
𝑓(𝑥2 )
]
2
⋮ ⋮ ⋮ ⋮ ⋮ 𝑎1 ⋮
𝑛−1 2 𝑓(𝑥𝑛 )
[𝑥𝑛 ⋯ 𝑥𝑛 𝑥𝑛 1] [ 𝑎 ]
0

Coefficient matrices of this form are referred to as Vandermonde matrices. Matlab


can generate the Vandermonde matrix using the built-in function vander(x), where x
is a vector of the 𝑥𝑖 data points. The following is an example.

The Vandermonde matrix can become ill-conditioned (especially for large 𝑛), and
therefore the solution for the above system formulation is very sensitive to round-
off errors. The Matlab instruction cond(A) can be used to return the condition
number of a matrix A; a high valued condition number indicates a nearly singular
matrix. For example,

The next section presents an alternative, numerically robust formulation for the
polynomial interpolation problem.
Newton’s Interpolation Polynomials

There are a variety of alternative forms for expressing an interpolation polynomial


beyond the previous formulation. Newton’s interpolation polynomial is among the
most popular.
The simplest form of interpolation is to start with two points (𝑥1 , 𝑓(𝑥1 )) and
(𝑥2 , 𝑓 (𝑥2 )) and connect them with a straight line. The equation for a straight line
that passes through two points can be easily derived and is expressed as

𝑓(𝑥2 ) − 𝑓(𝑥1 )
𝑝𝑁1 (𝑥 ) = (𝑥 − 𝑥1 ) + 𝑓(𝑥1 )
𝑥2 − 𝑥1

We will refer to 𝑝𝑁1 (𝑥 ) as Newton’s linear-interpolation formula. It allows us to


approximate the value of 𝑓(𝑥) at some point 𝑥 inside [𝑥1 𝑥2 ]. The notation 𝑝𝑁1
designates that this is a first-order Newton interpolating polynomial. The nominal
form for 𝑝𝑁1 is given by
𝑝𝑁1 (𝑥 ) = 𝑏1 + 𝑏2 (𝑥 − 𝑥1 )
where the coefficients are given by
𝑓(𝑥2 ) − 𝑓(𝑥1 )
𝑏1 = 𝑓(𝑥1 ), 𝑏2 =
𝑥2 − 𝑥1
It is obvious here that the smaller the interval between the data points, the better the
approximation. Alternatively, one can increase the order of the polynomial for
increased accuracy if more data points are available. For three data points, the
second-order Newton’s interpolation polynomial, 𝑝𝑁2 (𝑥 ), is

𝑝𝑁2 (𝑥 ) = 𝑏1 + 𝑏2 (𝑥 − 𝑥1 ) + 𝑏3 (𝑥 − 𝑥1 )(𝑥 − 𝑥2 )

with coefficients
𝑓 (𝑥3 ) − 𝑓(𝑥2 ) 𝑓(𝑥2 ) − 𝑓(𝑥1 )
𝑓(𝑥2 ) − 𝑓 (𝑥1 ) −
𝑥3 − 𝑥2 𝑥2 − 𝑥1
𝑏1 = 𝑓(𝑥1 ), 𝑏2 = , 𝑏3 =
𝑥2 − 𝑥1 𝑥3 − 𝑥1
Notice that the coefficients 𝑏1 and 𝑏2 are the same in the second and first-order
Newton’s polynomials. This means that if two points are given and a first order
Newton’s polynomial is fitted to pass through these points and then a third point is
added, the polynomial 𝑝𝑁2 would then be computed as

𝑝𝑁2 = 𝑝𝑁1 + 𝑏3 (𝑥 − 𝑥1 )(𝑥 − 𝑥2 )


and only one new coefficient (𝑏3 ) would need to be computed. This observation is
also true for higher order Newton polynomials. It is interesting to note that 𝑏3 is the
finite-difference, second-derivative approximation of 𝑓 ′′ (𝑥3 ).

The general form for Newton’s (𝑛 − 1)-order interpolating polynomial on the 𝑛


data points {(𝑥1 , 𝑓 (𝑥1 )), (𝑥2 , 𝑓 (𝑥2 )), … , (𝑥𝑛 , 𝑓(𝑥𝑛 ))}, is given by

𝑝𝑁(𝑛−1) (𝑥 ) = 𝑏1 + 𝑏2 (𝑥 − 𝑥1 ) + 𝑏3 (𝑥 − 𝑥1 )(𝑥 − 𝑥2 ) + ⋯ +
𝑏𝑛 (𝑥 − 𝑥1 )(𝑥 − 𝑥2 ) … (𝑥 − 𝑥𝑛−1 )
where the 𝑏𝑖 constants are evaluated directly (and efficiently) from the data points,
using the formulas:
𝑏1 = 𝑓(𝑥1 )
𝑓(𝑥2 ) − 𝑏1
𝑏2 =
𝑥2 − 𝑥1
𝑓 (𝑥3 ) − 𝑓(𝑥2 )
− 𝑏2
𝑥3 − 𝑥2
𝑏3 =
𝑥3 − 𝑥1
𝑓 (𝑥4 ) − 𝑓(𝑥3 ) 𝑓(𝑥3 ) − 𝑓(𝑥2 )

𝑥4 − 𝑥3 𝑥3 − 𝑥2
− 𝑏3
𝑥4 − 𝑥2
𝑏4 =
𝑥4 − 𝑥1
and so on.
Your turn: Deduce the formula for 𝑏5 .
Example. Solve the last example using a second-order Newton’s interpolation
polynomial. Again, the three interpolation points are (0,0), (1, −1.583) and
(2, −1.419).
Solution. The 𝑏 coefficients are:

𝑏1 = 𝑓(𝑥1 ) = 𝑓(0) = 0
𝑓 (𝑥2 ) − 𝑏1 −1.583 − 0
𝑏2 = = = −1.583
𝑥2 − 𝑥1 1−0
𝑓 (𝑥3 ) − 𝑓(𝑥2 )
− 𝑏2 −1.419 − (−1.583) − (−1.583)
𝑥3 − 𝑥2 2−1
𝑏3 = = = 0.874
𝑥3 − 𝑥1 2−0

which lead to the interpolation polynomial,


𝑝𝑁2 (𝑥 ) = 0 − 1.583(𝑥 − 0) + 0.874(𝑥 − 0)(𝑥 − 1)
= −1.583𝑥 + 0.874𝑥 (𝑥 − 1) = 0.874𝑥 2 − 2.457𝑥

The interpolated value at 𝑥 = 1.5 is 𝑝𝑁2 (1.5) = −1.719. These results are
consistent with those obtained based on solving the Vandermonde matrix
formulation (refer to the previous example).
The coefficients of the Newton polynomial can also be computed systematically by
solving (using forward substitution utilizing the “\” operator) the system of
equations shown below. Your turn: Use it to find {𝑏1 , 𝑏2 , 𝑏3 } for the last example.
1 0 0 0 … 0 𝑏1 𝑓(𝑥1 )
1 (𝑥2 − 𝑥1 ) 0 0 … 0 𝑏2 𝑓(𝑥2 )
1 (𝑥3 − 𝑥1 ) (𝑥3 − 𝑥1 )(𝑥3 − 𝑥2 ) 0 … 0 𝑏3 𝑓(𝑥3 )
=
. . . . . .
. . . . . .
[1 (𝑥𝑛 − 𝑥1 ) (𝑥𝑛 − 𝑥1 )(𝑥𝑛 − 𝑥2 ) … (𝑥𝑛 − 𝑥1 )(𝑥𝑛 − 𝑥2 )(… )(𝑥𝑛 − 𝑥𝑛−1 )] [𝑏𝑛 ] [𝑓(𝑥𝑛 )]
Function newtint is a Matlab implementation of Newton’s interpolation.

The following is an application of the function newtint to the solution of the last
example:

Your turn: Rewrite the above function using the matrix formulation shown at the
end of the last page. Email your Matlab function to the grader before the due date
for Assignment 11.
Matlab Built-in Polynomial Interpolation: polyfit

For the case where the number of data points is equal to the number of coefficients
of the fit polynomial, the built-in Matlab function polyfit performs interpolation
(polyfit can also be used to solve regression problems, as discussed later in Lecture
18). So, for interpolation, this function returns the coefficients of the (𝑛 − 1)-
degree polynomial that passes through 𝑛 data points.

The basic function-call is p=polyfit(x,y,m), where x is a vector of 𝑛 independent


values, y is the corresponding vector of 𝑛 dependent values and 𝑚 = 𝑛 − 1. The
function returns the coefficient vector of the (𝑛 − 1)-order interpolation
polynomial.

The last example can be solved using polyfit and polyval as follows:
The Curse of High-Dimensional Polynomials

When the data set is large, a high-order interpolating polynomial is required.


Interpolating with high-order polynomials should be avoided, because they tend to
be highly sensitive to round-off error and may also lead to overfitting. The later
phenomenon is illustrated in the next example. The next section introduces spline
interpolation as a way to avoid the overfitting problem encountered when
interpolating from large number of data points.

Example. Generate three sets of 5, 7 and 11 equidistant points within


1
[-1 1] from the function 𝑓(𝑥 ) = . For each data set, plot the function, its
1+25𝑥 2
interpolation polynomial and the interpolation points on the same graph.

Solution. Employing polyfit and polyval, we can solve the problem, as follows:

For 5 data points the solution is:

[𝑓(𝑥) is the plot in red]


Similarly, for 7 data points the solution is:

And for 11 data points, the solution is:

As can be seen from the above plots, as the degree of the interpolation polynomial
increases, the interpolation accuracy deteriorates significantly for points near the
edges of the interval.
Cubic Spline Interpolation

When a set of 𝑛 data points is given and a single (𝑛 − 1)-degree polynomial is used
for interpolation, then the polynomial gives the exact values at the points (passes
through the points) and gives estimates (interpolated) values between the points.
When the number of points is small, the order of the polynomial is low and, typically
that leads to reasonably accurate interpolation. However, as already demonstrated in
the last example, large interpolation errors (overfitting) may occur when a high-
degree polynomial is used for interpolation.

When a large number of points is involved, a better interpolation can be obtained


using multiple low-order polynomials (linear, quadratic or cubic) instead of a single
high-order polynomial. Here, each polynomial is valid in one interval between two
or several points. Typically, all polynomials are of the same degree, but each has
different coefficients. Interpolation in this way is called piecewise or spline
interpolation. For example, quadratic polynomials employed to connect each pair of
adjacent points are called quadratic splines. These polynomials can be constructed so
that the connections (knots) between adjacent quadratic equations are visually
smooth. The concept of the spline originated from the drafting technique of using a
thin, flexible tool (called spline) to draw smooth curves through a set of points. See
below for a picture of a typical spline drafting tool.
Linear splines. The notation used for splines is presented in the following figure.
For 𝑛 data points, there are 𝑛 − 1 intervals. Each interval 𝑖 has its own spline
function, 𝑠𝑖 , resulting in a total of 𝑛 − 1 splines.

[Figure courtesy: Applied Numerical Methods with Matlab, 2nd edition, S. C. Chapra (McGraw-Hill, 2008)]

With linear splines, interpolation is carried out by using a straight line between
adjacent points. The 𝑖th spline can then be expressed by Newton’s linear interpolation
formula
𝑓(𝑥𝑖+1 ) − 𝑓(𝑥𝑖 )
𝑠𝑖 (𝑥 ) = (𝑥 − 𝑥𝑖 ) + 𝑓 (𝑥𝑖 ), 𝑖 = 1, 2, … , 𝑛 − 1
𝑥𝑖+1 − 𝑥𝑖
The interpolated value at a given point 𝑥 ′ is computed by simply “looking up” the
interval 𝑖 that 𝑥′ belongs to, and then evaluating 𝑠𝑖 (𝑥 ′ ).

Example. Fit the five data points in the following table with linear splines. Then,
interpolate at 𝑥 = 3.5 and 5.
𝑖 𝑥𝑖 𝑓(𝑥𝑖 )
1 3.0 2.5
2 4.5 1.0
3 7.0 2.5
4 9.0 0.5
Solution. The data is plotted in the following figure and the three linear splines are
shown.

The equations for the 𝑛 − 1 linear splines are:


1.0 − 2.5
𝑠1 (𝑥 ) = (𝑥 − 3.0) + 2.5 = −𝑥 + 5.5
4.5 − 3
2.5 − 1.0
𝑠2 (𝑥 ) = (𝑥 − 4.5) + 1.0 = 0.6𝑥 − 1.7
7.0 − 4.5
0.5 − 2.5
𝑠3 (𝑥 ) = (𝑥 − 7) + 2.5 = −𝑥 + 9.5
9.0 − 7.0

The first interpolation point 𝑥 = 3.5 belongs to the first interval, [3.0 4.5], so we
employ spline 𝑠1 to solve for the interpolated value of the function: 𝑠1 (3.5) =
−3.5 + 5.5 = 2.

The second interpolation point 𝑥 = 5.0 belongs to the second interval, [4.5 7.0], so
we employ spline 𝑠2 to solve for the interpolated value of the function: 𝑠2 (5.0) =
0.6(5.0) − 1.7 = 1.3.
Cubic splines. Visual inspection of the above figure indicates that linear spline
interpolation is not smooth. At the data point at which two adjacent splines meet
(called a knot) the slope changes abruptly. This implies that the derivative is not
continuous at the knot, which violates the practical assumption that the underlying
function being interpolated is smooth. This deficiency is overcome by using higher
order polynomial splines that would allow for the first derivative at the knot to be
continuous. For improved smoothness, we may also require that the second
derivative is continuous at each knot. These requirements can’t be achieved with a
linear spline, since a straight line does not have enough parameters (coefficients) to
allow for such flexibility. Therefore, higher order polynomial splines must be used.

The following derivation capture the spirit of the spline method. Here we seek the
lowest-order polynomial spline 𝑠𝑖 , that passes through the two interpolating points
(𝑥𝑖 , 𝑓(𝑥𝑖 )) and (𝑥𝑖+1 , 𝑓(𝑥𝑖+1 )), have a slope (first derivative) equal to the slope of
spline 𝑠𝑖−1 at 𝑥𝑖 and have a concavity (second derivative) that is equal to that of
spline 𝑠𝑖−1 , at 𝑥𝑖 . Mathematically, we require that spline 𝑠𝑖 satisfy all of the
following four constraints,
𝑠𝑖 (𝑥𝑖 ) = 𝑓(𝑥𝑖 )
𝑠𝑖 (𝑥𝑖+1 ) = 𝑓(𝑥𝑖+1 )
𝑑𝑠𝑖 (𝑥𝑖 ) 𝑑𝑠𝑖−1 (𝑥𝑖 )
=
𝑑𝑥 𝑑𝑥
𝑑2 𝑠𝑖 (𝑥𝑖 ) 𝑑2 𝑠𝑖−1 (𝑥𝑖 )
=
𝑑𝑥 2 𝑑𝑥 2

The polynomial that satisfies these four constraints must have at least four degrees
of freedom (coefficients). That would be a 3rd-order polynomial, or a cubic.
Therefore, 𝑠𝑖 has the form,

𝑠𝑖 (𝑥 ) = 𝑎𝑖,3 𝑥 3 +𝑎𝑖,2 𝑥 2 + 𝑎𝑖,1 𝑥 + 𝑎𝑖,0 , 𝑖 = 1, 2, … , 𝑛 − 1

(Note that in order to fit 𝑛 data points, we would require 𝑛 − 1 splines.)


In order to compute all 4(𝑛 − 1) spline coefficients, we would need to solve
4(𝑛 − 1) equations. The computation of the cubic splines is illustrated next for
three data points 𝑥1 , 𝑥2 , 𝑥3 . For three points, we need [𝑛 − 1 = 3 − 1 = 2] two
cubic splines:

𝑠1 (𝑥 ) = 𝑎1,3 𝑥 3 +𝑎1,2 𝑥 2 + 𝑎1,1 𝑥 + 𝑎1,0 (spline between 𝑥1 and 𝑥2 )


𝑠2 (𝑥 ) = 𝑎2,3 𝑥 3 +𝑎2,2 𝑥 2 + 𝑎2,1 𝑥 + 𝑎2,0 (spline between 𝑥2 and 𝑥3 )

whose first and second derivatives are given by, respectively,

𝑑𝑠1 (𝑥 )
= 3𝑎1,3 𝑥 2 +2𝑎1,2 𝑥 + 𝑎1,1
𝑑𝑥
𝑑2 𝑠1 (𝑥 )
= 6𝑎1,3 𝑥+2𝑎1,2
𝑑𝑥 2
𝑑𝑠2 (𝑥 )
= 3𝑎2,3 𝑥 2 +2𝑎2,2 𝑥 + 𝑎2,1
𝑑𝑥
𝑑2 𝑠2 (𝑥 )
= 6𝑎2,3 𝑥+2𝑎2,2
𝑑𝑥 2

In order to satisfy the four constraints, we would need to solve eight equations. Let
us write down these equations. Each spline must pass through two end data points,
resulting in the four equations

𝑠1 (𝑥1 ) = 𝑎1,3 𝑥13 +𝑎1,2 𝑥12 + 𝑎1,1 𝑥1 + 𝑎1,0 = 𝑓(𝑥1 ) (1)


𝑠1 (𝑥2 ) = 𝑎1,3 𝑥23 +𝑎1,2 𝑥22 + 𝑎1,1 𝑥2 + 𝑎1,0 = 𝑓(𝑥2 ) (2)
𝑠2 (𝑥2 ) = 𝑎2,3 𝑥23 +𝑎2,2 𝑥22 + 𝑎2,1 𝑥2 + 𝑎2,0 = 𝑓(𝑥2 ) (3)
𝑠2 (𝑥3 ) = 𝑎2,3 𝑥33 +𝑎2,2 𝑥32 + 𝑎2,1 𝑥3 + 𝑎2,0 = 𝑓(𝑥3 ) (4)
The first derivatives (also the second derivatives) of splines 𝑠1 (𝑥) and 𝑠2 (𝑥), at the
interior knot (𝑥2 ) must be equal, resulting in two additional equations:
3𝑎1,3 𝑥22 +2𝑎1,2 𝑥2 + 𝑎1,1 = 3𝑎2,3 𝑥22 +2𝑎2,2 𝑥2 + 𝑎2,1
6𝑎1,3 𝑥2 +2𝑎1,2 = 6𝑎2,3 𝑥2 +2𝑎2,2
Or, equivalently,
3𝑎1,3 𝑥22 +2𝑎1,2 𝑥2 + 𝑎1,1 − 3𝑎2,3 𝑥22 −2𝑎2,2 𝑥2 − 𝑎2,1 = 0 (5)
3𝑎1,3 𝑥2 +𝑎1,2 −3𝑎2,3 𝑥2 −𝑎2,2 = 0 (6)

So far, we have a total of six equations and we need two more equations. These two
additional equations can be obtained in different ways, depending on how we want
the end splines 𝑠1 and 𝑠𝑛−1 to be constrained at the endpoints, 𝑥1 and 𝑥𝑛 ,
respectively. One common approach is to provide user specified first derivatives,
𝑓′(𝑥1 ) and 𝑓′(𝑥𝑛 ). For example, specifying zero derivatives 𝑓 ′ (𝑥1 ) = 𝑓 ′ (𝑥𝑛 ) = 0
forces the end splines to have flat tails. This specification (termed clamped-end-
condition) leads to the last two equations (for the present case of 𝑛 = 3),
3𝑎1,3 𝑥12 +2𝑎1,2 𝑥1 + 𝑎1,1 = 𝑓 ′ (𝑥1 ) = 0 (7)
3𝑎2,3 𝑥32 +2𝑎2,2 𝑥3 + 𝑎2,1 = 𝑓 ′ (𝑥3 ) = 0 (8)

The solution for the cubic spline coefficients, for the three data points problem,
becomes the solution of the following 8x8 matrix system [Equations (1)-(8)]
𝑥13 𝑥12 𝑥1 1 0 0 0 0 𝑎 𝑓(𝑥1 )
1,3
𝑥23 𝑥22 𝑥2 1 0 0 0 0 𝑎1,2 𝑓(𝑥2 )
0 0 0 0 𝑥23 𝑥22 𝑥2 1 𝑎1,1 𝑓(𝑥2 )
0 0 0 0 𝑥33 𝑥32 𝑥3 1 𝑎1,0 𝑓(𝑥3 )
=
3𝑥22 2𝑥2 1 0 −3𝑥22 −2𝑥2 −1 0 𝑎2,3 0
3𝑥2 1 0 0 −3𝑥2 −1 0 0 𝑎2,2 0
𝑎2,1 ′( )
𝑓 𝑥1
3𝑥12 2𝑥1 1 0 0 0 0 0
[𝑎2,0 ] [𝑓 ′ (𝑥3 )]
[ 0 0 0 0 3𝑥32 2𝑥3 1 0]
The above cubic spline formulation can be extended to 𝑛 points.
Example. Fit the data in the following table using cubic splines. Assume spline
derivatives at the end points, 𝑥1 and 𝑥3 , to be 0. Then, interpolate at 𝑥 = 3.5 and 5.

𝑖 𝑥𝑖 𝑓(𝑥𝑖 )
1 3.0 2.5
2 4.5 1.0
3 7.0 2.5

The cubic spline formulation applied to the above data leads to the system of linear
equations (verify it),

27 9 3 1 0 0 0 0 𝑎1,3 2.5
91.125 20.25 4.5 1 0 0 0 0 𝑎1,2 1.0
0 0 0 0 91.125 20.25 4.5 1 𝑎1,1 1.0
0 0 0 0 343 49 7 𝑎
1 1,0 2.5
𝑎 =
60.75 9 1 0 −60.75 −9 −1 0 2,3 0
13.5 1 0 0 −13.5 −1 0 0 𝑎2,2 0
27 6 1 0 0 0 0 0 𝑎2,1 0
[ 0 0 0 0 147 14 1 ] 𝑎
0 [ 2,0 ] [ 0]

Matlab left-division solution:


Therefore, the two fitted splines are
𝑠1 (𝑥 ) ≅ 0.622𝑥 3 − 7.200𝑥 2 + 26.400𝑥 − 28.700
𝑠2 (𝑥 ) ≅ −0.288𝑥 3 + 5.088𝑥 2 − 28.896𝑥 + 54.244

The plot for the interpolating cubic spline is shown below.

The first interpolation point, 𝑥 = 3.5, belongs to the first interval, [3.0 4.5], so we
employ spline 𝑠1 to solve for the interpolated value of the function: 𝑠1 (3.5) =
2.1682. (Note: By retaining more decimal digits for the coefficients of 𝑠1 (𝑥) leads
to the more accurate value of 2.1778). Similarly, the second interpolation point,
𝑥 = 5.0, belongs to the second interval, [4.5 7.0], so we employ spline 𝑠2 to solve
for the interpolated value of the function: 𝑠2 (5.0) = 0.9640.
Matlab Built-in Cubic Spline Interpolation: spline

Matlab has several built-in functions that implement piecewise interpolation. The
spline function performs cubic spline interpolation. spline has the general syntax
ys = spline(x,y,xx), where x and y are vectors containing the data points and ys is a
vector containing the results of the spline interpolation evaluated at the points of
vector xx.
By default, the spline function uses a “not-a-knot” condition. Here, the idea is to
force the continuity of the third derivative at the second and the next-to-last knots.
This introduces the two additional equations needed by the preceding formulation.
With this method, the first derivative constraints at the first and the last data points
are not enforced.
On the other hand, if y contains two more points than x has entries, then the first and
last values of y are used as the derivatives at the end points 𝑥1 and 𝑥𝑛 , respectively,
leading to the clamped-end-condition formulation described earlier.
Verification of last example:
Example. Consider the interpolation problem employing the nine data points
(𝑥−2)(2𝑥+1)
generated by evaluating the function 𝑓(𝑥 ) = at the following nine 𝑥
1+𝑥 2
1 1
values, [−5 − 3 − 1 − 0 1 3 6]. Plot the 8-degree polynomial interpolation
2 3
solution (use polyfit function), and compare it (graphically) to cubic spline
interpolating (employ the spline function). Try both clamped-end and not-a-knot-
end options.

Solution with “clamped-end” condition:


x=[-5 -3 -1 -1/2 0 1/3 1 3 6];
y=(x-2).*(2*x+1)./(1+x.^2);
xx=linspace(-5,6);
yy=(xx-2).*(2*xx+1)./(1+xx.^2);
p=polyfit(x,y,8);
y8=polyval(p,xx);
yc=[0 y 0];
ys=spline(x,yc,xx);
plot(x,y,’or’,xx,ys,xx,y8,xx,yy)
axis([-5 6 -3 3]);

Cubic Spline vs. Polynomial Interpolation

𝑓(𝑥 ) (red). Spline interpolation (blue). Polynomial interpolation (green).


Effect of cubic spline end condition:

x=[-5 -3 -1 -1/2 0 1/3 1 3 6];


y=(x-2).*(2*x+1)./(1+x.^2);
xx=linspace(-5,6);
yy=(xx-2).*(2*xx+1)./(1+xx.^2);
ys1=spline(x,y,xx);
yc=[0 y 0];
ys2=spline(x,yc,xx);
plot(x,y,’or’,xx,ys2,xx,ys1,xx,yy)
axis([-5 6 -3 3]);

The following graph compares the two spline solutions. Here, since 𝑓(𝑥) is flat at
its left and right ends, the clamped-end condition leads to a more appropriate
interpolation solution.

Spline interpolation with the clamped-end condition (blue), and with the
not-a-knot end condition (green).
Matlab has a convenient utility for generating and visualizing cubic splines. It
employs the not-a-knot end condition by default. First, the user generates a plot of
the data.

Next, from the Figure Window menu, the user would select Tools>Basic Fitting
and then choose the “spline interpolant” option. The following screen capture
shows the results using the data from the previous example. The red trace is the
exact function and the blue plot is the cubic spline fit (to the data points shown as
red circles). This blue trace is identical to the green trace in the above figure.
Interpolation Using Rational Functions

As we have demonstrated earlier, interpolation with a polynomial can be


problematic when a large number of points is used. Polynomials also lack the
ability to realize horizontal asymptotes and/or vertical ones (singularities). A
rational function of the form
𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + ⋯ + 𝑎𝑛 𝑥 𝑛
̃
𝑓 (𝑥 ) =
1 + 𝑏1 𝑥 + 𝑏2 𝑥 2 + ⋯ + 𝑏𝑚 𝑥 𝑚
can easily realize asymptotes. Rational functions can also be much less oscillatory.
Therefore, we should expect rational functions to be superior interpolants to
polynomials.

We have encountered such rational functions (in Lecture 9) in connection with Padé
approximation, where the 𝑓̃(𝑥 ) parameters (coefficients 𝑎𝑖 , 𝑏𝑖 ) were obtained by
solving a system of equations. There, we assumed that the function being
approximated, 𝑓(𝑥), is known and that the system of equations for the coefficients
was obtained by setting: 𝑓̃(0) = 𝑓(0), 𝑓̃̇(0) = 𝑓̇(0), 𝑓̃̈(0) = 𝑓̈(0), etc.

Rational functions with 𝑁 parameters can also be used for interpolating 𝑁 points.
The following is a simple example that illustrates the method of rational function
interpolation.

Example. Interpolate the points (0,1), (0.5, √1.5) and (1, √2) [obtained from
𝑓(𝑥 ) = √1 + 𝑥 ] using the rational function

𝑎0 + 𝑎1 𝑥
𝑓̃(𝑥 ) =
1 + 𝑏1 𝑥
Rewriting the equation we obtain

𝑓̃(𝑥 )(1 + 𝑏1 𝑥 ) = 𝑎0 + 𝑎1 𝑥
or,
𝑎0 + 𝑥𝑎1 − 𝑥𝑓̃(𝑥 )𝑏1 = 𝑓̃(𝑥 )

Evaluating the last equation at (𝑥, 𝑓̃(𝑥 )) = (0,1) we obtain 𝑎0 = 1. The remaining
two parameters are obtained as the solution of the following set of two linear
algebraic equations generated from the points (0.5, √1.5) and (1, √2), as follows,
0.5𝑎1 − 0.5√1.5𝑏1 = √1.5 − 1
𝑎1 − √2𝑏1 = √2 − 1

The solution is 𝑎1 ≅ 0.6775 and 𝑏1 ≅ 0.1862 leading to

1 + 0.6775𝑥
𝑓̃(𝑥 ) =
1 + 0.1862𝑥

Note that this function has a horizontal asymptote (as, 𝑥 → ∞) at 0.6775/0.1862 ≅


1
3.639 and a singularity at 𝑥 = − ≅ −5.3710 (vertical asymptote due to a zero
0.1862
denominator). The interpolation solution is depicted in the following figures.

Note that 𝑓̃(𝑥 ) is only accurate for 𝑥 ∈ [0 2].

Your turn: Approximate the function 𝑓(𝑥 ) = ln(1 + 𝑥) employing the four-
parameter rational function
𝑎1 𝑥 + 𝑎2 𝑥 2
𝑓̃(𝑥 ) =
1 + 𝑏1 𝑥 + 𝑏2 𝑥 2

1 1 3
over the interval ≤ 𝑥 ≤ 2. Choose the 𝑥 values { , 1, , 2} when performing
2 2 2
interpolation. Plot 𝑓(𝑥 ) and 𝑓̃(𝑥 ) for 𝑥 ∈ [−0.85 5]. Does 𝑓̃(𝑥 ) have a singularity
close to 𝑥 = −1, that matches the singularity in ln(1 + 𝑥 )?

You might also like