Matrix Least Squares Calculator

Solve rectangular least-squares problems min ‖Ax − b‖.

FreeNo accountOn your device

Runs in your browser. How to verify

Loading tool…

About this tool

Enter a tall matrix A and a vector b to find the x that minimises |Ax − b|. With a column of ones and x-values, it returns the intercept and slope of the best-fit line.

How to use

1
Step 1
Type A with a first column of 1s and a second column of x-values.
2
Step 2
Type b as a column, one y-value per line.
3
Step 3
Choose least_squares.
4
Step 4
Press Calculate.

How it works

When there are more equations than unknowns, no exact solution exists. The least squares solution solves the normal equations AᵀAx = Aᵀb, which makes the sum of squared residuals as small as possible. For a straight-line fit, the result matches linear regression.

x = (AᵀA)⁻¹ Aᵀ b

Worked example

Line through four points

Input
A = [1 1; 1 2; 1 3; 1 4], b = [2; 3; 5; 7]
Output
[0; 1.7]
Walkthrough
The intercept is 0 and the slope is 1.7, so y = 1.7x. The points (1,2), (2,3), (3,5), (4,7) give the same line in the regression calculator.

Limitations

  • Entries must be real numbers; complex entries are refused.
  • The columns of A should be independent, otherwise AᵀA cannot be inverted.
  • b must be a single column with one value per row of A.

Questions

What is the least squares line for (1,2), (2,3), (3,5), (4,7)?
y = 1.7x, intercept 0 and slope 1.7.
What is the best fit for (1,1), (2,2), (3,2)?
Intercept 0.666667 and slope 0.5.
Is this the same as linear regression?
For a line, yes. Putting 1s in the first column of A gives the same slope and intercept.

Features

  • Overdetermined systems
  • Matches linear regression
  • Runs in your browser

Related resources

Guides and videos from around the web—opens on YouTube or the publisher’s site.

  • Vectors | Essence of linear algebra

    3Blue1Brown

    Linear algebra intuition for matrix and vector tools.

    Watch on YouTube
  • Introduction to scientific notation

    Khan Academy

    Scientific notation for calculator and number tools.

    Watch on YouTube
  • Data Index

    Math is Fun

    Stats and probability explainers for data tools.

  • Euler's Number (e)

    Math is Fun

    e for exponential and scientific calculator keys.