All tools

Linear regression with uncertainty calculator

Paste two columns of numbers from Excel or type the points by hand. The calculator fits a line by least squares, works out the uncertainties of the slope and the intercept and R², draws the chart and prepares pgfplots code for your lab report. A third column with the uncertainty of y adds error bars to the chart.

Result
a=(1.940±0.091),b=(0.15±0.25),R2=0.99566a = (1.940 \pm 0.091), \quad b = (0.15 \pm 0.25), \quad R^2 = 0.99566

a = (1.940 ± 0.091), b = (0.15 ± 0.25), R² = 0.99566

Slope: a, intercept: b, y = a·x + b

Chart
Chart
24681234
a = 1.94 ± 0.0906 · b = 0.15 ± 0.248 · R² = 0.9957
LaTeX code
% camtex-chart: {"x":[1,2,3,4],"y":[2.1,3.9,6.2,7.8],"xlabel":"x","ylabel":"y","lang":"en"}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{axis}[
    xlabel={x}, ylabel={y},
    grid=both, grid style={gray!25}, legend pos=north west, width=0.8\linewidth,
]
\addplot[only marks, mark=*, mark size=1.4pt]
    coordinates {(1, 2.1) (2, 3.9) (3, 6.2) (4, 7.8)};
\addlegendentry{Measurements}
\addplot[red, thick, domain=0.85:4.15, samples=2]
    {1.94*x + 0.15};
\addlegendentry{Least-squares fit}
\end{axis}
\end{tikzpicture}
\caption{$a = \num{1.940 \pm 0.091}$, $b = \num{0.15 \pm 0.25}$, $R^2 = \num{0.9957}$}
\end{figure}

Turn it into a full lab report

In the CamteX editor the same calculations go straight into your document: the table of measurements, uncertainties, a chart with a fitted line and conclusions. Open a finished pendulum example, with no account and no scans used.

Uncertainty formulas in LaTeX

How it works

Packages you need

\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{siunitx}  % \num{}

Package documentation on CTAN: pgfplots, siunitx

More examples and common mistakes: SI units in LaTeX: siunitx \SI, \num and ± values

Frequently asked questions

How do you find the uncertainty of the slope in linear regression?
From the scatter of the points around the fitted line: u(a) = sqrt(n s² / (n Σx² - (Σx)²)), where s² is the sum of squared residuals divided by n - 2. The calculator gives u(a) and u(b) as soon as you paste the data.
How many points do I need for a linear fit?
Two points define a line, but the uncertainties of the coefficients need at least 3. In a lab report aim for 5 or more, because the uncertainties shrink quickly with the number of points.
What does R² mean?
The coefficient of determination tells you what share of the variation in y the line explains. A value close to 1 means the points lie close to the line. R² alone does not replace the uncertainties of a and b.
How do I put the chart into LaTeX?
Copy the code and add the pgfplots and siunitx packages to your preamble. The figure compiles in Overleaf and in pdfLaTeX with no extra files.

Other tools

Got formulas on paper?

Draw them or snap a photo and CamteX AI turns them into LaTeX and inserts them into your document.

Try it free