Type a command or search the notes. Tab cycles completions. Up and down recall history when blank. Escape dismisses, then leaves the prompt.
MATH
[1251]

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

[DEFINITION]

Coordinate vector

Let B=(u1,…,un)B=(u_1,\ldots,u_n) be an ordered basis for a vector space VV. If

u=x1u1+⋯+xnun,u=x_1u_1+\cdots+x_nu_n,

then the coordinate vector of uu with respect to BB is the column

[u]B=(x1⋮xn).[u]_B= \begin{pmatrix} x_1\\ \vdots\\ x_n \end{pmatrix}.
[DEFINITION]

Basis

A set BB in a vector space VV is a basis for VV if it is linearly independent and spans VV.

[DEFINITION]

Span

For a subset SS (possibly infinite) in a vector space VV over FF, the span of SS is the set of all finite linear combinations of vectors in SS:

span⁡(S)={λ1v1+⋯+λkvk:k≥0, v1,…,vk∈S, λ1,…,λk∈F}.\operatorname{span}(S) = \left\{ \lambda_1v_1+\cdots+\lambda_kv_k: k\geq 0,\ v_1,\ldots,v_k\in S,\ \lambda_1,\ldots,\lambda_k\in F \right\}.

For a finite set S={v1,…,vn}S=\{v_1,\ldots,v_n\}, this is the same as allowing one coefficient for each listed vector. When k=0k=0, the sum is the empty sum 00.

Data fitting starts with prescribed pairs (ti,yi)(t_i,y_i) and asks for a function whose values describe the data. The inputs tit_i may be times or another variable, the outputs yiy_i 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.

[METHOD]

Fit a straight line by least squares

For real data (ti,yi)∈R2(t_i,y_i)\in\mathbb R^2, form

M=(t11t21⋮⋮tn1),Y=(y1y2⋮yn).M= \begin{pmatrix} t_1&1\\ t_2&1\\ \vdots&\vdots\\ t_n&1 \end{pmatrix}, \qquad Y= \begin{pmatrix} y_1\\y_2\\\vdots\\y_n \end{pmatrix}.

If at least two input values are distinct, the least-squares straight-line fit y=mt+cy=mt+c has the unique coefficient vector

(mc)=(MTM)−1MTY.\begin{pmatrix}m\\c\end{pmatrix} =(M^{\mathsf T}M)^{-1}M^{\mathsf T}Y.

For a candidate line y=mt+cy=mt+c, the vector of predicted values at the inputs is

M(mc).M\begin{pmatrix}m\\c\end{pmatrix}.

The columns of MM contain the sampled values of tt and 11, so multiplying them by mm and cc produces the line's predicted values. The line need not pass through every data point.

The fitted value vector M(mc)M\binom{m}{c} lies in the column space

col⁡(M)=span⁡{(t1⋮tn),(1⋮1)}.\operatorname{col}(M)=\operatorname{span}\left\{ \begin{pmatrix}t_1\\\vdots\\t_n\end{pmatrix}, \begin{pmatrix}1\\\vdots\\1\end{pmatrix} \right\}.

Least squares chooses mm and cc to minimise the sum of squared vertical errors

E(m,c)=∑i=1n(yi−mti−c)2=∥Y−M(mc)∥2,E(m,c)=\sum_{i=1}^n(y_i-mt_i-c)^2 =\left\|Y-M\begin{pmatrix}m\\c\end{pmatrix}\right\|^2,

where ∥⋅∥\|\cdot\| is the usual Euclidean norm on Rn\mathbb R^n. The problem is therefore to find the point of col⁡(M)\operatorname{col}(M) closest to YY; the formula in the statement is the coordinate calculation for that projection.

At a minimum, differentiating with respect to mm and cc gives

∑i=1nti(yi−mti−c)=0,∑i=1n(yi−mti−c)=0.\sum_{i=1}^n t_i(y_i-mt_i-c)=0, \qquad \sum_{i=1}^n(y_i-mt_i-c)=0.

These are exactly the two entries of

MT(Y−M(mc))=0,MTM(mc)=MTY.M^{\mathsf T}\left(Y-M\binom{m}{c}\right)=0, \qquad M^{\mathsf T}M\binom{m}{c}=M^{\mathsf T}Y.

Thus the residual

Y−M(mc)Y-M\binom{m}{c}

is perpendicular to both columns of MM, hence to their span. If x∗x_* solves the normal equations and xx is any other coefficient vector, this perpendicularity gives

∥Y−Mx∥2=∥Y−Mx∗∥2+∥M(x−x∗)∥2.\|Y-Mx\|^2=\|Y-Mx_*\|^2+\|M(x-x_*)\|^2.

So Mx∗Mx_* is the closest model vector to YY.

When two input values are distinct, ati+b=0at_i+b=0 for every ii forces a=b=0a=b=0, so the columns of MM are independent. Hence MTMM^{\mathsf T}M is invertible and the minimising coefficient vector is unique. Multiplying the normal equations by (MTM)−1(M^{\mathsf T}M)^{-1} gives the coefficient formula in the statement.

[EXAMPLE]

A least-squares line

For (0,2)(0,2), (3,4)(3,4) and (4,4)(4,4),

M=(013141),Y=(244).M= \begin{pmatrix} 0&1\\ 3&1\\ 4&1 \end{pmatrix}, \qquad Y= \begin{pmatrix} 2\\4\\4 \end{pmatrix}.

Here

MTM=(25773),MTY=(2810).M^{\mathsf T}M= \begin{pmatrix}25&7\\7&3\end{pmatrix}, \qquad M^{\mathsf T}Y=\binom{28}{10}.

The determinant of MTMM^{\mathsf T}M is 2626 and solving the normal equations gives

(mc)=126(3−7−725)(2810)=(7/1327/13).\begin{pmatrix}m\\c\end{pmatrix} =\frac1{26} \begin{pmatrix}3&-7\\-7&25\end{pmatrix} \binom{28}{10} =\begin{pmatrix}7/13\\27/13\end{pmatrix}.

Thus the best-fit line is

y=713t+2713.y=\frac{7}{13}t+\frac{27}{13}.

It does not pass through all three data points; it minimises their total squared vertical error.

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 PkP_k means Pk(R)P_k(\mathbb R) in this section.

[DEFINITION]

Polynomial interpolation

Given nn distinct inputs t1,…,tnt_1,\ldots,t_n and prescribed outputs y1,…,yny_1,\ldots,y_n, polynomial interpolation finds the polynomial f∈Pn−1f\in P_{n-1} satisfying

f(ti)=yi,i=1,…,n.f(t_i)=y_i,\qquad i=1,\ldots,n.

Using the standard basis

B=(1,t,t2,…,tn−1),B=(1,t,t^2,\ldots,t^{n-1}),

write

f(t)=a0+a1t+⋯+an−1tn−1.f(t)=a_0+a_1t+\cdots+a_{n-1}t^{n-1}.

The coefficients form the coordinate vector

[f]B=(a0a1⋮an−1).[f]_B= \begin{pmatrix} a_0\\a_1\\\vdots\\a_{n-1} \end{pmatrix}.

The interpolation conditions become a linear system for this coordinate vector.

[METHOD]

Interpolate in the standard polynomial basis

To find the coordinate vector of the interpolating polynomial in B=(1,t,…,tn−1)B=(1,t,\ldots,t^{n-1}):

  1. Form the Vandermonde matrix
A=(1t1t12⋯t1n−11t2t22⋯t2n−1⋮⋮⋮⋮1tntn2⋯tnn−1)A= \begin{pmatrix} 1&t_1&t_1^2&\cdots&t_1^{n-1}\\ 1&t_2&t_2^2&\cdots&t_2^{n-1}\\ \vdots&\vdots&\vdots&&\vdots\\ 1&t_n&t_n^2&\cdots&t_n^{n-1} \end{pmatrix}

and the data vector y=(y1,…,yn)Ty=(y_1,\ldots,y_n)^T. 2. Solve

Ax=y.Ax=y.
  1. Read the solution
x=(a0a1⋮an−1)=[f]Bx= \begin{pmatrix} a_0\\a_1\\\vdots\\a_{n-1} \end{pmatrix} =[f]_B

as the coordinates of the interpolating polynomial.

The iith row imposes the condition f(ti)=yif(t_i)=y_i. Because the inputs are distinct, these conditions determine exactly one coordinate vector.

[THEOREM]

Distinct inputs give a unique interpolant

If t1,…,tnt_1,\ldots,t_n are distinct, the columns of the Vandermonde matrix are linearly independent and span Rn\mathbb R^n. Hence Ax=yAx=y has a unique solution for every data vector yy, so there is exactly one polynomial in Pn−1P_{n-1} satisfying

f(ti)=yi,i=1,…,n.f(t_i)=y_i,\qquad i=1,\ldots,n.
[PROOF]

Suppose a linear combination of the columns is zero. Its coefficients define

p(t)=λ1+λ2t+⋯+λntn−1,p(t)=\lambda_1+\lambda_2t+\cdots+\lambda_nt^{n-1},

and the zero combination says p(ti)=0p(t_i)=0 for every ii. By the factor theorem, each distinct root contributes a factor (t−ti)(t-t_i), so a non-zero polynomial of degree at most n−1n-1 cannot have all nn roots. Hence every λi\lambda_i is zero. The nn columns are independent in Rn\mathbb R^n, so they also span Rn\mathbb R^n, giving a unique solution to Ax=yAx=y.

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.

[DEFINITION]

Lagrange polynomials

For distinct inputs t1,…,tnt_1,\ldots,t_n, the jjth Lagrange polynomial is

ℓj(t)=∏k=1k≠jnt−tktj−tk,j=1,…,n.\ell_j(t)=\prod_{\substack{k=1\\k\ne j}}^n \frac{t-t_k}{t_j-t_k}, \qquad j=1,\ldots,n.

It has degree at most n−1n-1 and satisfies

ℓj(tj)=1,ℓj(tk)=0when k≠j.\ell_j(t_j)=1, \qquad \ell_j(t_k)=0\quad\text{when }k\ne j.

Each factor t−tkt-t_k makes ℓj\ell_j zero at one other input, while dividing by tj−tkt_j-t_k makes that factor equal to 11 at tjt_j. Distinct inputs ensure that no denominator is zero.

[THEOREM]

The Lagrange polynomials form a basis

The set

L=(ℓ1,…,ℓn)L=(\ell_1,\ldots,\ell_n)

is a basis for Pn−1P_{n-1}.

[PROOF]

If

λ1ℓ1(t)+⋯+λnℓn(t)=0,\lambda_1\ell_1(t)+\cdots+\lambda_n\ell_n(t)=0,

evaluate at t=tkt=t_k. Every term vanishes except λkℓk(tk)=λk\lambda_k\ell_k(t_k)=\lambda_k, so λk=0\lambda_k=0. This holds for every kk, so the Lagrange polynomials are linearly independent; with nn polynomials in Pn−1P_{n-1}, they form a basis.

[METHOD]

Interpolate in the Lagrange basis

For data (tj,yj)(t_j,y_j), construct the Lagrange polynomials and set

f(t)=∑j=1nyjℓj(t).f(t)=\sum_{j=1}^n y_j\ell_j(t).

Then f(ti)=yif(t_i)=y_i for every ii, which gives the interpolating polynomial.

The data values are the coordinates in the Lagrange basis, while a0,…,an−1a_0,\ldots,a_{n-1} are the coordinates of the same polynomial in the standard basis:

[f]L=(y1⋮yn),[f]B=(a0⋮an−1).[f]_L= \begin{pmatrix} y_1\\\vdots\\y_n \end{pmatrix}, \qquad [f]_B= \begin{pmatrix} a_0\\\vdots\\a_{n-1} \end{pmatrix}.

The coordinate vector changes with the basis; the polynomial ff does not.

[EXAMPLE]

Estimating a root from an interpolant

For

g(x)=x−9−x,g(x)=x-9^{-x},

use the inputs 0,12,10,\frac12,1. The data values are

g(0)=−1,g(12)=16,g(1)=89.g(0)=-1,\qquad g\left(\frac12\right)=\frac16,\qquad g(1)=\frac89.

The corresponding Lagrange polynomials are

ℓ1(t)=2(t−12)(t−1),ℓ2(t)=−4t(t−1),ℓ3(t)=2t(t−12).\ell_1(t)=2\left(t-\frac12\right)(t-1),\qquad \ell_2(t)=-4t(t-1),\qquad \ell_3(t)=2t\left(t-\frac12\right).

Therefore

p(t)=−ℓ1(t)+16ℓ2(t)+89ℓ3(t)=−1+259t−89t2.\begin{aligned} p(t) &=-\ell_1(t)+\frac16\ell_2(t)+\frac89\ell_3(t)\\ &=-1+\frac{25}{9}t-\frac89t^2. \end{aligned}

Solving p(t)=0p(t)=0 gives

8t2−25t+9=0,t=25±33716.8t^2-25t+9=0, \qquad t=\frac{25\pm\sqrt{337}}{16}.

The root of the interpolating polynomial in the data range is

t≈0.415.t\approx 0.415.

This root is chosen because the sampled values change sign between 00 and 12\frac12; the other quadratic root lies outside the data interval. It approximates the root of gg itself, which is about 0.4080040.408004. Interpolation is exact at the sampled values, not at a zero between sample points.

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.

On this page