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.
a = (1.940 ± 0.091), b = (0.15 ± 0.25), R² = 0.99566
Slope: a, intercept: b, y = a·x + b
% 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
- Mean value
- Standard deviation
- Type A uncertainty
- Combined uncertainty
- Uncertainty propagation
- Expanded uncertainty
How it works
- The line is fitted by least squares, and the uncertainties u(a) and u(b) come from the scatter of the points around the line, with n - 2 degrees of freedom.
- The column separator is detected for you: a tab from Excel, a semicolon or a space, and numbers may use a decimal point or a comma.
- The data is checked before any result is shown: fewer than 3 points, an empty y column or constant x give a clear warning instead of zeros.
- The LaTeX code is a pgfplots figure with the points, error bars and the line, and the coefficients in the caption are written with siunitx.
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
- CSV and Excel to LaTeX table
- LaTeX matrix generator
- Propagation of uncertainty calculator
- Type A, type B and expanded uncertainty calculator
- Handwriting to LaTeX
Got formulas on paper?
Draw them or snap a photo and CamteX AI turns them into LaTeX and inserts them into your document.