# How to fit data set using Graph

Hello
I have a data set, which is discrete ,not continuous. I want to fit it using a graph which is also discrete not by a continuous function. How to do it?
say, I have the data set
X = {1,2,3,4,5,6,7,8} and corresponding
Y = {35,57,25,22,55,47,89,44}

I have a user defined fitting function
y= ax+bx+c
where a,b are parameters and x is the variable.
I want that this user defined function to take only those values of “x” which is present in data set and no other.
How to do this?

1 Like

Try:

```{ Double_t x[] = {1,2,3,4,5,6,7,8}; Double_t y[] = {35,57,25,22,55,47,89,44}; TGraph *g = new TGraph((sizeof(x) / sizeof(Double_t)), x, y); TF1 *f = new TF1("f", "[2] * x * x + [1] * x + [0]"); g->Fit(f); g->Draw("AL"); }```

Note that your function is a simple polynomial (having a degree of 2) so you can simply try:

```{ Double_t x[] = {1,2,3,4,5,6,7,8}; Double_t y[] = {35,57,25,22,55,47,89,44}; TGraph *g = new TGraph((sizeof(x) / sizeof(Double_t)), x, y); g->Fit("pol2"); g->Draw("AL"); }```

See, for example, http://root.cern.ch/root/html/TGraph.html#TGraph:Fit and “Fitting tutorials” http://root.cern.ch/root/html/tutorials/fit/index.html and “How to Fit Histograms or Data Points ?” http://root.cern.ch/drupal/content/how-fit-histograms-or-data-points