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"
Interpolation is not the only way to use polynomials for global approximation of functions. In Fitting functions to data we saw how to find least-squares polynomial fits to data by solving linear least-squares matrix problems. This idea can be extended to fitting functions.
We can extend least-squares fitting from data to functions by extending several familiar finite-dimensional definitions. The continuous extension of a sum is an integral, which leads to the following.
If we are extending our notion of vectors to include continuous functions, what should serve as an extension of a matrix? One of our most important interpretations of a matrix is the connection to linear combinations. For instance, the Vandermonde-like system Vc≈y from (3.1.2) is the statement
which we want to abbreviate in “matrix”-vector form.
We consider any other expressions involving a quasimatrix to be undefined. It might help to think of F as an ∞×n matrix, which is consistent with the definitions that Fz is a function (∞×1), FTg is a vector (n×1), and FTF is a matrix (n×n). When infinite dimensions combine in a product, we use integrals rather than sums.
The discrete linear least-squares problem of minimizing ∥y−Vc∥2 over all possible c, given matrix V and data vector y, has a solution via the normal equations (3.2.3),
We can now reinterpret (9.4.11) in terms of quasimatrices.
There is no need to supply a proof of Theorem 9.4.1 because it will read exactly the same as for the discrete normal equations. All the effort has gone into making definitions that set up a perfect analogy. In retrospect, all we needed in the original discrete case were linear combinations and inner products.
Equation (9.4.13) becomes much simpler if VTV is diagonal. By our definitions, this would imply that the columns of V are mutually orthogonal in the sense of the function inner product. This is not the case for the monomial functions xj. But there are orthogonal polynomials which do satisfy this property.
For what follows, let Pn⊂S be the set of polynomials of degree n or less.
Here are some key facts that are straightforward to prove.
The results from Theorem 9.4.2 apply to Chebyshev polynomials as well, with orthogonality being in the sense of (9.4.23). Their Gram matrix is given by
The least-squares solution is not the same in the Legendre and Chebyshev cases: both find the nearest approximation to a given f(x), but the norm used to measure distances is not the same.
Interesting properties can be deduced entirely from the orthogonality conditions. The following result will be relevant in Spectrally accurate integration. The same result holds for orthogonal polynomial families with different weight functions, such as the Chebyshev polynomials.
The result of Theorem 9.4.3 holds for orthogonal families of polynomials for other weight functions. The Chebyshev case is unusual in that thanks to (9.4.25), the roots of Tn are known explicitly:
These are known as the Chebyshev points of the first kind. The chief difference between first-kind and second-kind points is that the latter type include the endpoints ±1. Both work well for polynomial interpolation and give spectral convergence.
Both interpolation and the solution of a linear least-squares problem produce a projection of a function into the space of polynomials Pn. In the least-squares case, the close connection with inner products and orthogonality makes the 2-norm, perhaps with a weight function, a natural setting for analysis. Because a constant weight function is the simplest choice, Legendre polynomials are commonly used for least squares.
Interpolation has no easy connection to inner products or the 2-norm. With interpolants a different kind of approximation analysis is more fruitful, often involving the complex plane, in which the max-norm is the natural choice. For reasons beyond the scope of this text, Chebyshev polynomials are typically the most convenient to work with in this context.