Write a python macro ExerciseGraph.py
.
(1.0, 2.1), (2.0, 2.9), (3.0, 4.05), (4.0, 5.2), (5.0, 5.95)
x
to 0.0 and the errors on y
to 0.1 (all at once).f(x) = ax + b
and fit it to the graph.Bonus questions:
a
and b
and their estimated uncertainties.import ROOT
%jsroot on
Welcome to JupyROOT 6.28/00
from ROOT import TCanvas, TF1, TGraphErrors
canvas = TCanvas("canvas", "", 800, 600)
graph = TGraphErrors(5)
graph.SetPoint(0, 1.0, 2.1)
graph.SetPoint(1, 2.0, 2.9)
graph.SetPoint(2, 3.0, 4.05)
graph.SetPoint(3, 4.0, 5.2)
graph.SetPoint(4, 5.0, 5.95)
for i in range(5):
graph.SetPointError(i, 0, 0.1)
funct = TF1("funct", "[0] * x + [1]", 0, 6)
graph.Fit("funct")
graph.Draw()
funct.Draw("same")
canvas.Draw()
print(f"a = {funct.GetParameter(0):.6f} +/- {funct.GetParError(0):.6f}")
print(f"b = {funct.GetParameter(1):.6f} +/- {funct.GetParError(1):.6f}")
a = 1.000000 +/- 0.031623 b = 1.040000 +/- 0.104881 FCN=5.7 FROM MIGRAD STATUS=CONVERGED 33 CALLS 34 TOTAL EDM=2.71359e-21 STRATEGY= 1 ERROR MATRIX ACCURATE EXT PARAMETER STEP FIRST NO. NAME VALUE ERROR SIZE DERIVATIVE 1 p0 1.00000e+00 3.16228e-02 1.70422e-05 -2.08465e-09 2 p1 1.04000e+00 1.04881e-01 5.65226e-05 -1.21781e-09
Last update: 06 March 2023