22 void makePoints(Int_t n, Double_t *x, Double_t *y, Double_t *e, Int_t p);
27 Double_t *x =
new Double_t[n];
28 Double_t *y =
new Double_t[n];
29 Double_t *e =
new Double_t[n];
30 TCanvas *myc =
new TCanvas(
"myc",
31 "Fitting 3 TGraphErrors with linear functions");
35 makePoints(n, x, y, e, 3);
36 TGraphErrors *gre3 =
new TGraphErrors(n, x, y, 0, e);
41 TF1 *f3 = gre3->GetFunction(
"pol3");
45 makePoints(n, x, y, e, 2);
46 TGraphErrors *gre2=
new TGraphErrors(n, x, y, 0, e);
48 gre2->SetMarkerColor(kBlue);
49 gre2->SetLineColor(kBlue);
57 TF1 *f2 =
new TF1(
"f2",
"sin(x) ++ sin(2*x)", -2, 2);
59 f2 = gre2->GetFunction(
"f2");
60 f2->SetLineColor(kBlue);
64 makePoints(n, x, y, e, 4);
65 TGraphErrors *gre4=
new TGraphErrors(n, x, y, 0, e);
67 gre4->SetMarkerColor(kRed);
68 gre4->SetLineColor(kRed);
71 gre4->Fit(
"1 ++ exp(-x)");
73 TF1 *f4 = gre4->GetFunction(
"1 ++ exp(-x)");
75 f4->SetLineColor(kRed);
78 TLegend *leg =
new TLegend(0.3, 0.7, 0.65, 0.9);
79 leg->AddEntry(gre3,
" -7 + 2*x*x + x*x*x",
"p");
80 leg->AddEntry(gre2,
"sin(x) + sin(2*x)",
"p");
81 leg->AddEntry(gre4,
"-2 + exp(-x)",
"p");
86 void makePoints(Int_t n, Double_t *x, Double_t *y, Double_t *e, Int_t p)
93 x[i] = r.Uniform(-2, 2);
94 y[i]=TMath::Sin(x[i]) + TMath::Sin(2*x[i]) + r.Gaus()*0.1;
100 x[i] = r.Uniform(-2, 2);
101 y[i] = -7 + 2*x[i]*x[i] + x[i]*x[i]*x[i]+ r.Gaus()*0.1;
106 for (i=0; i<n; i++) {
107 x[i] = r.Uniform(-2, 2);
108 y[i]=-2 + TMath::Exp(-x[i]) + r.Gaus()*0.1;