This code is used as plot tool in my second year physics lab period. It can import data from a comma delimitated csv file and generate linear best fit line with residuals and uncertainties.
I used method of least squares[1] in a linear function of x and y,
y=mx+c
where m is the gradient and c is the y-intercept.
with the uncertainty in the intercept and the gradient,
where
and is common uncertainty defined as
Ref:[1]I. G.Hughes and T. P.A.Hase, Measurements and their Uncertainties A practical guide to modern error analysis, 1st ed. Oxford: Oxford University Press, 2010, p. 58.
The demo data set was shown as file demo.csv with uncertainties on both x and y axis (No unit consideration).
X value | Uncertainty of X | Y value | Uncertainty of Y |
---|---|---|---|
1 | 0.05 | 2 | 0.1 |
2 | 0.05 | 4 | 0.1 |
3 | 0.05 | 6 | 0.1 |
4 | 0.05 | 8 | 0.1 |
5 | 0.05 | 10 | 0.1 |
6 | 0.1 | 12 | 0.5 |
7 | 0.1 | 14 | 0.5 |
8 | 0.5 | 16 | 0.5 |
9 | 0.2 | 18 | 1 |
10 | 0.5 | 20 | 1 |
The plot can be found as plot.png , which is also the default output figure name.