borch

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

If a block above errors, that is worth knowing. Every example on this page runs against the same library the tests run against — nothing here is a screenshot. Press Reset to get the original code back.