Linear Algebra / Unit 6, Orthogonality / Topic 6.4
Unit 6. Orthogonality
This topic finds the best approximate solution of a system that has more equations than unknowns and no exact solution. The method is called least squares. Its main use is fitting a line or a curve to measured data.
You need Topics 2.3, 6.1, and 6.3 for this topic.
Measurements usually give more equations than unknowns. Suppose that a quantity y is expected to depend on the time t along a line, y = C + Dt, and three measurements give the points (1, 1), (2, 2), and (3, 2). Each point gives one equation for the two unknowns C and D.
C + D = 1 C + 2D = 2 C + 3D = 2In matrix form, Ax = b with
A = 111213 x = CD b = 122The three points are not on one line, so the system has no solution. From Topic 2.3, this is the usual case when there are more equations than unknowns. The vector b is not in the column space of A.
When no x makes Ax equal to b, look for the x that makes Ax as close to b as possible. The difference e = b − Ax is called the error, or the residual. Its length squared is the sum of the squares of its components.
|e|² = e₁² + e₂² + … + em²The least squares solution is the vector x̂ that makes this sum as small as possible.
Topic 6.1 already solved this problem. The vectors Ax are the vectors of the column space, and the vector of the column space that is closest to b is the projection p = Ax̂. The error is then perpendicular to the column space.
The least squares solution satisfies the normal equations AT Ax̂ = ATb.
To obtain the normal equations, multiply both sides of the system Ax = b by AT. The new system always has a solution, and the solution is unique when the columns of A are independent.
For the three points of Part 1,
AT A = 111123 111213 = 36614the left side of the normal equationsATb = 111123 122 = 511the right side of the normal equations 3C + 6D = 5the first normal equation6C + 14D = 11the second normal equation2D = 1subtract 2 times the first equation from the secondD = 1/2divide both sides by 2C = (5 − 3)/3 = 2/3the first equation, with 6D = 3y = 2/3 + 1/2tthe lineThis is the line that is closest to the three points, in the sense that the sum of the squares of the vertical distances is as small as possible.
| t | Measured y | Value on the line | Error |
|---|---|---|---|
| 1 | 1 | 7/6 | −1/6 |
| 2 | 2 | 5/3 | 1/3 |
| 3 | 2 | 13/6 | −1/6 |
The error vector is e = (−1/6, 1/3, −1/6). It is perpendicular to both columns of A.
−1/6 + 1/3 − 1/6 = 0the dot product with the column (1, 1, 1)−1/6 + 2/3 − 1/2 = 0the dot product with the column (1, 2, 3)1/36 + 4/36 + 1/36 = 1/6the sum of the squares of the errorsNo other line gives a smaller sum.
Try it: your line and the least squares line
Drag the four points to set the data. Move the sliders to change the intercept C and the slope D of the blue line, and try to make the sum of the squares of the errors as small as you can. Then press the button to see the least squares line in green.
The sum of the squares is a function of C and D.
S(C, D) = (C + D − 1)² + (C + 2D − 2)² + (C + 3D − 2)²At its minimum, the derivative with respect to C is 0 and the derivative with respect to D is 0. Calculating these two derivatives and dividing by 2 gives 3C + 6D = 5 and 6C + 14D = 11. These are the normal equations again. The geometric argument with the projection and the calculus argument lead to the same system.
The method works whenever the unknowns appear linearly, even if the curve is not a line. Each quantity that multiplies an unknown gives one column of A.
| Model | Unknowns | Columns of A |
|---|---|---|
| y = C + Dt | C, D | 1 and t |
| y = C + Dt + Et² | C, D, E | 1, t, and t² |
| y = Dt, a line through the origin | D | t |
| y = C cos t + D sin t | C, D | cos t and sin t |
The word linear in linear algebra refers to the unknowns. A parabola is not a line, but fitting a parabola is still a linear problem, because C, D, and E appear only to the first power.
With one unknown, A has one column a, and the normal equations give the formula for the projection onto a line.
D = (a · b)/(a · a)For example, Hooke’s law says that the force of a spring is F = kx. If the stretches 1, 2, and 3 cm give the measured forces 2.1, 3.9, and 6.2 N, then
k = ((1)(2.1) + (2)(3.9) + (3)(6.2))/(1 + 4 + 9) = 28.5/14 ≈ 2.04 N/cmIf A = QR, as in Topic 6.3, then AT A = RT QT QR = RT R, and the normal equations simplify.
Rx̂ = QTbThe system is solved by back substitution. Computer programs use this form, because forming AT A makes rounding errors larger and the orthogonal matrix Q does not.
If the columns of A are dependent, then AT A has no inverse, and the normal equations have infinitely many solutions. All of them give the same projection p, but the coefficients are not determined. This happens, for example, when a line is fitted to data that were all measured at the same time t. Topic 7.3 shows how to choose one solution in this case.
| Term | Meaning |
|---|---|
| Residual | e = b − Ax |
| Least squares solution | The x̂ that makes |e|² as small as possible |
| Normal equations | AT Ax̂ = ATb |
| Fitted values | p = Ax̂, the projection of b onto the column space |
| With QR | Rx̂ = QTb |
| Line through the origin | D = (a · b) / (a · a) |
Work each problem on paper first. Then open the answer to check your work.
A = 10111213 and b = (1, 3, 4, 4).
AT A = 46614ATb = (12, 23)4C + 6D = 12the first normal equation6C + 14D = 23the second normal equation5D = 5subtract 1.5 times the first equation from the secondD = 1C = (12 − 6)/4 = 1.5the first equation, with D = 1y = 1.5 + tthe line
The values on the line are 1.5, 2.5, 3.5, and 4.5, so e = (−0.5, 0.5, 0.5, −0.5).
−0.5 + 0.5 + 0.5 − 0.5 = 0the dot product with the column (1, 1, 1, 1)(0)(−0.5) + (1)(0.5) + (2)(0.5) + (3)(−0.5) = 0the dot product with the column (0, 1, 2, 3)
A is the single column (1, 1, 1).
C = (4 + 5 + 9)/3 = 6
The least squares fit of a constant is the average of the measurements.
The column is a = (1, 4, 9).
a · b = 0.5 + 8.4 + 39.6 = 48.5a · a = 1 + 16 + 81 = 98k = 48.5/98 ≈ 0.495 m/s²acceleration = 2k ≈ 0.99 m/s²because d = (1/2)at²
The columns are 1, t, and t²: A = 1−11100111124.
The matrix A for the model y = C + Dt has a column of ones and a column with the times. The instruction np.ones(3) makes a vector of three ones, and np.column_stack builds a matrix from its columns. The code below uses the three points of Part 1.
Python
The result has the rows 1, 1 and 1, 2 and 1, 3.
The expression A.T is the transpose of A, the @ sign multiplies matrices and vectors, and np.linalg.solve solves a system. The code below solves ATAx̂ = ATb.
Python
The result is [0.66666667 0.5], so C = 2/3 and D = 1/2, as in Part 3.
Now fit a line to the four points of Practice Problem 1: (0, 1), (1, 3), (2, 4), and (3, 4). Three lines have to change, because the column of ones needs four entries.
Python
The code below calculates the error vector for the three points of Part 1, tests that it is perpendicular to the columns of A, and adds the squares of its components. Two stars mean a power, and np.sum adds the numbers in a list.
Python
The errors are −1/6, 1/3, and −1/6. The second result is two numbers such as 4.4e-16, which are rounding errors for 0. The sum of the squares is 1/6, which is about 0.167.
The next box tests a different line, y = 0.5 + 0.5t, on the same data. Its sum of squares is larger. Change the two numbers in the line that starts with x to any other values, and try to get a sum below 0.17.
Python
The instruction np.polyfit fits a polynomial by least squares. Its inputs are the times, the measured values, and the degree of the polynomial. A line has the degree 1. The result lists the coefficient of the highest power first, so for a line it gives the slope D and then the intercept C.
Python
The result is [0.5 0.66666667]: the slope 1/2 and the intercept 2/3.
For the model y = C + Dt + Et², the matrix A gets a third column with the squares of the times. The data below lie close to a parabola.
Python
The matrix A is the one of Practice Problem 6. The fitted parabola is about y = 0.945 − 0.065t + 1.075t².
With one unknown, the least squares solution is a ratio of two dot products. The code below finds the spring constant of Part 5. The instruction np.dot calculates a dot product.
Python
The result is about 2.04 N/cm.
Now solve Practice Problem 5. The model is d = kt², so the column holds the squares of the times 1, 2, and 3, and the measured distances are 0.5, 2.1, and 4.4. Change the first two lines.
Python
This quiz has 5 questions on this topic. A question asks you either to choose one answer or to type numbers. The page grades your answers and explains each one. A number can be typed as a whole number, a decimal, a fraction such as 1/3, or a calculation such as sqrt(18)/2 or arccos(1/2). The page shows the value of a calculation next to the box. Angles in a calculation are in degrees. The Calculator button under each question opens a calculator. Your answers are saved in your browser, so they are still on the page when you come back to it.
Choose how your answers are checked
1. Which equations are the normal equations of the system Ax = b?
To obtain the normal equations, multiply both sides of Ax = b by AT.
2. Three measurements of one quantity give 2, 3, and 7. Find the constant C that fits the three measurements best in the least squares sense.
The matrix A is the one column (1, 1, 1), and b = (2, 3, 7).C = (a · b)/(a · a) = 12/3 = 4This is the average of the measurements.
3. Find the least squares line y = C + Dx for the points (0, 0), (1, 2), and (2, 2). You can type fractions.
The columns of A are (1, 1, 1) and (0, 1, 2), and b = (0, 2, 2).3C + 3D = 4the first normal equation3C + 5D = 6the second normal equation2D = 2subtract the first equation from the secondD = 1C = (4 − 3)/3 = 1/3the first equation, with D = 1
4. Hooke’s law says that the force of a spring is F = kx. The stretches 1 cm and 2 cm give the measured forces 3 N and 5 N. Find the least squares value of k, in newtons per centimeter.
With a = (1, 2) and b = (3, 5):k = (a · b)/(a · a)k = (3 + 10)/(1 + 4) = 13/5 = 2.6
5. The least squares error is e = b − Ax̂. The error is perpendicular to which vectors?
The projection Ax̂ is the vector of the column space that is closest to b. The error is then perpendicular to the column space, so it is perpendicular to each column of A.
Next: Topic 7.1, Symmetric Matrices and the Spectral Theorem