Data fitting and polynomial interpolation
Approximate data with a least-squares line, or fit distinct inputs exactly with polynomial coordinates.
This page fits functions to data in two ways. A least-squares line approximates the data and polynomial interpolation fits it exactly by solving for coordinates in a polynomial basis.
Approximation versus exact interpolation
Coordinate vector
Let be an ordered basis for a vector space . If
then the coordinate vector of with respect to is the column
Basis
A set in a vector space is a basis for if it is linearly independent and spans .
Span
For a subset (possibly infinite) in a vector space over , the span of is the set of all finite linear combinations of vectors in :
For a finite set , this is the same as allowing one coefficient for each listed vector. When , the sum is the empty sum .
Data fitting starts with prescribed pairs and asks for a function whose values describe the data. The inputs may be times or another variable, the outputs may be measured quantities and the inputs need not be equally spaced.
An approximation uses a function that is close to the data, while interpolation requires an exact fit at every supplied input. Exact fitting is developed below as polynomial interpolation. Taylor polynomials are a different construction, matching derivative data at one centre.
Least squares as a projection
Least squares is an orthogonal projection of the data vector onto a space of model values. Projection is developed formally in the next chapter, so the normal equations below are derived directly by minimising a function of the line's two coefficients.
Fit a straight line by least squares
For real data , form
If at least two input values are distinct, the least-squares straight-line fit has the unique coefficient vector
For a candidate line , the vector of predicted values at the inputs is
The columns of contain the sampled values of and , so multiplying them by and produces the line's predicted values. The line need not pass through every data point.
The fitted value vector lies in the column space
Least squares chooses and to minimise the sum of squared vertical errors
where is the usual Euclidean norm on . The problem is therefore to find the point of closest to ; the formula in the statement is the coordinate calculation for that projection.
At a minimum, differentiating with respect to and gives
These are exactly the two entries of
Thus the residual
is perpendicular to both columns of , hence to their span. If solves the normal equations and is any other coefficient vector, this perpendicularity gives
So is the closest model vector to .
When two input values are distinct, for every forces , so the columns of are independent. Hence is invertible and the minimising coefficient vector is unique. Multiplying the normal equations by gives the coefficient formula in the statement.
A least-squares line
For , and ,
Here
The determinant of is and solving the normal equations gives
Thus the best-fit line is
It does not pass through all three data points; it minimises their total squared vertical error.
[routine]
Interpolation in a standard basis
Exact fitting becomes a coordinate problem when the chosen functions form a basis of a polynomial space. The data are real, so means in this section.
Polynomial interpolation
Given distinct inputs and prescribed outputs , polynomial interpolation finds the polynomial satisfying
Using the standard basis
write
The coefficients form the coordinate vector
The interpolation conditions become a linear system for this coordinate vector.
Interpolate in the standard polynomial basis
To find the coordinate vector of the interpolating polynomial in :
- Form the Vandermonde matrix
and the data vector . 2. Solve
- Read the solution
as the coordinates of the interpolating polynomial.
The th row imposes the condition . Because the inputs are distinct, these conditions determine exactly one coordinate vector.
Distinct inputs give a unique interpolant
If are distinct, the columns of the Vandermonde matrix are linearly independent and span . Hence has a unique solution for every data vector , so there is exactly one polynomial in satisfying
Suppose a linear combination of the columns is zero. Its coefficients define
and the zero combination says for every . By the factor theorem, each distinct root contributes a factor , so a non-zero polynomial of degree at most cannot have all roots. Hence every is zero. The columns are independent in , so they also span , giving a unique solution to .
Interpolation in a basis adapted to the data
The same interpolating polynomial has simpler coordinates in a basis whose polynomials each pick out one data input.
Lagrange polynomials
For distinct inputs , the th Lagrange polynomial is
It has degree at most and satisfies
Each factor makes zero at one other input, while dividing by makes that factor equal to at . Distinct inputs ensure that no denominator is zero.
The Lagrange polynomials form a basis
The set
is a basis for .
- [1]:Lagrange polynomials
- [2]:Basis
- [3]:Proof
- [4]:Interpolate in the Lagrange basis
If
evaluate at . Every term vanishes except , so . This holds for every , so the Lagrange polynomials are linearly independent; with polynomials in , they form a basis.
Interpolate in the Lagrange basis
For data , construct the Lagrange polynomials and set
Then for every , which gives the interpolating polynomial.
The data values are the coordinates in the Lagrange basis, while are the coordinates of the same polynomial in the standard basis:
The coordinate vector changes with the basis; the polynomial does not.
Estimating a root from an interpolant
For
use the inputs . The data values are
The corresponding Lagrange polynomials are
Therefore
Solving gives
The root of the interpolating polynomial in the data range is
This root is chosen because the sampled values change sign between and ; the other quadratic root lies outside the data interval. It approximates the root of itself, which is about . Interpolation is exact at the sampled values, not at a zero between sample points.
[exam]
Interpolation is exact at the supplied inputs, but accuracy between the inputs is a separate question and extrapolation beyond them can be poor. For equally spaced inputs, high-degree interpolants can develop large oscillations near the ends of the interval; this is Runge's phenomenon.