# Piecewise linear interpolation

## Contents

# 5.2. 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.

Given nodes \(t_0 < t_1 < \cdots < t_n\), the **piecewise linear** interpolant \(p(x)\) is given by

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

## Hat functions#

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

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

Each hat function is globally continuous and is linear inside every interval \([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.,

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

Function 5.2.2 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.2 is to make it zero there.

**Hat function/piecewise linear basis function**

```
1"""
2 hatfun(t,k)
3
4Create a piecewise linear hat function, where `t` is a
5vector of n+1 interpolation nodes and `k` is an integer in 0:n
6giving the index of the node where the hat function equals one.
7"""
8
9function hatfun(t,k)
10 n = length(t)-1
11 return function(x)
12 if k > 0 && t[k] ≤ x ≤ t[k+1]
13 return (x-t[k])/(t[k+1]-t[k])
14 elseif k < n && t[k+1] ≤ x ≤ t[k+2]
15 return (t[k+2]-x)/(t[k+2]-t[k+1])
16 else
17 return 0
18 end
19 end
20end
```

Let’s define a set of four nodes (i.e., \(n=3\) in our formulas).

```
t = [0, 0.55, 0.7, 1]
```

```
4-element Vector{Float64}:
0.0
0.55
0.7
1.0
```

```
plt = plot(layout=(4,1),legend=:top,
xlabel=L"x",ylims=[-0.1,1.1],ytick=[])
for k in 0:3
Hₖ = FNC.hatfun(t,k)
plot!(Hₖ,0,1,subplot=k+1)
scatter!(t,Hₖ.(t),m=3,subplot=k+1)
annotate!(t[k+1],0.25,text(latexstring("H_$k"),10),subplot=k+1)
end
plt
```

## Cardinality conditions#

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 \(c_0,\ldots,c_n\). But because of the cardinality conditions and the necessity for \(p(x)\) to interpolate the data values in \(\mathbf{y}\), expressing the interpolant using the hat functions is trivial:

We generate a piecewise linear interpolant of \(f(x)=e^{\sin 7x}\).

```
f = x -> exp(sin(7*x))
plot(f,0,1,label="function",xlabel=L"x",ylabel=L"y")
```

First we sample the function to create the data.

```
t = [0, 0.075, 0.25, 0.55, 0.7, 1] # nodes
y = f.(t) # function values
scatter!(t,y,label="values at nodes")
```

Now we create a callable function that will evaluate the piecewise linear interpolant at any \(x\), and then plot it.

```
p = FNC.plinterp(t,y)
plot!(p,0,1,label="interpolant",title="PL interpolation")
```

The resulting algorithmic simplicity is reflected in Function 5.2.5. Take note that the output of Function 5.2.5 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.

A final 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.

**Piecewise linear interpolation**

```
1"""
2 plinterp(t,y)
3
4Construct a piecewise linear interpolating function for data values in
5`y` given at nodes in `t`.
6"""
7function plinterp(t,y)
8 n = length(t)-1
9 H = [ hatfun(t,k) for k in 0:n ]
10 return x -> sum( y[k+1]*H[k+1](x) for k in 0:n )
11end
```

## Conditioning and convergence#

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

The absolute condition number of piecewise linear interpolation in the infinity norm equals 1. More specifically, if \(\mathcal{I}\) is the piecewise linear interpolation operator, then

(The norm on the left side is on functions, while the norm on the right side is on vectors.)

By linearity,

Call this piecewise linear function \(p(x)\). Consider a maximum element of \(\mathbf{z}\), i.e., choose \(i\) such that \(|z_i|=\|\mathbf{z}\|_\infty\). Then \(|p(t_i)|=\|\mathbf{z}\|_\infty\). Hence \(\|p\|_\infty\ge \|\mathbf{z}\|_\infty\). Now consider

You are asked to prove the final step above in Exercise 4. We conclude that \(\|p\|_\infty\le \|\mathbf{z}\|_\infty\), so that \(\|p\|_\infty = \|\mathbf{z}\|_\infty\), which completes the proof.

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., \(y_k=f(t_k)\) 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.

Suppose that \(f(x)\) has a continuous second derivative in \([a,b]\) (often expressed as \(f\in C^2([a,b])\)). Let \(p_n(x)\) be the piecewise linear interpolant of \(\bigl(t_i,f(t_i)\bigr)\) for \(i=0,\ldots,n\), where \(t_i=a+i h\) and \(h=(b-a)/n\). Then

where \(M = \bigl\| f'' \bigr\|_\infty\).

For an outline of a proof, see Exercise 5.

We normally don’t have access to \(f''\), so the importance of Theorem 5.2.7 is that the error in the interpolant is \(O(h^2)\) as \(h\to 0\).

If an approximation has error that is \(O(h^m)\) as \(h\to 0\) for an integer \(m\) and a discretization size parameter \(h\), then we say the approximation has **algebraic convergence**. If the error is not also \(O(h^{m+1})\), then \(m\) is the **order of accuracy**.

Thus, Theorem 5.2.7 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 \approx C h^m\), then

Hence a log-log graph of error versus \(h\) should be approximately a straight line of slope \(m\).

We measure the convergence rate for piecewise linear interpolation of \(e^{\sin 7x}\) over \(x \in [0,1]\).

```
f = x -> exp(sin(7*x))
x = range(0,1,length=10001) # sample the difference at many points
n = @. round(Int,10^(1:0.25:3.5))
maxerr = zeros(0)
for n in n
t = (0:n)/n # interpolation nodes
p = FNC.plinterp(t,f.(t))
err = @. f(x)-p(x)
push!(maxerr,norm(err,Inf) )
end
data = (n=n[1:4:end],err=maxerr[1:4:end])
pretty_table(data,["n","max-norm error"])
```

```
┌──────┬────────────────┐
│ n │ max-norm error │
├──────┼────────────────┤
│ 10 │ 0.150471 │
│ 100 │ 0.00166421 │
│ 1000 │ 1.66494e-5 │
└──────┴────────────────┘
```

As predicted, a factor of 10 in \(n\) produces a factor of 100 in the error. In a convergence plot, it is traditional to have \(h\) *decrease* from left to right, so we expect a straight line of slope \(-2\) on a log-log plot.

```
h = @. 1/n
order2 = @. 10*(h/h[1])^2
plot(h,maxerr,m=:o,label="error")
plot!(h,order2,l=:dash,label=L"O(h^2)",xflip=true,
xaxis=(:log10,L"h"),yaxis=(:log10,L"|| f-p\, ||_\infty"),
title="Convergence of PL interpolation")
```

## Exercises#

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

\[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 \(E\) as a function of \(n\) and add the line \(E=Cn^{-2}\) for a constant \(C\) of your choosing.

**(a)**\(\cos(\pi x^2)\) on \([0,4]\)**(b)**\(\log(x)\) on \([1,20]\)**(c)**\(\sin\left(\frac{1}{x}\right)\) on \(\left[\frac{1}{2},7\right]\)✍ 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) = \int_{x-1}^x H(t)\, dt\). Find a piecewise formula for \(Q(x)\). (Hint: Perform the integration separately for the cases \(-1\le x \le 0\), \(0\le x \le 1\), etc.)**(c)**Make a sketch of \(Q(x)\) for \(-2\le x \le 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,\ldots,3.9,4\), what is an upper bound on the error in the result?

✍ Show that for any node distribution and any \(x\in[t_0,t_n]\),

(5.2.8)#\[\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.✍ Here we consider a proof of Theorem 5.2.7 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

\[\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 \in (t_k,t_{k+1})\).

**(a)**Show that for some \(s \in (t_k,t_{k+1})\),\[f(x) = y_k + (x-t_k)f'(s).\]**(b)**Show that for some other values \(u\) and \(v\) in \((t_k,t_{k+1})\),\[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.