# Piece-wise polynomial fitting

**URL:** <https://root-forum.cern.ch/t/piece-wise-polynomial-fitting/40237>\
**Category:** Newbie\
**Created:** [July 3, 2020, 10:38pm UTC](https://root-forum.cern.ch/t/piece-wise-polynomial-fitting/40237 "2020-07-03T22:38:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abhinab_mukhopadhyay/32/20444_2.png) [@Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/u/Abhinab_Mukhopadhyay)\
**Post date:** [July 3, 2020, 10:38pm UTC](https://root-forum.cern.ch/t/piece-wise-polynomial-fitting/40237/1 "2020-07-03T22:38:54Z")

</div>

Hi,

So I am having some issues regarding fitting a section of data sets. It seems it doesn’t fit the last few data sets. I have tried using code similar to

```auto
TF1 *f4= new TF1("f4", "(pol3)*(x<500)+(pol3)*(x>=500 && x<=600)+(pol4)*(x>600 && x<=1510)+(pol3)*(x>1510 && x<=2000)",-1,2000); 

```

However, it doesn’t respect ranges and the fits don’t come out well. I have attached the picture and code below for reference. I have tried various combinations of pol as well as range however the best one is shown in the image.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/1/f/1f643c0eea102262fe6133d83eeeda1823f0e7ed.png)

```auto
double numbers3[14]={ 20,	40,	100,	200,	375,	500,	750,	825,	1000,	1250,	1500,	2000,	2500,	3000};
double numbers5[14]={ 0,	0,	5.00E-05,	0.0002,	0.0004,	0.0004,	0.0002,	0.00045,	0.00020001,	0.0002,	0.0002,	0,	0,	0 };
double numbers6[14]={0,	0,	5.00E-05,	1.00E-04,	0.000141393,	0.000141393,	1.00E-04,	0.000149966,	1.00E-04,	1.00E-04,	1.00E-04,	0,	0,	0};

   TGraphErrors *g = new TGraphErrors();
   for(int i=0;i<14;i++){            
   g->SetPoint(i, numbers3[i],numbers5[i]);
   g->SetPointError(i,0,numbers6[i]);
   }

  g->Draw("AP");
  g->SetMarkerStyle(20);
  g->SetFillColor(6);
  g->SetFillStyle(3005);
  g->GetXaxis()->SetLimits(0,3100);
  g->GetYaxis()->SetLimits(-10,0.07195);
  
  
TF1 *f1= new TF1("f5", "(pol4)*(x<=750)",-1,750);
TF1 *f2= new TF1("f4", "(pol3)*(x>=749 && x<=1001)",749,1001);
TF1 *f3= new TF1("f6", "(pol3)*(x>=999 && x<=1501)",999,1501);
TF1 *f4= new TF1("f7", "(pol1)*(x>=1499 && x<=2001)",1499,2001);

g->Fit(f1,"R+");
g->Fit(f2,"R+"); 
g->Fit(f3,"R+");
g->Fit(f4,"R+"); 

```

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [July 4, 2020, 9:31pm UTC](https://root-forum.cern.ch/t/piece-wise-polynomial-fitting/40237/2 "2020-07-04T21:31:03Z")

</div>

Perhaps @moneta or @StephanH can help?

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [July 6, 2020, 8:25am UTC](https://root-forum.cern.ch/t/piece-wise-polynomial-fitting/40237/3 "2020-07-06T08:25:26Z")

</div>

This here:

> [@Abhinab\_Mukhopadhyay](#):
>
> `(pol3)*(x<500)+(pol3)*(x>=500 && x<=600)`

Cannot work because the `pol3` for the left part and the `pol3` for the middle part have the same parameters. You need to write down the polynomials yourself, and give them different parameters:

```auto
(x[0]*1 + x[1]*x + x[2]*x*x) * (cut) + (x[3]*1 + ...) * (cut)

```

Note how I numbered the coefficients such that the polynomials are not the same.

---

<div class="post-metadata">

**Author:** ![Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abhinab_mukhopadhyay/32/20444_2.png) [@Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/u/Abhinab_Mukhopadhyay)\
**Post date:** [July 6, 2020, 4:21pm UTC](https://root-forum.cern.ch/t/piece-wise-polynomial-fitting/40237/4 "2020-07-06T16:21:08Z")

</div>

Hi,

I tried your suggestion, here is the code snippet:  
`TF1 *f4= new TF1("f4", "([0]*x*x*x+[1]*x*x+[2]*x+[3])*(x<=750)+([4]*x*x*x+[5]*x*x+[6]*x+[7])*(x>=750 && x<=1000)+([8]*x*x*x+[9]*x*x+[10]*x+[11])*(x>=1000 && x<=1500)",-1,1500);`

The graph created, however, isn’t very smooth, here is the image

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/2/8/28b8713778dc5faabccf37dc276427aa3c295dc5.png)

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [July 7, 2020, 12:37pm UTC](https://root-forum.cern.ch/t/piece-wise-polynomial-fitting/40237/5 "2020-07-07T12:37:51Z")

</div>

Hi,

Try to incrust the number of function points used to draw, by calling for example  
`f4-\>SetNpx(10000)

Lorenzo

---

<div class="post-metadata">

**Author:** ![Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abhinab_mukhopadhyay/32/20444_2.png) [@Abhinab\_Mukhopadhyay](https://root-forum.cern.ch/u/Abhinab_Mukhopadhyay)\
**Post date:** [July 7, 2020, 5:12pm UTC](https://root-forum.cern.ch/t/piece-wise-polynomial-fitting/40237/6 "2020-07-07T17:12:16Z")

</div>

> [@moneta](#):
>
> SetNpx(10000)

Hi, I tried the suggestion however the graph changed very little.
