0% found this document useful (0 votes)
19 views

Interpolation

Uploaded by

daniyadevacc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Interpolation

Uploaded by

daniyadevacc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Lecture notes for TMA4130/35 Mathematics 4N/D

Interpolation

Anne Kværnø, Markus Grasmair

Jan 18, 2023

The Python codes for this note are given in interpolation.py.


If you want to have a nicer theme for your jupyter notebook, download the cascade stylesheet file
tma4320.css and execute the next cell:

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.

Example 1: We are given the 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").

Example 2: Given the points


xi 0 1 3 4 7
,
yi 3 8 6 −1 2
calculate the interpolation polynomial by using the function Polynomial.fit within the polynomial package
in numpy, and plot the result.
See example2() in interpolation.py.

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.

Written in matrix-vector form, the system becomes


    
1 0 0 0 0 c0 3
1 1 1 1 1  c1   8 
    
   
 
1 3 9 27 81  c2  =  6  .
    
    
1 4 16 64 256 
 c3  −1
   

1 7 49 343 2401 c4 2

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

The cardinal functions have the following properties:


• ℓi ∈ Pn , i = 0, 1, . . . , n.
(
1, when i = j
• ℓi (xj ) = δij = .
0, when i = ̸ j
• They are linearly independent, and thus form a basis for Pn .
Assume now that we are given points (xi , yi )ni=0 , again with distinct xi values. The interpolation polynomial
is then given by
Xn
pn (x) = yi ℓi (x),
i=0
as
n
X
pn (xj ) = yi ℓi (xj ) = yj , j = 0, . . . , n.
i=0

Example 3: We are given the points


xi 0 1 3
.
yi 3 8 6
The corresponding cardinal functions are

(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

and the interpolation polynomial is given by (check it yourself):

p2 (x) = 3ℓ0 (x) + 8ℓ1 (x) + 6ℓ2 (x) = −2x2 + 7x + 3.

The method above is implemented as two functions:


• cardinal(xdata, x): Create a list of cardinal functions ℓi (x) evaluated in x.
• lagrange(ydata, l): Create the interpolation polynomial pn (x).
Here, xdata and ydata are arrays with the interpolation points, and x is an array of values in which the
polynomials are evaluated.
You are not required to understand the implementation of these functions, but you should understand
how to use them.

Example 4: Test the functions on the interpolation points of Example 3.


See example4() in interpolation.py.

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

pn (x) = cn (x − x0 )(x − x1 ) · · · (x − xn−1 ) + cn−1 (x − x0 )(x − x1 ) · · · (x − xn−2 ) + · · · + c1 (x − x0 ) + c0 .

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

f (x2 ) = pn (x2 ) = c0 + c1 (x2 − x0 ) + c2 (x2 − x0 )(x2 − x1 )

we deduce the coefficient


f (x1 )−f (x0 )
f (x2 ) − f (x0 ) − x1 −x0 (x2 − x0 )
c2 = .
(x2 − x0 )(x2 − x1 )
Playing with this quotient gives the much more structured expression
f (x2 )−f (x1 ) f (x1 )−f (x0 )
x2 −x1 − x1 −x0
c2 = .
(x2 − x0 )

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

pn (x) = cn (x − x0 )(x − x1 ) · · · (x − xn−1 ) + cn−1 (x − x0 )(x − x1 ) · · · (x − xn−2 ) + · · · + c1 (x − x0 ) + c0 ,

5
then the coefficients
ck = f [x0 , . . . , xk ]
for k = 0, . . . , n. In fact, a recursion is in place:

pn (x) = pn−1 (x) + f [x0 , . . . , xn ](x − x0 )(x − x1 ) · · · (x − xn−1 )

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

The method above is implemented as two functions:


• divdiff(xdata, ydata): Create the table of divided differences
• newtonInterpolation(F, xdata, x): Evaluate the interpolation polynomial.
Here, xdata and ydata are arrays with the interpolation points, and x is an array of values in which the
polynomial is evaluated. Run the code on the example above: See example_divided_differences() in
interpolation.py.

2.2 Theoretical Results


Existence and Uniqueness. We have already proved the existence of such polynomials, simply by
constructing them. But are they unique? The answer is yes!

Theorem: Existence and uniqueness of interpolation polynomials:


Given n + 1 points (xi , yi )ni=0 with distinct x values. Then there is one and only one polynomial
pn (x) ∈ Pn satisfying the interpolation condition

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.

What can be said about the error e(x) = f (x) − pn (x)?


Let us start with an numerical experiment, to have a feeling of what to expect.

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.

Numerical exercise: Repeat the experiment with Runge’s function


1
f (x) = , x ∈ [−5, 5].
1 + x2

Interpolation Error. An expression for the interpolation error f (x) − p(x) is given by the following
theorem:

Theorem: Interpolation error.

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:

φ(t) = e(t)ω(x) − e(x)ω(t).

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

φ(n+1) (ξ) = 0 = f (n+1) (ξ)ω(x) − e(x)(n + 1)!.

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]

for all x ∈ [a, b].


Let us now see how good this error bound is by an example.

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?

2.3 Optimal choice of interpolation points


If you have done the exercise with Runge’s function, you should have noted, that the interpolation
polynomial with equidistant interpolation points in this case appears not to converge to the function f f.
Instead the error becomes larger and the approximation worse as the degree increases. Therefore we are
interested in finding alternatives that yield a better approximation.
So how can the error
Qn be reduced? For a given n there is only one choice: to distribute the nodes in order
to make |ω(x)| = j=0 |x − xi | as small as possible. We will first do this on a standard interval [−1, 1],
and then transfer the results to some arbitrary interval [a, b].
Let us start taking a look at ω(x) for equidistributed nodes on the interval [−1, 1], for different values of
n:
Run the function plot_omega().
Run the code for different values of n. Notice the following:
• maxx∈[−1,1] |ω(x)| becomes smaller with increasing n.
• |ω(x)| has its maximum values near the boundaries of [−1, 1].
A a consequence of the latter, it seems reasonable to move the nodes towards the boundaries. It can be
proved that the optimal choice of nodes are the Chebyshev-nodes, given by
(2i + 1)π
 
x̃i = cos , i = 0, . . . , n
2(n + 1)
Qn
Let ωCheb (x) = j=0 (x − x̃i ). It is then possible to prove that

1
= max |ωCheb (x)| ≤ max |q(x)|
2n x∈[−1,1] x∈[−1,1]

for all polynomials q ∈ Pn+1 such that q(x) = xn+1 + cn xn + · · · + c1 x + c0 .


The distribution of nodes can be transferred to an interval [a, b] by the linear transformation
b−a b+a
x= x̃ +
2 2

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

From the theorem on interpolation errors we can conclude:

Theorem (interpolation error for Chebyshev interpolation).

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.

f (x) = sin(x), x ∈ [0, 2π]


1
f (x) = , x ∈ [−5, 5].
1 + x2

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,

as well as the “half-hat functions” at the boundary


( ( x−xn−1
x1 −x
if x0 ≤ x < x1 if xn−1 ≤ x < xn ,
Λ0 (x) = x1 −x0 and Λn (x) = xn −xn−1
0 else, 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

In particular, if we want to interpolate the points (xi , yi )m


i=1 with x0 < x1 < . . . < xm , the interpolating
linear spline is given as
Xm
s(x) = yi Λ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.

Example 7: Given the points


xi 0 1 3 4 7
,
yi 3 8 6 −1 2
compute the linear interpolation spline through these points and plot the result.
See example7() in interpolation.py.

3.3 Cubic Splines


In practice, the probably most important splines are cubic splines s ∈ S3,∆ , that is, splines of order 3.
One of the reasons is that cubic splines, by construction, are twice differentiable, and therefore appear
as “smooth” to the human eye. Also, when used for interpolating data points (xi , yi ), they minimise (an
approximation to) the curvature amongst all interpolating functions. This has the effect that they look
like a “natural” interpolation through the given points.
Next, we will discuss how large the space of cubic splines is. To that end, we assume that we are given a
grid ∆ = (x0 , x1 , . . . , xn ) with n + 1 nodes. Every spline s ∈ S3,∆ is a cubic polynomial on each of the
intervals [xi , xi+1 ], i = 0, . . . , n, and there are n such intervals. Thus, to start with, a cubic spline s is
determined by 4n coefficients: 4 for each subinterval, where the spline is equal to a cubic polynomial. We
can, for instance, write for some i = 0, . . . , n − 1 that

s(x) = ci,0 + ci,1 (x − xi ) + ci,2 (x − xi )2 + ci,3 (x − xi )3 if x ∈ [xi , xi+1 ). (3)

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.

Example 7: Given the points


xi 0 1 3 4 7
,
yi 3 8 6 −1 2
compute the different cubic interpolation splines through these points and plot the result.
See example8() in interpolation.py.
In contrast to the case of polynomials and linear splines,
Pn there is no simple representation of splines that
allows us to write the interpolation spline as s(x) = i=0 yi si (x) for some basis splines si . More precisely,
such a representation exists, but it is by no means simple and therefore usually not used for practical
applications. In scipy, for instance, the representation (3) is used. Another common representation makes
use of so-called B-splines (see e.g. https://en.wikipedia.org/wiki/B-spline). We won’t discuss their
construction in this note, though.

11

You might also like