From Polynomial Fits to Matrix Equations
Suppose measurements \((x_i,y_i)\), for \(i=1,\dots,m\), are given, and we want a polynomial of degree at most \(d\) that fits them. Write the polynomial in the fixed order of powers \(p(x)=c_0+c_1x+\cdots+c_dx^d\). At each input \(x_i\), the fitted value is a linear combination of the coefficients: \(c_0+c_1x_i+\cdots+c_dx_i^d\). Collecting these equations gives a matrix problem. When there are more measurements than coefficients, an exact fit may not exist; least squares chooses coefficients that minimize the sum of squared vertical residuals.
The columns correspond, in order, to the constant term, the coefficient of \(x\), the coefficient of \(x^2\), and so on. A quadratic fit therefore uses three columns, while a cubic fit uses four. A least-squares coefficient vector minimizes \(\|\mathbf{y}-\mathbf{A}\mathbf{c}\|^2\). The normal equations are \(\mathbf{A}^{\top}\mathbf{A}\mathbf{c}=\mathbf{A}^{\top}\mathbf{y}\); the earlier material on least squares establishes that their solutions are precisely the least-squares coefficient vectors.
For a quadratic fit, the \(i\)th row is \([1,x_i,x_i^2]\). For a cubic fit, it is \([1,x_i,x_i^2,x_i^3]\). These are sometimes called Vandermonde matrices even though some conventions display their transpose. Here the rows are observations and the columns are powers, so \(\mathbf{A}\mathbf{c}\) directly lists the fitted values at the sample inputs.
A Quadratic Fit with Four Measurements
Worked Example: Quadratic Least Squares and Residuals
Fit a quadratic to the data \((-1,-1),(0,4),(1,1),(2,10)\). Use the power order \(1,x,x^2\), then compute the fitted values and residual sum of squares.
The inputs \(-1,0,1,2\) give the design matrix and data vector
For this matrix, the entries of \(\mathbf{A}^{\top}\mathbf{A}\) are sums of powers of the inputs, and those of \(\mathbf{A}^{\top}\mathbf{y}\) are sums of the data values multiplied by those powers. The needed sums are \(\sum 1=4\), \(\sum x_i=2\), \(\sum x_i^2=6\), \(\sum x_i^3=8\), \(\sum x_i^4=18\), \(\sum y_i=14\), \(\sum x_iy_i=22\), and \(\sum x_i^2y_i=40\). Thus the normal equations are
The coefficient vector \((1,2,1)^\top\) satisfies these equations: multiplying the left matrix by it gives \((4+4+6,\ 2+12+8,\ 6+16+18)^\top=(14,22,40)^\top\). The columns of \(\mathbf{A}\) are independent, as will also follow from the distinct-input result below, so the least-squares coefficient vector is unique. The fitted polynomial is \(p(x)=1+2x+x^2\).
Evaluate it at each input: \(p(-1)=1-2+1=0\), \(p(0)=1\), \(p(1)=1+2+1=4\), and \(p(2)=1+4+4=9\). Therefore
Check: the residual is orthogonal to every column: its entries sum to \(0\); their input-weighted sum is \(1+0-3+2=0\); and their squared-input-weighted sum is \(-1+0-3+4=0\). Thus \(\mathbf{A}^{\top}\mathbf{r}=\mathbf{0}\), as required by the normal equations. The residual sum of squares is \((-1)^2+3^2+(-3)^2+1^2=20\).
Why Distinct Inputs Give a Unique Fit
Having enough data points does not by itself guarantee that the coefficients are identifiable. What matters for the Vandermonde matrix is that there are enough distinct inputs. The polynomial fact behind this condition is that a nonzero polynomial of degree at most \(d\) cannot vanish at more than \(d\) distinct real numbers.
Proof. We use induction on the degree. A nonzero constant polynomial has no roots, which proves the claim for degree \(0\). Now suppose the claim holds for degree at most \(d-1\), and let \(q\) be a nonzero polynomial of degree \(d\). If \(q\) has no root, the claim holds. Otherwise let \(a\) be one of its roots. The factor theorem gives \(q(x)=(x-a)s(x)\), where \(s\) is a nonzero polynomial of degree \(d-1\). Every root of \(q\) different from \(a\) is a root of \(s\), because \(x-a\ne0\) at such a root. By the induction hypothesis, \(s\) has at most \(d-1\) distinct roots. Including \(a\), \(q\) has at most \(d\). \(\square\)
Proof. Let \(\mathbf{c}=(c_0,\dots,c_d)^\top\) satisfy \(\mathbf{A}\mathbf{c}=\mathbf{0}\). Define \(q(x)=c_0+c_1x+\cdots+c_dx^d\). By the definition of the rows of \(\mathbf{A}\), the equation \(\mathbf{A}\mathbf{c}=\mathbf{0}\) says \(q(x_i)=0\) for all \(m\) distinct inputs. Since \(m\ge d+1\), the lemma forces \(q\) to be the zero polynomial. Therefore all its coefficients \(c_0,\dots,c_d\) are zero, proving that the columns are independent.
To verify uniqueness of the least-squares fit, full column rank implies that \(\mathbf{A}^{\top}\mathbf{A}\) is invertible: if \(\mathbf{A}^{\top}\mathbf{A}\mathbf{h}=\mathbf{0}\), then \(\|\mathbf{A}\mathbf{h}\|^2=\mathbf{h}^{\top}\mathbf{A}^{\top}\mathbf{A}\mathbf{h}=0\), so \(\mathbf{A}\mathbf{h}=\mathbf{0}\), and column independence gives \(\mathbf{h}=\mathbf{0}\). Let \(\widehat{\mathbf{c}}\) solve the normal equations and let \(\widehat{\mathbf{r}}=\mathbf{y}-\mathbf{A}\widehat{\mathbf{c}}\). Then \(\mathbf{A}^{\top}\widehat{\mathbf{r}}=\mathbf{0}\). For any other coefficient vector \(\widehat{\mathbf{c}}+\mathbf{h}\), its squared residual norm is
If \(\mathbf{h}\ne\mathbf{0}\), column independence gives \(\mathbf{A}\mathbf{h}\ne\mathbf{0}\), so the last term is positive. Hence \(\widehat{\mathbf{c}}\) is the unique minimizer. \(\square\)
Distinctness is a sufficient condition, not a requirement that every problem must satisfy to have some least-squares solution. If inputs repeat, the design matrix may have dependent columns and the coefficient vector may fail to be unique; least-squares solutions still exist, but the polynomial values at the observed inputs need not identify a unique coefficient vector.
A Cubic Fit with Five Measurements
Worked Example: Cubic Least Squares from Five Data Points
Fit a polynomial of degree at most three to \((0,2),(1,-1),(2,15),(3,21),(4,58)\). Find its fitted values and residual sum of squares.
Using columns in the order \(1,x,x^2,x^3\), form
The sums of input powers from \(x^0\) through \(x^6\) are \(5,10,30,100,354,1300,4890\). The data-weighted sums \(\sum y_i,\sum x_iy_i,\sum x_i^2y_i,\sum x_i^3y_i\) are \(95,324,1176,4398\). Substitution into the normal equations gives
Taking \(\mathbf{c}=(1,2,-1,1)^\top\), multiplication gives, row by row, \(5+20-30+100=95\), \(10+60-100+354=324\), \(30+200-354+1300=1176\), and \(100+708-1300+4890=4398\). The inputs are distinct and \(m=5\ge4=d+1\), so the theorem ensures the solution is unique. The fitted polynomial is \(p(x)=1+2x-x^2+x^3\).
Its fitted values at \(0,1,2,3,4\) are \(1,3,9,25,57\), respectively. Thus the residual vector is \((1,-4,6,-4,1)^\top\), and the residual sum of squares is \(1+16+36+16+1=70\).
Check: the residual entries sum to \(1-4+6-4+1=0\). Their weighted sums for \(x,x^2,x^3\) are \(-4+12-12+4=0\), \(-4+24-36+16=0\), and \(-4+48-108+64=0\). Hence \(\mathbf{A}^{\top}\mathbf{r}=\mathbf{0}\), confirming the normal equations. Direct evaluation also checks, for example, \(p(4)=1+8-16+64=57\), so the residual at \(x=4\) is \(58-57=1\).
A Quadratic Fit with Fractional Coefficients
Worked Example: Solving and Checking a Quadratic Fit
Fit a quadratic to \((0,1),(1,2),(2,2),(3,5)\), and calculate its fitted values and residual sum of squares.
The design matrix again has columns \(1,x,x^2\). The sums of powers through degree four are \(4,6,14,36,98\), while \(\sum y_i=10\), \(\sum x_iy_i=21\), and \(\sum x_i^2y_i=55\). Therefore
In scalar form, the equations are \(4c_0+6c_1+14c_2=10\), \(6c_0+14c_1+36c_2=21\), and \(14c_0+36c_1+98c_2=55\). Subtracting the second equation from the first gives \(-2c_0-8c_1-22c_2=-11\), or \(2c_0+8c_1+22c_2=11\). A convenient elimination is to subtract \(\tfrac32\) times the first equation from the second, obtaining \(5c_1+15c_2=6\), or \(c_1+3c_2=6/5\). Subtracting \(\tfrac72\) times the first equation from the third gives \(15c_1+49c_2=20\). Substituting \(c_1=6/5-3c_2\) into this last equation yields \(15(6/5-3c_2)+49c_2=20\), hence \(18+4c_2=20\) and \(c_2=1/2\). Then \(c_1=6/5-3/2=-3/10\). Substitution into the first normal equation gives \(4c_0-18/10+7=10\), so \(c_0=6/5\).
The fitted polynomial is \(p(x)=6/5-(3/10)x+(1/2)x^2\). Evaluating gives \(p(0)=6/5\), \(p(1)=6/5-3/10+1/2=7/5\), \(p(2)=6/5-6/10+2=13/5\), and \(p(3)=6/5-9/10+9/2=24/5\). The residuals are \(-1/5,3/5,-3/5,1/5\), so the residual sum of squares is
Check: the residuals sum to \(0\); their input-weighted sum is \(3/5-6/5+3/5=0\); and their squared-input-weighted sum is \(3/5-12/5+9/5=0\). Thus they are orthogonal to the three design columns, as required for the least-squares solution. For instance, at \(x=3\), the fitted value \(24/5\) plus residual \(1/5\) equals the observed value \(5\).
Column Order and a Common Mistake
A coefficient vector has meaning only relative to its stated column order. For the order \([1,x,x^2]\), the coefficient vector \((1,2,1)^\top\) represents \(1+2x+x^2\). If the columns are changed to \([1,x^2,x]\), the same polynomial is represented instead by \((1,1,2)^\top\). The design matrix and coefficient vector must be changed together; merely swapping columns while retaining the old coefficient vector changes the fit.
Common mistake: Suppose the first worked example is rewritten with columns \([1,x^2,x]\). The correct coefficient vector in that order is \((1,1,2)^\top\), still giving \(1+2x+x^2\). A wrong calculation keeps \((1,2,1)^\top\) and then reads it as though the columns were \([1,x,x^2]\), reporting \(1+x+2x^2\). At \(x=2\), that mistaken polynomial predicts \(1+2+8=11\), whereas the fitted value is \(9\). The correction is to keep the column order and coefficients paired: use \((1,2,1)^\top\) with \([1,x,x^2]\), or \((1,1,2)^\top\) with \([1,x^2,x]\).
Other checks help catch setup errors. Each row should use one observation, and each column should correspond to one power. The data vector must list the measured \(y_i\) values in the same order as the rows. After solving, calculate \(\mathbf{A}\mathbf{c}\) and \(\mathbf{y}-\mathbf{A}\mathbf{c}\); the latter should be orthogonal to every column when the normal equations hold. The residual sum of squares is a sum of squared residuals, so it cannot be negative.
Check Your Understanding
Use the column order \(1,x,x^2,\dots\) throughout, and verify least-squares answers by checking residual orthogonality.
- Write the Vandermonde design matrix for a quadratic fit to inputs \(-2,0,3\).
- For a cubic fit using inputs \(0,1,2,3\), how many rows and columns does the design matrix have?
- Explain why four distinct inputs guarantee independent columns for a cubic Vandermonde matrix.
- For the polynomial \(p(x)=2-x+3x^2\), give its coefficient vector in column order \([1,x^2,x]\).
- What equations can be checked to verify that a residual vector belongs to a least-squares fit?