Rather than basing an implementation on (5.2.1), we return to the idea used in Demo 2.1.1 of choosing the interpolant from among the linear combinations of a preselected finite set of functions. In the present context we use, for k=0,…,n,
The functions H0,…,Hn are called hat functions. They depend on the node vector t, but this dependence is not usually indicated explicitly.
Each hat function is globally continuous and is linear inside every interval [tk,tk+1]. Consequently, any linear combination of them will have the same property. Furthermore, any such function is expressible as a unique linear combination of hat functions, i.e.,
for some choice of the coefficients c0,…,cn. No smaller set of functions can have the same properties. We summarize these facts by calling the hat functions a basis of the set of functions that are continuous and piecewise linear relative to t. Another point of view, familiar from abstract linear algebra, is that a basis sets up a one-to-one correspondence between the spanned function space and the more familiar space Rn+1, with each function being represented by its coefficients c0,…,cn.
An appealing characteristic of the hat function basis is that it depends only on the node locations, while the expansion coefficients in (5.2.3) depend only on the data values. This clean separation would be useful if we wanted to construct many interpolants on the same node set, and it has deeper theoretical uses as well.
Function 5.2.1 presents a simple implementation of hat functions. The inputs are a presorted vector of nodes and a value of k between 0 and n, which represent the indices of the endpoints. The return value is a function of x that can be evaluated as needed. Note that we have not formally defined values for a hat function outside of the node interval; our choice in Function 5.2.1 is to make it zero there.
A handy property of the hat functions is that they are cardinal functions for piecewise linear interpolation, since they satisfy the cardinality conditions
All candidate piecewise linear (PL) functions can be expressed as a linear combination such as (5.2.3) for some coefficients c0,…,cn. But because of the cardinality conditions and the necessity for p(x) to interpolate the data values in y, expressing the interpolant using the hat functions is trivial:
The resulting algorithmic simplicity is reflected in Function 5.2.2. Take note that the output of Function 5.2.2 is itself a function, meant to be called with a single argument representing a value of x. Our mathematical viewpoint is that the result of an interpolation process is a function, and our codes reflect this.
The condition number bounds from Theorem 5.1.1 are very simple for piecewise linear interpolation because the interpolant of the data ek is just the hat function Hk. Hence 1≤κ≤n+1. However, there is an even simpler result.
Now suppose that f is a “nice” function on an interval [a,b] containing all of the nodes. We can sample values of f to get data, i.e., yk=f(tk) for all k, then perform piecewise linear interpolation of the data to get a different function, the interpolant p. How close is p to the original f?
To make a simple statement, we will consider only the case of equally spaced nodes covering the interval. It turns out that piecewise linear interpolation converges at second order in the spacing of the nodes.
We normally don’t have access to f′′, so the importance of Theorem 5.2.2 is that the error in the interpolant is O(h2) as h→0.
Thus, Theorem 5.2.2 states that piecewise linear interpolation is second-order accurate. For instance, if we increase the number of equally spaced nodes by a factor of 10, the piecewise linear interpolant becomes about 100 times more accurate. Note also that if y≈Chm, then
⌨ For each given function and interval, perform piecewise linear interpolation using Function 5.2.2 for n+1 equispaced nodes with n=10,20,40,80,160,320. For each n, estimate the error
by evaluating the function and interpolant at 1600 points in the interval. Make a log-log plot of E as a function of n and add the line E=Cn−2 for a constant C of your choosing.
(a)cos(πx2) on [0,4]
(b)log(x) on [1,20]
(c)sin(x1) on [21,7]
✍ For this problem, let H(x) be the hat function that passes through the three points (−1,0), (0,1), and (1,0).
(a) Write out a piecewise definition of H in the style of (5.2.2).
(b) Define the function Q by Q(x)=∫x−1xH(t)dt. Find a piecewise formula for Q(x). (Hint: Perform the integration separately for the cases −1≤x≤0, 0≤x≤1, etc.)
(c) Make a sketch of Q(x) for −2≤x≤2.
(d) Show that Q is continuous. Are Q′ and Q′′?
✍ Before electronic calculators, the function ln(x) was often computed using piecewise linear interpolation with a table of values. If you were using such a table at the nodes 3.1,3.2,…,3.9,4, what is an upper bound on the error in the result?
✍ Show that for any node distribution and any x∈[t0,tn],
(Hint: The simplest way is to apply (5.2.5).) This is called the partition of unity property.
✍ Here we consider a proof of Theorem 5.2.2 using the mean value theorems from elementary calculus: If f is continuously differentiable in (a,b), then there exist points s and t in (a,b) such that