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 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,,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 of 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 of 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.

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.

Hence a log-log graph of error versus hh should be approximately a straight line of slope mm.

5.2.4Exercises

  1. ⌨ For each given function and interval, perform piecewise linear interpolation using Function 5.2.2 for n+1n+1 equispaced nodes with n=10,20,40,80,160,320n=10,20,40,80,160,320. For each nn, estimate the error

    E(n)=fp=maxxf(x)p(x)E(n) = \| f-p \|_\infty = \max_x | f(x) - p(x) |

    by evaluating the function and interpolant at 1600 points in the interval. Make a log-log plot of EE as a function of nn and add the line E=Cn2E=Cn^{-2} for a constant CC of your choosing.

    (a) cos(πx2)\cos(\pi x^2) on [0,4][0,4]

    (b) log(x)\log(x) on [1,20][1,20]

    (c) sin(1x)\sin\left(\frac{1}{x}\right) on [12,7]\left[\frac{1}{2},7\right]

  2. ✍ For this problem, let H(x)H(x) be the hat function that passes through the three points (1,0)(-1,0), (0,1)(0,1), and (1,0)(1,0).

    (a) Write out a piecewise definition of HH in the style of (5.2.2).

    (b) Define the function QQ by Q(x)=x1xH(t)dtQ(x) = \int_{x-1}^x H(t)\, dt. Find a piecewise formula for Q(x)Q(x). (Hint: Perform the integration separately for the cases 1x0-1\le x \le 0, 0x10\le x \le 1, etc.)

    (c) Make a sketch of Q(x)Q(x) for 2x2-2\le x \le 2.

    (d) Show that QQ is continuous. Are QQ' and QQ''?

  3. ✍ Before electronic calculators, the function ln(x)\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,43.1,3.2,\ldots,3.9,4, what is an upper bound on the error in the result?

  4. ✍ Show that for any node distribution and any x[t0,tn]x\in[t_0,t_n],

    k=0nHk(x)=1.\sum_{k=0}^n H_k(x) = 1.

    (Hint: The simplest way is to apply (5.2.5).) This is called the partition of unity property.

  5. ✍ Here we consider a proof of Theorem 5.2.2 using the mean value theorems from elementary calculus: If ff is continuously differentiable in (a,b)(a,b), then there exist points ss and tt in (a,b)(a,b) such that

    abf(z)dz=(ba)f(s)andf(t)=f(b)f(a)ba.\int_a^b f(z) \, dz = (b-a)f(s) \qquad \text{and} \qquad f'(t) = \frac{f(b)-f(a)}{b-a}.

    For the following, suppose x(tk,tk+1)x \in (t_k,t_{k+1}).

    (a) Show that for some s(tk,tk+1)s \in (t_k,t_{k+1}),

    f(x)=yk+(xtk)f(s).f(x) = y_k + (x-t_k)f'(s).

    (b) Show that for some other values uu and vv in (tk,tk+1)(t_k,t_{k+1}),

    f(s)yk+1yktk+1tk=(su)f(v).f'(s) - \frac{y_{k+1}-y_k}{t_{k+1}-t_k} = (s-u) f''(v).

    (c) Use (5.2.1) to finish the proof of the theorem.