Least Squares Calculator

This least squares calculator fits the line of best fit to your data and shows the sums, the normal equations, the sum of squared errors and every residual.

Checks on your data

  • Least squares is sensitive to outliers, because it squares each error. Check the residual chart for a point far from zero.

Results

Slope b₁
2.02
Intercept b₀
-0.02
Least-squares line
y = −0.02 + 2.02x
Sum of squared errors (SSE)
0.128
R²
0.998211
Root mean squared error
0.146059
average miss, in y units
Data and the least-squares line
510123456xy
Residuals (y − ŷ): they sum to about zero
00.050.10.151: 0.110.12: -0.122-0.123: 0.1630.164: -0.264-0.265: 0.0250.026: 0.160.1
Least-squares working table
xyx·yx²Fitted ŷResidual y − ŷ
12.12.1120.1
23.97.844.02-0.12
36.218.696.040.16
47.831.2168.06-0.26
510.150.52510.080.02
612.273.23612.10.1
Σ42.3183.49142.32.6645 × 10^-15
Show step-by-step solution
  1. n = 6, Σx = 21, Σy = 42.3, Σxy = 183.4, Σx² = 91.
  2. Least squares chooses the line that makes Σ(y − ŷ)² as small as possible. Setting the derivatives to zero gives the normal equations: Σy = n·b₀ + b₁·Σx and Σxy = b₀·Σx + b₁·Σx².
  3. Slope b₁ = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²) = (1100.4 − 888.3) ÷ (546 − 441) = 2.02.
  4. Intercept b₀ = (Σy − b₁Σx) ÷ n = -0.02.
  5. Error: SSE = Σ(y − ŷ)² = 0.128. No other straight line gives a smaller value.
MinimisesΣ(y − ŷ)²
Slope(nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²)
Interceptȳ − b₁x̄
Residuals sum to0

What Is Least Squares?

The least squares method chooses the straight line that makes the sum of squared vertical errors as small as possible. Setting the derivatives of that sum to zero gives two normal equations whose solution is the slope and the intercept.

Enter your x and y values to get the slope, intercept, SSE, R² and root mean squared error. A working table lists x·y, x², each fitted value and each residual, so you can follow the arithmetic.

Add an x value to predict y, but stay within the range of your data.

How to Use the Least Squares Calculator

  1. Enter the x values.
  2. Enter the y values.
  3. Optionally enter an x to predict at.
  4. Read the line, SSE and the working table.

Least Squares Formula

Slope

b1=n∑xy−∑x∑yn∑x2−(∑x)2b_1=\frac{n\sum xy-\sum x\sum y}{n\sum x^2-(\sum x)^2}

Intercept

b0=∑y−b1∑xnb_0=\frac{\sum y-b_1\sum x}{n}

Sum of squared errors

SSE=∑(y−y^)2SSE=\sum (y-\hat y)^2

Least Squares Variables Explained

SymbolMeaningNotes
b₁Slope
b₀Intercept
SSESum of squared errors
ŷFitted value

Least Squares Worked Example

For x = 1 to 6 and y = 2.1, 3.9, 6.2, 7.8, 10.1, 12.2: n = 6, Σx = 21, Σy = 42.3, Σxy = 183.4 and Σx² = 91. Then b₁ = (6 × 183.4 − 21 × 42.3) ÷ (6 × 91 − 441) = 212.1 ÷ 105 = 2.02, and b₀ = (42.3 − 2.02 × 21) ÷ 6 = −0.02. The line is y = −0.02 + 2.02x.

With the example values loaded in the calculator above, the steps are:

  1. n = 6, Σx = 21, Σy = 42.3, Σxy = 183.4, Σx² = 91.
  2. Least squares chooses the line that makes Σ(y − ŷ)² as small as possible. Setting the derivatives to zero gives the normal equations: Σy = n·b₀ + b₁·Σx and Σxy = b₀·Σx + b₁·Σx².
  3. Slope b₁ = (nΣxy − ΣxΣy) ÷ (nΣx² − (Σx)²) = (1100.4 − 888.3) ÷ (546 − 441) = 2.02.
  4. Intercept b₀ = (Σy − b₁Σx) ÷ n = -0.02.
  5. Error: SSE = Σ(y − ŷ)² = 0.128. No other straight line gives a smaller value.

Result: Slope b₁ = 2.02.

How to Read the Least-Squares Line

For x = 1 to 6 and y = 2.1, 3.9, 6.2, 7.8, 10.1, 12.2 the line is y = −0.02 + 2.02x. Each extra unit of x adds about 2.02 to y, and the sum of squared errors is 0.128, which gives an R² of 0.998.

The residuals sum to zero, and the residual chart shows how far each point sits from the line. A residual that stands out marks a point the line fits poorly. A pattern in the residuals, such as a curve, says a straight line is the wrong model.

Where Least Squares Is Used

Line of best fit

Fit a straight line to measured data.

Showing the working

See each sum, the normal equations and the residuals.

Prediction

Predict y at a new x within the data range.

Residual check

Look for outliers and curved patterns.

Common Least Squares Mistakes to Avoid

  • Plot the points first. A straight line only suits roughly linear data.
  • The line passes through the point (x̄, ȳ).
  • One outlier can pull the line. Check the residual chart.
  • Do not extrapolate far past your x values.

Least Squares Terms and Definitions

Residual
The vertical gap between an observed y and the fitted ŷ.

Least Squares FAQs

What does least squares mean?

It means choosing the line whose squared vertical errors add up to the smallest possible total.

Why square the errors?

Squaring stops positive and negative errors cancelling and gives a smooth problem with a single exact answer.

What are the normal equations?

They are two equations, Σy = nb₀ + b₁Σx and Σxy = b₀Σx + b₁Σx², whose solution gives the slope and intercept.

Is least squares the same as linear regression?

Simple linear regression uses least squares to fit the line. Least squares is the method, and regression is the wider modelling idea.

Related calculators

Last updated 2026-11-09. See our disclaimer.