# Fit pol2 with constraints

Hi,

I was wondering if there’s a way to constrain the pol2 function to the (0,0) point.

Thanks!

Hi Salamong,
welcome to the ROOT forum!

I don’t think ROOT can do this out of the box (@StephanH or @moneta might correct me).
Depending on your actual usecase, one of the threads here might be of help.

Cheers,
Enrico

1 Like

Try:

{
TF1 *f = ((TF1*)(gROOT->GetFunction("pol2")));
f->FixParameter(0, 0.0); // 0 = "p0"
// ...
f->ReleaseParameter(0); // 0 = "p0"
}
2 Likes

Hey, thanks guys! Actually think I figured it out. As long as the points are on a TGraph (originally I had them on a TGraphErrors w/ errorbars but maybe the errorbars was throwing my code off). I ended up doing:

c1 = new TCanvas(“c1”,"",200,10,700,500);
c1->SetGrid();

int n = 6;
// x and y
float x[n] = {2.348E-07,0.000001935,2.52324E-06,3.13388E-06,3.76564E-06,8.5805E-06}
float y[n] = {1.30,10.37,13.95,17.74,22.162,59.54}

TGraph *gr1 = new TGraph(n,x,y);
gr1->SetMarkerColor(1);
gr1->SetMarkerStyle(6);
gr1->Draw(“AP”);

w/ the fit function :

TF1 *fit2 = new TF1(“fit2”,"[1]*x**2 + [0]*x",0,10e-6)
gr->Fit(“fit2”)

which has (0,0) built in as a constraint. What do y’all think?

1 Like

I don’t see a constraint. But if you want (0,0) to be a part of your dataset, just add it to the data like this:

n = 7;
float x[n] = {0, 2.348E-07,0.000001935,2.52324E-06,3.13388E-06,3.76564E-06,8.5805E-06}
float y[n] = {0, 1.30,10.37,13.95,17.74,22.162,59.54}

EDIT:
Oh, now I see what you mean. Technically, you didn’t add a constraint, at least some people would understand that differently.
But you certainly wrote down a function that will always go through (0,0)!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.