# Setting Fit parameters bounds relative to each other

Good day.

I have the equation

``````double fitrange_low = 75;
double fitrange_hgh = 560;
TF1 *fm3 = new TF1("fm3","(x<[0])*([2]-[3]/(x**2)) + (x>[0] && x<[1])*([2]-[3]/([0]**2)) + (x>[1] && x>[0])*([2]-[3]/([0]**2)+([4]*(x-[1])**2))",fitrange_low,fitrange_hgh);
``````

that I am fitting to a range of experimental data where the two parameters [0] and [1] are being used to the transition between different parts of the equation.

[0] should always be less than or equal to [1]. Is there a way to enforce that limit when fitting to a graph?

I have used set fm3->SetParLimits() and SetParameter(), but the high and low range can overlap between different data sets so that is not enough as I don’t want to tune 100+ fittings by hand.

I think you should use something like this

[code]Double_t myFunction(Double_t x, Double_t par)
{
Float_t xx =x[0];
Double_t f = (xx<par[0])
(par[2]-par[3]/(xx**2)) + (xx>par[0] && x<par[1])
(par[2]-par[3]/(par[0]**2)) + (xx>par[1] && xx>par[0])(par[2]-par[3]/(par[0]**2)+(par[4](xx-par[1])**2;
if(par[0]>par[1]) return -1000000; // if [0] is greater than [1] then, the function takes a crap value in order the fit do not converge for this set of parameters
return f;
}
void myfit()
{
TF1 * f = new TF1(“f”,myFunction,75,560,5); //5 parameters
// you can now use f to fit what you want.

}[/code]

That looks like it solves the problem. Thank you for your help.

To make it compile with te + option on the Root command line I had to change from **2 to square things to TMath::Power( , 2.0) as shown below.

``````Double_t myFunction(Double_t *x, Double_t *par)
{
Float_t xx =x[0];
Double_t f = (xx<par[0])*(par[2]-par[3]/(TMath::Power(xx , 2.0)))
+ (xx>par[0] && xx<par[1])*(par[2]-par[3]/(TMath::Power(par[0],2.0)))
+ (xx>par[1] && xx>par[0])*(par[2]-par[3]/(TMath::Power(par[0],2.0))+(par[4]*TMath::Power((xx-par[1]),2.0)));

if(par[0]>par[1]) return -1000000; // if [0] is greater than [1] then, the function takes a crap value in order the fit do not converge for this set of parameters
return f;
}``````