Line of Best Fit with Error Bars
Plot x and y error bars and get the slope and intercept with their uncertainties.
Runs in your browser. Nothing is uploaded.
Your data
| # | L (m) | ΔL (m) | T (s)plotted as T² | ΔT (s) |
|---|---|---|---|---|
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 | ||||
| 6 | ||||
| 7 | ||||
| 8 |
Type your measurements, or paste columns from a spreadsheet into the first cell. Two columns are read as x, y; three as x, y, Δy; four as x, Δx, y, Δy. Up to 500 rows. Tab moves along a row; Enter starts the next one.
Graph and results
Slope (gradient)
4.0 ± 0.3 s²/m
± 6.6%
Intercept
−0.02 ± 0.15 s²
Max/min lines through every error box: steepest 4.326, shallowest 3.793. The uncertainty is half their difference. The best line comes from least squares.
Max/min rule: half the difference, lines through every box
With these lines, half the difference gives ± 0.27 s²/m (6.6%) and the largest difference from the best line gives ± 0.30 s²/m (7.4%). Lines through the first and last boxes would give ± 0.43 s²/m (11%).
Courses differ. Many take half the difference. AQA compares the best line with the worst one. Many IB classes and Vernier’s guide draw the lines through the first and last error bars.
Your graph appears here. Enter at least 2 points, or try an example.
- Measurements with ±ΔL and ±Δ(T²) error bars
- Best line (least squares)
- Steepest and shallowest lines through every error box
Point at a dot to read its values.
Your data
Measured values are shown as you typed them. Calculated values are rounded to their uncertainty. The CSV file keeps every digit.
Columns and uncertainties
After a curved transform the true error bars are slightly uneven. The tool uses the symmetric first-order value, for example Δ(T²) = 2T·ΔT.
All four methods
| Method | Slope (s²/m) | Intercept (s²) |
|---|---|---|
| Least squares | 4.03 ± 0.15 (3.7%) | −0.02 ± 0.10 |
| R² = 0.9944; uncertainty from the scatter (4 degrees of freedom). | ||
| Weighted least squares | 4.06 ± 0.18 (4.5%) | −0.05 ± 0.10 |
| Weights 1/Δy². χ² = 2.40 for 4 degrees of freedom; reduced χ² = 0.60: your error bars match the scatter. | ||
| Max/min lines On the graph | 4.0 ± 0.3 (6.6%) | −0.02 ± 0.15 |
| Steepest 4.326, shallowest 3.793 s²/m. Half the difference: 0.27; largest difference from the best line: 0.30. | ||
| York (errors in x and y) | 4.06 ± 0.20 (4.9%) | −0.05 ± 0.11 |
| Uses the x and y error bars. χ² = 2.12 for 4 degrees of freedom; reduced χ² = 0.53: your error bars match the scatter. | ||
Max/min gives a larger uncertainty (± 0.3 s²/m) than least squares (± 0.15 s²/m). It uses the corners of the error boxes, not the scatter of the points. The weighted fit’s reduced χ² is 0.60: your error bars match the scatter.
Use the slope
Carry the slope’s uncertainty into a result, such as g from a pendulum. Use m for the slope and c for the intercept.
Write π as pi or π, powers as ^ (4 pi^2/m), and ln, log, sqrt, sin, cos and tan as functions.
g from the max/min slope
9.8 ± 0.6 m/s²
That is ± 6.6%. Read as: g = 4·π²/m. ∂g/∂m = −4·π²/m² = −2.436. Δg = |∂g/∂m| × Δm = 2.436 × 0.266 = 0.649.
- Least squares 9.8 ± 0.4 m/s²
- Weighted least squares 9.7 ± 0.4 m/s²
- York (errors in x and y) 9.7 ± 0.5 m/s²
Show the working
Here x is L and y is T², as plotted. Numbers are shown to 8 significant figures; the tool keeps full precision.
Repeat readings: means and uncertainties
Least squares: sums and formulas
Weighted least squares and χ²
Max/min lines: the corners they pass through
York’s method
How to use
- Type your measurements into the table, or paste columns from Sheets, Excel or Numbers into the first cell. You can also open a CSV file.
- Name each column and give its unit. Set its uncertainty: one value for every point, a column of its own, or a percentage.
- If you repeated each measurement, set Readings per point. The tool plots the mean of the readings, with an error bar from their spread.
- If theory predicts a curve, choose Plot as to straighten (linearize) it: T² for a pendulum, or ln y for decay.
- Pick the line your course asks for: least squares, weighted, max/min lines or York. For max/min lines, open Max/min rule to match your course.
- Read the slope and intercept with their uncertainties. Type a formula such as g = 4π²/m to carry the slope into your answer. Then download the graph, or copy the link to share the same data and method.
Your last table is kept in this browser, so you can pick it up on your next visit. Clear removes it. A CSV file you download here opens again with the same table and settings. A link holds up to 200 points, so share a longer table as a CSV file.
Worked example
A pendulum’s period T is timed for six lengths L. Theory says T² = (4π²/g)·L. So a graph of T² against L is a straight line through the origin with a slope of 4π²/g. Each length is measured to ± 0.01 m. Each period comes from 5 swings timed by hand to ± 0.2 s, so ΔT = ± 0.04 s.
| L (m) | T (s) | T² (s²) | Δ(T²) (s²) |
|---|---|---|---|
| 0.25 | 0.96 | 0.9216 | 0.0768 |
| 0.40 | 1.29 | 1.6641 | 0.1032 |
| 0.55 | 1.50 | 2.2500 | 0.1200 |
| 0.70 | 1.66 | 2.7556 | 0.1328 |
| 0.85 | 1.81 | 3.2761 | 0.1448 |
| 1.00 | 2.02 | 4.0804 | 0.1616 |
Settings: ΔL = 0.01 m and ΔT = 0.04 s for every point, with y plotted as T². Try an example loads exactly this. Results, with slopes in s²/m and intercepts in s²:
- Least squares: slope 4.026 ± 0.151 and intercept −0.025 ± 0.102, shown rounded as 4.03 ± 0.15 and −0.02 ± 0.10. The intercept is within its uncertainty of zero, as theory expects.
- Weighted least squares: slope 4.063 ± 0.184, with χ² = 2.40 for 4 degrees of freedom. Reduced χ² is 0.60, so the error bars match the scatter.
- Max/min lines: the steepest line through every error box has a slope of 4.3256 and the shallowest 3.7927. Half the difference is Δm = (4.3256 − 3.7927)/2 = 0.266, so the slope is 4.0 ± 0.3, or ± 6.6%.
- The other max/min rule compares the best line with the worst one: 4.3256 − 4.0258 = 0.300, or ± 7.4%. Open Max/min rule to use it.
- York, which also counts the ± 0.01 m in L: slope 4.057 ± 0.197.
From the slope, g = 4π²/m = 4π²/4.026 = 9.81 m/s². With the max/min uncertainty, Δg = g·Δm/m = 9.81 × 0.266/4.026 = 0.65 m/s². Rounded, g = 9.8 ± 0.6 m/s², which is ± 6.6%. Least squares alone gives ± 0.4 m/s².
How it works
The tool fits a straight line y = mx + c to the values you plot, four ways. Pick the one your course asks for; the others are there to compare. Every formula uses the plotted values, so with T² on the y axis, y means T².
Least squares (LINEST)
Least squares picks the line with the smallest sum of squared vertical distances to the points. It ignores your error bars and estimates the uncertainty from how far the points scatter around the line. These are the numbers Excel and Google Sheets return from LINEST.
D = nΣx² − (Σx)² m = (nΣxy − ΣxΣy) / D c = (Σy − mΣx) / n s = √(Σr² / (n − 2)), where r = y − (mx + c) σm = s·√(n / D) σc = s·√(Σx² / D)
Weighted least squares (weighted linear regression) and χ²
Each point gets the weight w = 1/Δy², so points with small error bars count more. The uncertainties come from the error bars alone, and χ² tells you whether the scatter fits them.
S = Σw Sx = Σwx Sy = Σwy Sxx = Σwx² Sxy = Σwxy Δ = S·Sxx − Sx² m = (S·Sxy − Sx·Sy) / Δ c = (Sxx·Sy − Sx·Sxy) / Δ σm = √(S / Δ) σc = √(Sxx / Δ) χ² = Σw(y − mx − c)² reduced χ² = χ² / (n − 2)
A reduced χ² near 1 means your error bars match the scatter. Well below 1 means they look generous, which is common when they come from the resolution of a scale. Well above 1 means the points scatter more than the error bars allow, or a straight line is the wrong model.
The tool calls a value well below or well above 1 when chance alone would give it less than 5% of the time. With only a few points that test rarely decides. When it leaves a reduced χ² below 0.5 or above 2 undecided, the tool says how often chance alone gives such a value.
Max/min lines (steepest and shallowest, also called worst lines)
Draw a box around each point, Δx wide on each side and Δy high on each side. The steepest straight line that still passes through every box, and the shallowest one, give mmax and mmin. For a falling line both slopes are negative, and the steepest line has the smaller one. Courses turn them into an uncertainty in one of two ways:
half the difference Δm = (m_max − m_min) / 2 Δc = |c(steepest) − c(shallowest)| / 2 best line and worst line Δm = largest |m_best − m_worst| Δc = largest |c_best − c_worst| percentage uncertainty = Δm / m × 100%
The tool finds the two lines exactly. Each one touches the corners of two boxes, listed in Show the working. The slope it reports is the least-squares slope, which normally lies between the two. Both uncertainties are always shown, and Max/min rule sets which one goes into the result.
Some courses draw the two lines through the first and last error boxes only. Max/min rule has that choice too. It ignores the points in between, so it usually gives a larger uncertainty.
By hand, draw the best line first. Then lay a ruler through the bottom of the first box and the top of the last one. Turn it until it is as steep as it can be while still touching every box. Do the same for the shallowest line.
If no straight line passes through every box, the scatter is bigger than the error bars. The tool says so and switches to lines through opposite corners of the first and last boxes.
York’s method (errors in x and y)
When both x and y carry uncertainties, York’s method finds the best line using both. Each point gets the weight W = 1/(Δy² + m²Δx²). That weight depends on the slope, so the tool repeats the calculation until the slope stops changing. The standard errors come from your error bars. With no x error bars it gives the same line as the weighted fit.
The method follows York, Evensen, López Martínez and De Basabe Delgado (2004). Their paper is “Unified equations for the slope, intercept, and standard errors of the best straight line”, American Journal of Physics 72, 367, doi:10.1119/1.1632486.
Lines through the origin
m = Σxy / Σx² s = √(Σr² / (n − 1)) σm = s / √Σx² weighted: m = Σwxy / Σwx² σm = 1 / √Σwx²
Force the line through the origin only when two things hold. Theory says it must pass through (0, 0), and the fitted intercept is within its uncertainty of zero. A clear intercept points to a systematic error, such as a zero offset on a meter or a late start on a stopwatch. Forcing the line then hides the error and pulls the slope away from its true value.
With the line forced through the origin, the max/min lines start at the origin too. The first-and-last choice then draws them from the origin through the corners of the last box. York’s method always has an intercept, so it is left out.
Curves made straight, and their error bars
Many laws become straight lines after a transform. Plot T² against L for a pendulum, ln N against t for radioactive decay, or V against 1/p for a gas at constant temperature. This is also called linearizing the data. The error bars are carried through with first-order rules:
| Plot as | Error bar |
|---|---|
| x² | Δ(x²) = 2|x|·Δx |
| 1/x | Δ(1/x) = Δx / x² |
| √x | Δ(√x) = Δx / (2√x) |
| ln x | Δ(ln x) = Δx / x |
| log₁₀ x | Δ(log₁₀ x) = Δx / (x·ln 10) |
After a curved transform the true bars are slightly uneven. For T = 1.50 ± 0.04 s, T² runs from 2.1316 to 2.3716 s², which is 0.1184 below and 0.1216 above 2.25. The tool uses the symmetric value 2T·ΔT = 0.12 s². Rows a transform can’t use, such as ln of 0 or of a negative number, are left out and listed under the table.
Choosing error bars, and repeat readings
A common choice is the larger of two things: the resolution of the instrument, and half the range of repeated readings. Set Readings per point to enter the repeats, and the tool does this for you. For bounce heights of 21, 23 and 24 cm the mean is 22.7 cm and half the range is (24 − 21)/2 = 1.5 cm. With a ruler read to ± 1 cm, the error bar is ± 1.5 cm. You can use the standard deviation or the standard error of the mean instead.
Timing several swings and dividing shrinks the uncertainty of one swing. In the example, 5 swings timed to ± 0.2 s give ± 0.04 s per swing; 20 swings would give ± 0.01 s. For a percentage uncertainty, choose Percent, or type a value such as 5%. Your course may have its own rule, so check its guidance.
Decimal points and decimal commas
Type 0.25 or 0,25: both are read as a quarter, in typed and in pasted data. A value such as 1,095 could mean 1.095 or 1095. The tool reads it the way the other values in its column show, and says under the table how it read it. An uncertainty typed once for a column is read the same way as that column. You can set the decimal mark there yourself.
Rounding
Uncertainties are rounded to one significant figure, or two when the first digit is 1. Each value is then rounded to the same decimal place. So 4.0258 ± 0.2664 becomes 4.0 ± 0.3, and 4.0258 ± 0.1508 becomes 4.03 ± 0.15. Percentage uncertainties are shown to two significant figures. Turn on Show full precision for unrounded numbers. The CSV file always keeps them.
Course guidance
Courses differ on which method they expect and how they round, so check your own course’s official guidance:
- IB Diploma Programme physics, International Baccalaureate
- AQA AS and A-level Physics required practical handbook (PDF)
- OCR AS and A Level Physics practical skills handbook (PDF)
- AP Physics 1 and 2 inquiry-based investigations (teacher’s manual), College Board
How the numbers are checked
The fits are tested against the NIST Statistical Reference Datasets for linear regression: Norris, and NoInt1 and NoInt2 for lines through the origin. York’s method is tested against the Pearson–York data used by York et al. (2004) and against SciPy’s orthogonal distance regression. The max/min lines are tested against a search over every pair of box corners.
FAQ
How do I find the uncertainty in the gradient of a graph?
Draw the steepest and shallowest straight lines that still pass through every error bar. These are the max and min lines, or worst lines. Many courses take half the difference between their gradients: Δm = (mmax − mmin)/2. Others take the difference between the best line and the worst line. Divide by the gradient and multiply by 100 for the percentage uncertainty. This tool shows both rules, and a least-squares estimate from the scatter. For the pendulum example they are ± 0.27, ± 0.30 and ± 0.15 s²/m.
What’s the difference between the max/min method and least squares?
Max/min looks only at the error boxes and asks which straight lines are still possible. Least squares ignores the error bars and uses how far the points sit from the line. When your error bars are generous, max/min gives the bigger uncertainty. When the points scatter more than the bars allow, often no line passes through every box. Least squares is then the better guide.
How do I add horizontal error bars when Google Sheets can’t?
Enter or paste your data here and set an x uncertainty: one value for every point, a column of its own, or a percentage. The graph draws horizontal and vertical error bars. Download it as a PNG at 300 dpi, 16 cm or 8 cm wide, and insert it into Google Docs or Slides. The SVG stays sharp at any size in Word, PowerPoint and Inkscape.
Should I force my line through the origin?
Only when theory says the line must pass through (0, 0) and your fitted intercept is within its uncertainty of zero. A clear intercept points to a systematic error, such as a zero offset. Forcing the line through the origin hides it and changes the slope.
What does a reduced chi-squared of 0.2 or 5 mean?
Reduced χ² compares the scatter of your points with your error bars. Around 1, they agree. At 0.2 the points sit much closer to the line than the error bars suggest, so the bars look generous. At 5 the points scatter more than the bars allow: the bars are too small, or a straight line is the wrong model. With only a few points the value varies a lot, and a low or high one can be chance. The tool then says how often chance alone gives such a value.
My max and min lines can’t pass through every error bar. What now?
That means the scatter is bigger than your error bars. First check for a misread or mistyped point; the Residuals tab shows which point is out of line. Then use the least-squares uncertainty, or the lines through the first and last boxes that the tool switches to. Say in your report which one you used.
How many significant figures should the gradient have?
Round the uncertainty to one significant figure, or two if it starts with 1. Then round the gradient to the same decimal place: 4.0258 ± 0.2664 becomes 4.0 ± 0.3. If your course asks for something else, turn on Show full precision and round the unrounded numbers yourself.
Can I use this for a curve, like T² against L or ln y against x?
Yes, when the relationship becomes a straight line after a transform. Choose Plot as x², 1/x, √x, ln x or log₁₀ x for either column. The error bars are carried through with first-order rules, such as Δ(T²) = 2T·ΔT. The transformed values appear in the Table tab and the CSV file.
Step by step: pendulum g and uncertainty, custom error bars in Google Sheets, or LINEST slope uncertainty. Also see whether your result agrees with the accepted value.
Updated