from numpy import *
from scipy import linalg
from scipy.linalg import norm
from matplotlib.pyplot import *
from prettytable import PrettyTable
from timeit import default_timer as timer
import sys
sys.path.append('fncbook/')
import fncbook as FNC
# This (optional) block is for improving the display of plots.
# from IPython.display import set_matplotlib_formats
# set_matplotlib_formats("svg","pdf")
# %config InlineBackend.figure_format = 'svg'
rcParams["figure.figsize"] = [7, 4]
rcParams["lines.linewidth"] = 2
rcParams["lines.markersize"] = 4
rcParams['animation.html'] = "jshtml" # or try "html5"
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,…,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 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 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