Interpolation
Interpolation
Interpolation
1 Introduction
In this chapter, we will discuss the problem of approximating a function f on some bounded interval
x ∈ [a, b] by a simpler function p. This problem comes in two flavours that are similar enough to be
treated in a unified way.
The first flavour is the case where the function f is only given implicitly, or has a very complicated form
that is not useful for practical considerations. In this case, one possibility for approximating the function
is to choose distinct points xi ∈ [a, b] and compute yi := f (xi ). Then we try to find a simpler function p
on [a, b] such that p(xi ) = yi for all xi .
The second flavour of the approximation problem is the setting where the function f itself is unknown,
and only distinct measurement data yi = f (xi ) of the function at points xi are available. In contrast to
the first flavour, we are here typically not free to choose the placement of the measurement points xi or
their number.
Still, we end up in both cases with the problem
Interpolation problem.
Given n + 1 points (xi , yi )ni=0 . Find a function p(x) out of a given class of functions satisfying the
interpolation condition
p(xi ) = yi , i = 0, . . . , n. (1)
The solution p(x) is called the interpolator, the xi values are called nodes, and the points (xi , yi )
interpolation points.
xi 0 2/3 1
,
yi 1 1/2 0
which we want to approximate with a quadratic polynomial p. The resulting interpolation polynomial can
be computed as
p(x) = (−3x2 − x + 4)/4.
The y-values of this example are chosen such that yi = cos (πxi /2). So p2 (x) can be considered as an
approximation to cos (πx/2) on the interval [0, 1] by a quadratic polynomial.
Content of this note. In this note, we will consider the cases of polynomial interpolation and spline
interpolation.
For polynomial interpolation, we will discuss the following:
• Method: How to compute the polynomials?
• Existence and uniqueness results.
• Error analysis: If the polynomial is used to approximate a function, how good is the approximation?
• Improvements: If the nodes xi can be chosen freely, how should we do it in order to reduce the
error?
For splines we will discuss:
• What are splines?
• Implementation and usage in Python.
2 Polynomial Interpolation
Let us start with some useful notation and facts about polynomials.
• A polynomial of degree n is given by
pn (x) = cn xn + cn−1 xn−1 + · · · + c1 x1 + c0 , ci ∈ R, i = 0, 1, . . . , n, with cn ̸= 0. (2)
• Pn is the set of all polynomials of degree at most n.
• C m [a, b] is the vector space of all continuous functions that have continuous first m derivatives.
• The value r is a root or a zero of a polynomial p if p(r) = 0.
• A nonzero polynomial of degree n can never have more than n roots in R (there may be fewer).
• A polynomial of degree n with n real roots r1 , r2 , . . . , rn can be written as
n
Y
pn (x) = c(x − r1 )(x − r2 ) · · · (x − rn ) = cn (x − ri ).
i=1
2.1 Methods
In this section, we present three techniques for finding the interpolation polynomial for a given set of
data.
The direct approach. For a polynomial of degree n the interpolation condition (1) is a linear systems
of n + 1 equations in n + 1 unknowns:
n
X
xij ci = yj , j = 0, . . . , n.
i=0
If we are basically interested in the polynomials themself, given by the coefficients ci , i = 0, 1, . . . , n, this
is a perfectly fine solution. It is also the strategy implemented in Python’s interpolation routines within
the polynomial package (see "https://numpy.org/doc/stable/reference/routines.polynomials.html").
2
If we actually want to solve this problem by hand, we have to solve the following linear system:
c0 = 3,
c0 + c1 + c2 + c3 + c4 = 8,
c0 + 3c1 + 9c2 + 27c3 + 81c4 = 6,
c0 + 4c1 + 16c2 + 64c3 + 256c4 = −1,
c0 + 7c1 + 49c2 + 343c3 + 2401c4 = 2.
We obtain this system by inserting the given points (xi , yi ) into the interpolation condition
c0 + c1 x + c2 x2 + c3 x3 + c4 x4 = y.
As we can see already from this matrix, the involved numbers become large rather rapidly as the number
of points and the degree of the polynomial increase.
The system becomes even worse if we move the points in this example by 100 to the right, that is, if we
want to interpolate the points
xi 100 101 103 104 107
yi 3 8 6 −1 2
instead. In this case, the entry in the fifth column and fifth row of the resulting matrix would be
1074 = 131079601. In fact, it is not possible to perform any sensible numerical computations with this
matrix: rounding errors at the level of the machine accuracy will be amplified massively and will actually
influence the numerical solution of the linear system in a noticeable manner (in technical terms, we have
that the resulting matrix is ill-conditioned). Thus, we have to modify our approach somewhat.
Fortunately, there is a rather straightforward way of mitigating some of these problems: We can move the
interpolation points ourselves into a "nicer" interval, for instance the interval [−1, 1]. To that end, we
define a := mini xi and b := maxi xi and use the shift of variables
a+b b−a
xi = + ti
2 2
or
2 b+a
ti = xi − .
b−a b−a
This moves all the nodes xi into the interval [−1, 1]. Then we can compute the interpolation polynomial q
through the points (ti , yi )ni=0 . Since all the nodes lie between −1 and +1, all the entries in the required
matrix (all of which are powers of the nodes) lie between −1 and +1 as well. From the polynomial q,
we can then, if we want to, get back to an interpolation polynomial p through the points (xi , yi )ni=0 by
reversing the shift of variables:
2 b + a
p(x) = q(t) = q x− .
b−a b−a
This strategy of moving the interpolation points to [−1, 1] is also implemented in numpy. Calling the
function Polynomial.polynomial.fit actually results in the computation of the polynomial q. In order to
move the polynomial back to the original interval, we have to use the method convert.
3
Lagrange interpolation. In this course, polynomial interpolation will be used as a basic tool to
construct other algorithms, in particular for integration. In that case, the direct approach is not the most
convenient option, so we concentrate on a different strategy, which essentially makes it possible to just
write up the polynomials.
For n + 1 points xi , i = 0, . . . , n, with distinct values xi ̸= xj if i ̸= j, the associated cardinal functions
are defined by:
n
Y x − xj x − x0 x − xi−1 x − xi+1 x − xn
ℓi (x) = = ··· · ··· , i = 0, . . . , n.
xi − xj xi − x0 xi − xi−1 xi − xi+1 xi − xn
j=0,j̸=i
(x − 1)(x − 3) 1 4
ℓ0 (x) = = x2 − x + 1
(0 − 1)(0 − 3) 3 3
(x − 0)(x − 3) 1 2 3
ℓ1 (x) = =− x + x
(1 − 0)(1 − 3) 2 2
(x − 0)(x − 1) 1 2 1
ℓ2 (x) = = x − x
(3 − 0)(3 − 1) 6 6
4
Numerical exercises:
1. Plot the cardinal functions for the nodes of Example 1.
2. Plot the interpolation polynomials for some points of your own choice.
Newton Interpolation. This is an alternative approach to find the interpolation polynomial. In the
following, we will assume that yi = f (xi ) for some given function f (x).
Let x0 , x1 , . . . , xn be n + 1 distinct real numbers. The so-called Newton form of a polynomial of degree n
is an expansion of the form
n−1
X n−1−i
Y
pn (x) = cn−i (x − xj ) + c0 ,
i=0 j=0
or more explicitly
In the light of this form of writing a polynomial, the polynomial interpolation problem leads to the
following observations: Let us start with a single node x0 , then f (x0 ) = p(x0 ) = c0 . We go one step further
and consider two nodes x0 , x1 . Then we see that f (x0 ) = p(x0 ) = c0 and f (x1 ) = p(x1 ) = c0 + c1 (x1 − x0 ).
The latter implies that the coefficient c1 is given as
f (x1 ) − f (x0 )
c1 = .
x1 − x0
Given three nodes x0 , x1 , x2 , we obtain the coefficients c0 , c1 as defined above, and from the equation
This procedure can be continued and yields a so-called triangular system that permits to define the
remaining coefficients c3 , . . . , cn . One sees quickly that the coefficient ck only depends on the interpolation
points (x0 , y0 ), . . . , (xk , yk ), where yi := f (xi ), i = 0, . . . , n.
We introduce the following so-called finite difference notation for a function f . The 0th order finite
difference is defined to be f [x0 ] := f (x0 ). The 1st order finite difference is
f (x1 ) − f (x0 )
f [x0 , x1 ] := .
x1 − x0
The second order finite difference is defined as
f [x1 , x2 ] − f [x0 , x1 ]
f [x0 , x1 , x2 ] := .
x2 − x0
In general, the kth order finite difference of the function f is defined to be
f [x1 , . . . , xk ] − f [x0 , . . . , xk−1 ]
f [x0 , . . . , xk ] := .
xk − x0
Newton’s method to solve the polynomial interpolation problem can be summarized as follows: We are
given n + 1 interpolation points (x0 , y0 ), . . . , (xn , yn ), yi := f (xi ). If the order n interpolation polynomial
is expressed in Newton’s form
5
then the coefficients
ck = f [x0 , . . . , xk ]
for k = 0, . . . , n. In fact, a recursion is in place:
It is common to write the finite differences in a table, which for n = 3 will have the form
x0 f [x0 ]
f [x0 , x1 ]
x1 f [x1 ] f [x0 , x1 , x2 ]
f [x1 , x2 ] f [x0 , x1 , x2 , x3 ]
x2 f [x2 ] f [x1 , x2 , x3 ]
f [x2 , x3 ]
x3 f [x3 ]
Example 1 again: We are given the points in Example 1. The corresponding table of divided differences
becomes
0 1
−3/4
2/3 1/2 −3/4
−3/2
1 0
and the interpolation polynomial becomes
3 3 2 1 3
p2 (x) = 1 − (x − 0) − (x − 0)(x − ) = 1 − x − x2 .
4 4 3 4 4
pn (xi ) = yi , i = 0, . . . , n.
Proof: Suppose there exist two different interpolation polynomials pn and qn of degree n interpolating the
same n + 1 points. The polynomial r(x) = pn (x) − qn (x) is of degree n with zeros in all the nodes xi , that
is a total of n + 1 zeros. But then r ≡ 0, thus the two polynomials pn and qn are identical.
6
Error Analysis. Given some function f ∈ C[a, b], choose n + 1 distinct nodes in [a, b] and let pn (x) ∈ Pn
satisfy the interpolation condition
pn (xi ) = f (xi ), i = 0, . . . , n.
Example 5: Let f (x) = sin(x), x ∈ [0, 2π]. Choose n + 1 equidistributed nodes, that is xi = ih,
i = 0, . . . , n, and h = 2π/n. Calculate the interpolation polynomial (using whatever
by use of the functions cardinal and lagrange. Plot the error en (x) = f (x) − pn (x) for different values
of n. Choose n = 4, 8, 16 and 32. Notice how the error is distributed over the interval, and find the
maximum error maxx∈[a,b] |en (x)| for each n.
See example5() in interpolation.py.
Interpolation Error. An expression for the interpolation error f (x) − p(x) is given by the following
theorem:
Given f ∈ C (n+1) [a, b]. Let pn ∈ Pn interpolate f in n + 1 distinct nodes xi ∈ [a, b]. For each
x ∈ [a, b] there is at least one ξ(x) ∈ (a, b) such that
n
f (n+1) (ξ(x)) Y
f (x) − pn (x) = (x − xi ).
(n + 1)! i=0
Proof: Start by assuming f to be sufficiently differentialable, what this is will be revealed on the go. We
will also need the following function, defined solely by the nodes:
n
Y
ω(x) = (x − xi ) = xn+1 + · · · .
i=0
Obviously, in the nodes xi there are no error, thus e(xi ) = 0, i = 0, . . . , n. Choose an arbitrary x ∈ [a, b],
x ∈ [a, b], where x ̸= xi , i = 0, 1, . . . , n. For this fixed x, define a function in t as:
where e(t) = f (t) − pn (t). Notice that φ(t) is as differentiable with respect to t as f (t). The function φ(t)
has n + 2 distinct zeros (the nodes and the fixed x). As a consequence of Rolle’s theorem (see Preliminaries,
Some other useful results) the derivative φ′ (t) has at least n + 1 distinct zeros, one between each of the
zeros of φ(t). So φ′′ (t) has n distinct zeros, etc. By repeating this argument, we can see that φn+1 (t) has
at least one zero in [a, b], let us call this ξ(x), as it do depend on the fixed x. Since ω (n+1) (t) = (n + 1)!
and e(n+1) (t) = f (n+1) (t) we can conclude that
Solving this with respect to e(x) gives the statement of the theorem.
The interpolation error consists of three elements: The derivative of the function f , the number of
interpolation points n + 1 and the distribution of the nodes xi . We cannot do much with the first of these,
but we can choose the two others. Let us first look at the most obvious choice of nodes.
7
Equidistributed nodes. The nodes are equidistributed over the interval [a, b] if xi = a+ih, h = (b−a)/n.
In this case it can be proved that:
hn+1
|ω(x)| ≤ n!
4
such that
hn+1
|e(x)| ≤ M, M = max |f (n+1) (x)|.
4(n + 1) x∈[a,b]
Example 6: Let again f (x) = sin(x) on the interval [0, 2π] and pn (x) the polynomial interpolating f (x)
in n + 1 equidistributed points. Find an upper bound for the error for different values of n.
Clearly, maxx∈[0,2π] |f (n+1) (x)| = M = 1 for all n, so
n+1
1 2π
|en (x)| = |f (x) − pn (x)| ≤ , x ∈ [a, b].
4(n + 1) n
Use the code in Example 5 to verify the result. How close is the bound to the real error?
1
= max |ωCheb (x)| ≤ max |q(x)|
2n x∈[−1,1] x∈[−1,1]
8
where x ∈ [a, b] and x̃ ∈ [−1, 1]. By doing so we get
n n
n+1 Y n+1
Y b−a b−a
ω(x) = (x − xi ) = (x̃ − x̃i ) = ωCheb (x̃).
i=0
2 i=0
2
Given f ∈ C (n+1) [a, b], and let Mn+1 = maxx∈[a,b] |f (n+1) (x)|. Let pn ∈ Pn interpolate f i n + 1
Chebyshev-nodes xi ∈ [a, b]. Then
(b − a)n+1
max |f (x) − pn (x)| ≤ Mn+1 .
x∈[a,b] 22n+1 (n + 1)!
The Chebyshev nodes over an interval [a, b] are evaluated in the following function: See chebyshev_nodes(a, b, n)
in interpolation.py.
Numerical exercises:
1. Plot ωCheb (x) for 3, 5, 9, 17 interpolation points.
2. Repeat Example 3 using Chebyshev interpolation on the functions below. Compare with the results
you got from equidistributed nodes.
For information: Chebfun is software package which makes it possible to manipulate functions and
to solve equations with accuracy close to machine accuracy. The algorithms are based on polynomial
interpolation in Chebyshev nodes.
3 Spline Interpolation
3.1 Idea of Splines
The results we have obtained above have shown that we obtain the best results for polynomial interpolation,
if we use the Chebyshev interpolation points. Instead, if we use equidistant interpolation points, the
resulting interpolation polynomials might actually diverge as the number of nodes increases. This is what
we have observed for the example of Runge’s function. Chebyshev interpolation, however, is only possible
if we are free to choose the nodes ourselves. In the case where we want to interpolate an unknown function
in some given measurement points, this is not the case. Because of that, we will here briefly discuss a
different approximation strategy that uses piecewise polynomials instead of polynomials.
Assume again that [a, b] is an interval and that we are given n + 1 nodes a = x0 < x1 < . . . < xn = b.
Write ∆ := (x0 , x1 , . . . , xn ). A spline of order k is a function s ∈ C k−1 (a, b) such that the restriction of s
to every interval (xi , xi+1 ) is a polynomial of degree k. That is, s consists of piecewise polynomials that
are glued together at the nodes xi in an as smooth as possible manner. The space of all splines of order k
on the grid ∆ is denoted as Sk,∆ .
For k = 1, we speak of linear splines, for k = 2 of quadratic splines, and for k = 3 of cubic splines.
NB: In the literature, there exist different notations for the order of splines. Sometimes linear splines are
said to be of order 0, quadratic splines of order 1, cubic splines of order 2, and so on.
9
3.2 Linear Splines
Linear splines, or splines of order 1, are by definition continuous functions that are linear in between the
nodes. Graphically, we obtain the linear interpolation spline through points (xi , yi ) by simply connecting
consecutive points by a straight line. Formally, the construction is not much more difficult than that:
One can show that the “hat-functions”
x−x
i−1
if xi−1 ≤ x < xi ,
xi − xi−1
Λi (x) := x i+1 − x for i = 1, . . . , n − 1,
if xi ≤ x < xi+1 ,
xi+1 − xi
0 else,
form a basis of the spline space S1,∆ . Every linear spline s ∈ S1,∆ has the unique representation
m
X
s(x) = s(xi )Λi (x).
i=0
Note the conceptual similarity to the cardinal functions in polynomial interpolation: As in the case of
Lagrange interpolation, we simply obtain the interpolation spline by multiplying the y-values with the
corresponding basis functions and summing up over all i.
In numpy, interpolation with linear splines is implemented in the function interp.
However, the coefficients ci,j cannot be chosen freely, as a cubic spline also have to be twice continuously
differentiable. This adds restrictions at the interior nodes x1 , . . . , xn−1 : At each of these nodes, the spline
s has to be continuous with continuous first and second derivatives s′ , s′′ . We therefore obtain 3 conditions
10
for each interior node, which gives us 3(n − 1) conditions in total. At the boundary nodes x0 and xm , we
do not get any further conditions. In total, we thus have 4n coefficients, which have to satisfy 3(n − 1)
additional conditions. This yields n + 3 degrees of freedom. Indeed, one can show that the space S3,∆ is a
vector space of dimension n + 3.
Assume now that we want to interpolate the data points (xi , yi )ni=0 with a cubic spline s. Then the spline
s has to satisfy the n + 1 equations s(xi ) = yi . This, however, does not give us enough conditions to
determine s uniquely, since, as discussed above, we actually have m + 3 degrees of freedom. We therefore
need to specify 2 additional conditions. Typically, this is done in the form of boundary conditions that
add additional requirements at, or near, the boundary nodes x0 and xn . The most important conditions
are the following:
• Natural boundary conditions: Here we require that s′′ (x0 ) = 0 and s′′ (xn ) = 0, that is, the spline s
has no curvature at the boundary.
• Clamped boundary conditions: Here we require that s′ (x0 ) = 0 and s′ (xn ) = 0, that is, the spline s
has horizontal tangents at the boundaries. This is useful when we want to interpolate a function
with the same property. Else the result looks unnatural and we obtain relatively large errors near
the boundary.
• Not-a-knot boundary conditions: Here we remove the nodes x1 and xn−1 from the grid and define
the spline using the modified grid ∆ ˜ = (x0 , x2 , x3 , . . . , xn−3 , xn−2 , xn ) instead. Doing so, we lose 2
degrees of freedom in the spline space we are using. We still require that the interpolation condition
s(xi ) = yi is satisfied at all xi , including the two nodes we have removed from the grid.
To our best knowledge, there exists no built in method for interpolation with cubic splines in numpy. There
exists, however, an implementation in the package scipy, which we will use in the following example.
11