Skip to article frontmatterSkip to article content

Piecewise linear interpolation

Piecewise linear interpolation is simply a game of connect-the-dots. That is, the data points are joined pairwise by line segments.

It should be clear from (5.2.1) that on each interval [tk,tk+1][t_k,t_{k+1}], p(x)p(x) is a linear function passing through both (tk,yk)(t_k,y_k) and (tk+1,yk+1)(t_{k+1},y_{k+1}).

5.2.1Hat functions

Rather than basing an implementation on (5.2.1), we return to the idea used in Example 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,,nk=0,\ldots,n,

Hk(x)={xtk1tktk1if x[tk1,tk],tk+1xtk+1tkif x[tk,tk+1],0otherwise. H_k(x) = \begin{cases} \dfrac{x-t_{k-1}}{t_k-t_{k-1}} & \text{if $x\in[t_{k-1},t_k]$},\\[2.5ex] \dfrac{t_{k+1}-x}{t_{k+1}-t_{k}} & \text{if $x\in[t_{k},t_{k+1}]$},\\[2.5ex] 0 & \text{otherwise}. \end{cases} \qquad

The functions H0,,HnH_0,\ldots,H_n are called hat functions. They depend on the node vector t\mathbf{t}, but this dependence is not usually indicated explicitly.

Each hat function is globally continuous and is linear inside every interval [tk,tk+1][t_k,t_{k+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.,

k=0nckHk(x) \sum_{k=0}^n c_k H_k(x)

for some choice of the coefficients c0,,cnc_0,\ldots,c_n. 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\mathbf{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\mathbb{R}^{n+1}, with each function being represented by its coefficients c0,,cnc_0,\ldots,c_n.

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 kk between 0 and nn, which represent the indices of the endpoints. The return value is a function of xx that can be evaluated as needed. Note that we have not formally defined values for a hat function outside the node interval; our choice in Function 5.2.1 is to make it zero there.

5.2.2Cardinality conditions

A handy property of the hat functions is that they are cardinal functions for piecewise linear interpolation, since they satisfy the cardinality conditions

Hk(ti)={1if i=k,0otherwise.H_k(t_i) = \begin{cases} 1 &\text{if $i=k$,}\\ 0 & \text{otherwise.} \end{cases}

All candidate piecewise linear (PL) functions can be expressed as a linear combination such as (5.2.3) for some coefficients c0,,cnc_0,\ldots,c_n. But because of the cardinality conditions and the necessity for p(x)p(x) to interpolate the data values in y\mathbf{y}, expressing the interpolant using the hat functions is trivial:

p(x)=k=0nykHk(x). p(x) = \sum_{k=0}^n y_k H_k(x).

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 xx. Our mathematical viewpoint is that the result of an interpolation process is a function, and our codes reflect this.

5.2.3Conditioning and convergence

The condition number bounds from Theorem 5.1.1 are very simple for piecewise linear interpolation because the interpolant of the data ek\mathbf{e}_k is just the hat function HkH_k. Hence, 1κn+11\le \kappa \le n+1. However, there is an even simpler result.

Now suppose that ff is a “nice” function on an interval [a,b][a,b] containing all the nodes. We can sample values of ff to get data, i.e., yk=f(tk)y_k=f(t_k) for all kk, then perform piecewise linear interpolation of the data to get a different function, the interpolant pp. How close is pp to the original ff?

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.

For an outline of a proof, see Exercise 5.2.5.

We normally don’t have access to ff'', so the importance of Theorem 5.2.2 is that the error in the interpolant is O(h2)O(h^2) as h0h\to 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 yChmy \approx C h^m, then

logym(logh)+logC.\log y \approx m (\log h) + \log C.

5.2.4Exercises