Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools

How to Fit a Line or Curve to Data

Measured data never sit exactly on a line. Write one equation per data point, accept that the system has no exact solution, and let least squares choose the coefficients that make the total squared error smallest.

Recognize the problem

Trigger words: line of best fit, regression line, least-squares line, best-fitting parabola, fit a model, predict. A table of (t,y)(t, y) points and a model with unknown coefficients: y=c0+c1ty = c_0 + c_1t, y=c0+c1t+c2t2y = c_0 + c_1t + c_2t^2, y=kty = kt.

Set it up

  1. 1. ModelWrite the model with its unknown coefficients: y=c0+c1ty = c_0 + c_1t for a line, add c2t2c_2t^2 for a parabola, drop c0c_0 for a line through the origin.
  2. 2. EquationsSubstitute each data point. Each point gives one equation that is linear in the coefficients, even when the model is curved in tt.
  3. 3. MatrixStack the equations as Ac=yA\mathbf{c} = \mathbf{y}: one row per point, one column per coefficient (a column of 11s, a column of tt values, a column of t2t^2 values). y\mathbf{y} holds the measured values.
  4. 4. Normal equationsForm ATAA^TA and ATyA^T\mathbf{y} (normal equations). For a line, ATA=(n∑t∑t∑t2)A^TA = \begin{pmatrix} n & \sum t \\ \sum t & \sum t^2 \end{pmatrix} and ATy=(∑y,∑ty)A^T\mathbf{y} = (\sum y, \sum ty).
  5. 5. CheckYou need more points than coefficients, and at least two different tt values for a line; otherwise ATAA^TA is singular.
→Now solve it: How to solve a least-squares problemSolve ATA c^=ATyA^TA\,\hat{\mathbf{c}} = A^T\mathbf{y} for the coefficients.
Interpret the result. Write the fitted model with the coefficients in place and units on tt and yy. The slope is the change in yy per unit of tt. Predict by substituting a new tt, with care far outside the data range. The residuals yi−y^iy_i - \hat{y}_i should be small and show no pattern; the sum of their squares is the minimum error. This is regression as least squares.

Worked examples

Example 1 · weekly sales

line
sales in weeks 11 to 44: 2,3,5,62, 3, 5, 6 hundred units; predict week 66
model y=c0+c1ty = c_0 + c_1t
ATA=(4101030)A^TA = \begin{pmatrix} 4 & 10 \\ 10 & 30 \end{pmatrix}, ATy=(16,47)A^T\mathbf{y} = (16, 47)
∑t=10\sum t = 10, ∑t2=30\sum t^2 = 30, ∑ty=47\sum ty = 47
c^0=0.5\hat{c}_0 = 0.5, c^1=1.4\hat{c}_1 = 1.4
determinant 120−100=20120 - 100 = 20
y=0.5+1.4ty = 0.5 + 1.4t; week 66: about 890890 units
0.5+1.4⋅6=8.90.5 + 1.4 \cdot 6 = 8.9 hundred

Example 2 · spring stretch

through the origin
forces 1,2,31, 2, 3 N stretch a spring 2,6,72, 6, 7 cm; model x=kFx = kF
no intercept in this model
A=(1,2,3)TA = (1, 2, 3)^T: ATA=14A^TA = 14, ATx=2+12+21=35A^T\mathbf{x} = 2 + 12 + 21 = 35
one column, no column of 11s
k^=3514=2.5\hat{k} = \frac{35}{14} = 2.5 cm per N
residuals −0.5,1,−0.5-0.5, 1, -0.5

Example 3 · height of a thrown ball

parabola
heights 1,4,4,01, 4, 4, 0 m at t=0,1,2,3t = 0, 1, 2, 3 s; model h=c0+c1t+c2t2h = c_0 + c_1t + c_2t^2
three coefficients, four points
ATA=(461461436143698)A^TA = \begin{pmatrix} 4 & 6 & 14 \\ 6 & 14 & 36 \\ 14 & 36 & 98 \end{pmatrix}, ATh=(9,12,20)A^T\mathbf{h} = (9, 12, 20)
sums of tkt^k and tkht^kh
c^=(0.95,4.95,−1.75)\hat{\mathbf{c}} = (0.95, 4.95, -1.75)
solve the 3×33 \times 3 system
h=0.95+4.95t−1.75t2h = 0.95 + 4.95t - 1.75t^2, squared error 0.050.05
residuals 0.05,−0.15,0.15,−0.050.05, -0.15, 0.15, -0.05

See it happen

Picture each data point tied to the line by a vertical spring whose stretch is its residual. Least squares finds the position where the total squared stretch is smallest; there the residuals add to zero and their products with tt add to zero, which is what the two normal equations say. Drag the points in the least squares visualizer.

Common mistakes

Measuring the wrong distances

✗ measuring residuals horizontally or perpendicular to the line
✓ least squares measures vertical errors yi−y^iy_i - \hat{y}_i

Forgetting the column of ones

✗ A=(1,2,3,4)TA = (1, 2, 3, 4)^T for the model y=c0+c1ty = c_0 + c_1t
✓ AA has a column of 11s for c0c_0 and a column of tt values for c1c_1

Calling a curved model nonlinear

✗ y=c0+c1t+c2t2y = c_0 + c_1t + c_2t^2 needs a nonlinear method
✓ it is linear in c0,c1,c2c_0, c_1, c_2; use one column per power of tt

Predicting far outside the data

✗ week 5050: 0.5+1.4⋅50=70.50.5 + 1.4 \cdot 50 = 70.5 hundred units, stated as fact
✓ trust a fit near the data; flag long-range predictions as rough