Matrix Least Squares Calculator
Solve rectangular least-squares problems min ‖Ax − b‖.
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
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)?
What is the best fit for (1,1), (2,2), (3,2)?
Is this the same as linear regression?
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 YouTubeIntroduction 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.