Tutorials › Linear Algebra › Polynomial Curve Fitting With Vandermonde Matrices

Least Squares and QR Factorization · Tutorial 693 of 1000

Polynomial Curve Fitting With Vandermonde Matrices

Build Vandermonde matrices for polynomial fits and compute their least-squares coefficients, fitted values, and residual sums of squares.

Intermediate 12 min read

What You'll Learn

  • Construct quadratic and cubic Vandermonde design matrices from sample inputs.
  • Use the normal equations to calculate polynomial fit coefficients.
  • Find fitted values and residuals for each data point.
  • Compute and verify a residual sum of squares.
  • Explain why distinct sample inputs give a unique least-squares fit when there are enough data points.

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.

Definition: For sample inputs \(x_1,\dots,x_m\in\mathbb{R}\) and a degree bound \(d\), the Vandermonde design matrix for the polynomial basis \(1,x,\dots,x^d\) is the \(m\times(d+1)\) matrix \(\mathbf{A}\) whose \(i\)th row is \([1,\ x_i,\ x_i^2,\ \dots,\ x_i^d]\). With \(\mathbf{y}=(y_1,\dots,y_m)^\top\) and \(\mathbf{c}=(c_0,\dots,c_d)^\top\), the fitted-value vector is \(\mathbf{A}\mathbf{c}\), the residual vector is \(\mathbf{r}=\mathbf{y}-\mathbf{A}\mathbf{c}\), and the residual sum of squares is \(\|\mathbf{r}\|^2=\sum_{i=1}^{m}(y_i-p(x_i))^2\).

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

$$ \mathbf{A}= \begin{bmatrix} 1&-1&1\\ 1&0&0\\ 1&1&1\\ 1&2&4 \end{bmatrix}, \qquad \mathbf{y}= \begin{bmatrix}-1\\4\\1\\10\end{bmatrix}. $$

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

$$ \begin{bmatrix} 4&2&6\\ 2&6&8\\ 6&8&18 \end{bmatrix} \begin{bmatrix}c_0\\c_1\\c_2\end{bmatrix} = \begin{bmatrix}14\\22\\40\end{bmatrix}. $$

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

$$ \mathbf{A}\mathbf{c}= \begin{bmatrix}0\\1\\4\\9\end{bmatrix}, \qquad \mathbf{r}=\mathbf{y}-\mathbf{A}\mathbf{c} = \begin{bmatrix}-1\\3\\-3\\1\end{bmatrix}. $$

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.

Lemma: A nonzero polynomial over \(\mathbb{R}\) of degree at most \(d\) has at most \(d\) distinct real roots.

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\)

Theorem: If \(x_1,\dots,x_m\) are distinct real numbers and \(m\ge d+1\), then the columns of the Vandermonde design matrix for \(1,x,\dots,x^d\) are linearly independent. Consequently, the least-squares polynomial of degree at most \(d\) is unique.

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

$$ \|\widehat{\mathbf{r}}-\mathbf{A}\mathbf{h}\|^2 = \|\widehat{\mathbf{r}}\|^2 -2\widehat{\mathbf{r}}^{\top}\mathbf{A}\mathbf{h} +\|\mathbf{A}\mathbf{h}\|^2 = \|\widehat{\mathbf{r}}\|^2+\|\mathbf{A}\mathbf{h}\|^2. $$

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

$$ \mathbf{A}= \begin{bmatrix} 1&0&0&0\\ 1&1&1&1\\ 1&2&4&8\\ 1&3&9&27\\ 1&4&16&64 \end{bmatrix}, \qquad \mathbf{y}= \begin{bmatrix}2\\-1\\15\\21\\58\end{bmatrix}. $$

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

$$ \begin{bmatrix} 5&10&30&100\\ 10&30&100&354\\ 30&100&354&1300\\ 100&354&1300&4890 \end{bmatrix} \begin{bmatrix}c_0\\c_1\\c_2\\c_3\end{bmatrix} = \begin{bmatrix}95\\324\\1176\\4398\end{bmatrix}. $$

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

$$ \begin{bmatrix} 4&6&14\\ 6&14&36\\ 14&36&98 \end{bmatrix} \begin{bmatrix}c_0\\c_1\\c_2\end{bmatrix} = \begin{bmatrix}10\\21\\55\end{bmatrix}. $$

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

$$ \left(-\frac15\right)^2+ \left(\frac35\right)^2+ \left(-\frac35\right)^2+ \left(\frac15\right)^2 = \frac{1+9+9+1}{25} = \frac45. $$

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.

  1. Write the Vandermonde design matrix for a quadratic fit to inputs \(-2,0,3\).
  2. For a cubic fit using inputs \(0,1,2,3\), how many rows and columns does the design matrix have?
  3. Explain why four distinct inputs guarantee independent columns for a cubic Vandermonde matrix.
  4. For the polynomial \(p(x)=2-x+3x^2\), give its coefficient vector in column order \([1,x^2,x]\).
  5. What equations can be checked to verify that a residual vector belongs to a least-squares fit?

Test yourself: Linear Algebra final exam practice

Ready to check what you know? Work a full-length practice final, then open the complete worked solutions.

Want all 1,114 practice problems?

This tutorial covers one topic. Calc Pass covers the full Calculus AB & BC curriculum (the core of Calculus I and II): full review, every practice question, and step-by-step solutions to each one.

QR code to download the Calc Pass app

Get the Calc Pass App

Scan to download and practice on the go, with instant scoring.

Calc Pass Volume 1: 1,114 Practice Questions book cover

Vol. 1: 1,114 Questions

The complete practice question bank, organized by AB & BC topic.

View on Amazon
Calc Pass Volume 2: Solutions Manual book cover

Vol. 2: Solutions Manual

Fully worked solutions to every question in Volume 1.

View on Amazon