Foundations · 5
Linear systems
A linear system A x = b asks which combination of the columns of
A produces b. When there is an exact answer, solve
finds it; when there are more equations than unknowns and no exact answer exists,
least squares finds the closest one. Everything below runs in this page, in both
languages.
Solving A x = b exactly
With a square, invertible A, A.solve(b) returns the one
x that lands on b. Reading the result is awaited, like any
value coming back from the GPU.
No exact answer: least squares
Four points, a line with two unknowns (intercept and slope): no line passes through
all four, so there is no exact x. lstsq returns the one that
misses by the least — the best fit.
Seeing the fit: the least-squares line
The four points do not lie on one line, so lstsq finds the line nearest all of them. Plotted here, it is the straight line least-squares chose — the best compromise.
Your turn: solve the system
The block reports the residual max|A·x − b|, which is zero only when
x actually solves the system. Right now x is just
b. Replace it with the real solution.
What to remember
- A square system
A x = bwith invertibleAhas one exact solution:A.solve(b). - With more equations than unknowns there is usually no exact answer;
A.lstsq(b)returns the closest fit. - Least squares minimises the sum of squared residuals — the line nearest every point.
- In borch:
A.solve(b),A.lstsq(b); both awaited.