Professional Documents
Culture Documents
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
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 )
𝑓(𝑥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 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
𝑓(𝑥2 ) − 𝑓(𝑥1 )
𝑝𝑁1 (𝑥 ) = (𝑥 − 𝑥1 ) + 𝑓(𝑥1 )
𝑥2 − 𝑥1
𝑝𝑁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
𝑝𝑁(𝑛−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
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 last example can be solved using polyfit and polyval as follows:
The Curse of High-Dimensional Polynomials
Solution. Employing polyfit and polyval, we can solve the problem, as follows:
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.
[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 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,
𝑑𝑠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
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]
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.
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
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
1 + 0.6775𝑥
𝑓̃(𝑥 ) =
1 + 0.1862𝑥
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 + 𝑥 )?