A727 of 56
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

A7 · Lesson 27 · Least squares and projection

The best answer when there's no exact one

Six measurements of current at six voltages give six equations for one unknown, the resistance. With measurement noise they can't all be true at once: the system has no exact solution. Yet there's clearly a best answer. Least squares finds it, and its geometry, dropping a perpendicular, is one of the most useful pictures in all of engineering.

Play

  • The dots are six noisy measurements. The line I=sV goes through the origin with slope s, the conductance.
  • Move s to make the line fit the dots as well as you can. The strip shows the sum of the squared vertical misses.
  • Find the smallest total you can. Compare your slope with the least-squares slope in the strip.

Predict

The best slope minimizes the sum of squared misses. At that slope, what is true of the misses?

Name it

Least squares is a projection

Write the measurements as an overdetermined system A𝐱≈𝐛: more equations than unknowns. For the line through the origin, A is the column of voltages, 𝐱 is the single unknown s, and 𝐛 is the column of currents.

No 𝐱 makes A𝐱=𝐛 exactly, so choose the one that makes the residual 𝐫=𝐛−A𝐱 as short as possible. The closest point A𝐱 to 𝐛 is where 𝐫 is perpendicular to every column of A:

A𝖳(𝐛−A𝐱)=0⟹A𝖳A𝐱=A𝖳𝐛

These are the normal equations. For a single column they reduce to

s=∑VkIk∑Vk2

the formula in the strip. For a line with an intercept, A has a second column of ones, and the normal equations are 2 × 2. This is exactly what np.polyfit solved in A4.

Projection. A𝐱 is the projection of 𝐛 onto the space the columns of A can reach. In two dimensions, projecting 𝐛 onto the direction of 𝐚 gives 𝐚⋅𝐛𝐚⋅𝐚𝐚: the same formula as s.

In practice. Solve least-squares problems with np.linalg.lstsq(A, b), which avoids forming A𝖳A (that step squares the condition number and loses accuracy). The same mathematics fits curves, estimates channels in a radio receiver and locates you from GPS satellites.

Worked example

Fit y=sx to (1,2.1), (2,3.9), (3,6.2).

Show the solution
  1. The formula. s=∑xy∑x2
  2. Plug in. s=2.1+7.8+18.61+4+9=28.514=2.036

Answer the best slope is s=2.036.

Worked example

Fit y=c+mx to (0,1), (1,3), (2,4), and check the residuals.

Show the solution
  1. Normal equations. [3335][cm]=[811]
  2. Solve. They give m=1.5 and c=1.167.
  3. Residuals. 1−1.167=−0.167 3−2.667=0.333 4−4.167=−0.167
  4. Check them. They add to zero and are perpendicular to (0,1,2) too, as the projection requires.

Answer y=1.167+1.5x, with residuals −0.167, 0.333 and −0.167.

Worked example

A capacitor's voltage is measured as 10, 6.1 and 3.6 V at t = 0, 1 and 2 s. Fit v=V0e−t/τ.

Show the solution
  1. Take logarithms. ln⁡v=ln⁡V0−t/τ is a straight line in t, so fit ln⁡v = 2.3026, 1.8083, 1.2809 by least squares.
  2. Slope. With t¯=1: m=∑(t−t¯)(ln⁡v−ln⁡v¯)∑(t−t¯)2=−1.02172=−0.5108
  3. Intercept. The intercept is 2.3081.
  4. Back to the exponential. τ=1/0.5108=1.958sV0=e2.3081=10.06V
  5. A caution. Fitting the logarithm weights the small late readings more heavily than a direct fit would.

Answer V0 = 10.06 V and τ = 1.958 s.

Check

Two measurements: 6 mA at 2 V and 14 mA at 4 V. What is the least-squares slope s (in mA per volt) of a line I=sV through the origin?

Write units the way you normally would: 1.5 mA/V, 1500 µA/V and 0.0015 A/V are the same answer.

Check

Project 𝐛=(7,9) onto the direction of 𝐚=(3,1). What is the first component of the projection?

Check

Fit a line with an intercept, I=sV+c, to the six measurements in the sim using np.linalg.lstsq. Set s and c.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Fit lines to (1,1), (2,3), (3,7).

(a)

What slope does y=sx, through the origin, get?

(b)

What slope does y=c+mx get?

Check

Fit y=sx to (1,a) and (2,b).

Step 1

With A=(1,2), what is AT𝐲?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Check

Fit y=sx by least squares to (1,1.9), (2,3.8) and (3,5.7). What is the sum of the squared residuals at the best s?

Explain why

Why is the least-squares residual perpendicular to the columns of A?

Practice

Keep practicing

6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.