How to simultaneous fit a two variable function

I want to do a simultaneous fit of a two variable function that looks like:

f(r,t)=C®e^(a-b/r+cr)*t

I want to fit it in terms of the parameters C® and a,b,c. The problem is that I don’t now how C® looks like and this is why I want to fit it as a parameter (if r takes N distinct values then I will determine N parameters C). So at the end of the fit I want to have the N C® parameters and a,b,c.

(I was able to fit it separately, first as a function of t: f(t)=Ce^(mt), and after this I fitted m=a-b/r+c*r. However I really want to do also a simultaneous fit).

I don’t now how to call Minuit to do this. I tried to use C as a vector parameter, so my chi square function and function I wanted to fit looked like:

void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t par, Int_t iflag)
{
const Int_t nbins = 9,max=34;
Int_t i,j;
Double_t chisq = 0;
Double_t delta;
for (j=0;j<max;j ++)
{
for (i=0;i<nbins; i++)
{
delta = (z[i][j]-func(x[i],y[j],par))/error[i][j];
chisq += delta
delta;
}
}
f = chisq;
}

Double_t func(Double32_t x, Double32_t y, Double32_t par)
{
Double_t arg = 0;
arg = (-x
(par[0]-par[1]/y+par[2]*y)));
Double_t value = par[3][j]*TMath::Exp(arg);
return value;
}
where par[3] is a vector in j with j from 0 to max. However this didn’t work.

Could you help me with any ideas?

Hello,

in principle what you are doing should work fine. Which kind of error are you getting ? Could it be that your initial values are wrong or the function does not model correctly the data.
Maybe send me the minimal running program reproducing this problem with the data

Best Regards

Lorenzo

Hi,

I attached the source code and two files with the input data. I modified a bit the initial functions from the previous post. The program can be compiled but I obtain bad fits: p0=1.28e-01, p1=4.24e-02 p2=3.92e-02 instead of
p0~0.9, p1~0.5, p2~0.12 which I obtained from doing a fit in t followed by one in r.

Could you tell me if you see any error in the code that could produce these bad fits?

Thank you very much for your help,
Ion
minuitfin.C (1.95 KB)
fit300.txt (10.5 KB)
out300.txt (41.2 KB)

Also,
for the correct fit the time should be between 3 or 4 and 9.
This would mean i=3 and nbins=9;
and the distance sqrt(3) and 6
so j=3 or 4 and max~34.

Hi,

looking at your code code, I have not fully understood what you are trying to do.
From the data sets, it seems to me you have measurements data (factors)

x[i] y[j] (i=0,9, j=0,114)

and response data (values)

z[i][j] with errors err[i][j]

So basically you are fitting 9 x 114 data points.
Now, when you are building the least square function, you are using only the first 34 values of j:

void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag) 
{ 
const Int_t nbins = 9,max=34; 
Int_t i,j; 
Double_t chisq = 0; 
Double_t delta; 
for (j=0;j<max;j ++) 
{ 
for (i=0;i<nbins; i++) 
{ 
delta = (z[i][j]-func(x[i],y[j],par))/error[i][j]; 
chisq += delta*delta; 
} 
} 
f = chisq; 
} 

Why this ? You want to have different values C(j) for each y[j] ?
Then max should be 114 in this case, why 34 ? You want to use only the first 34 y values ?
If this is the case, your 8 i-bins do not agree at all with your model function for every j. The fit returns a very high value of chi2 ( ~ 11000) and your ndf is 34 * 9 - 37 = 269

Doing this you are also loosing the statistical power of the data.
In general, I would eventually try to parameterize (if possible) the coefficient C(j) with a function of fewer parameters.

Lorenzo

P.S. You have also few C++ syntax errors in your code. I would first compile it before, for example using ACLIC, by doing:

root> .L minuitfin.C+ 

Hi,

I do not want to use all the data points. I want to fit only in the intervals t [3,9] and r [1.8,6] or something close to this. This is why I want to take i=3,nbins=9 and j=4 and max=34.
Also, I can’t replace C® with a function because I do not know who it should look like.

My problem is that when I do the fittings separate (first in t and then in r), in for the same interval as above, I obtain totally different results compared with the simultaneous fit.

Thanks,
Ion

Are the separate fits having good chi2 values ? If the fits are bad, it is quite normal that you get very different results.

The separate fits looked ok.

[quote=“Ion”]The separate fits looked ok.[/quote]Please, elaborate whether their “look is ok” or their “chi2 values” are Ok. If the later is your case then, please, send us those values.

The file contains the values given by gMinuit->mnstat after the first fit which is using a function : f(t)=Ce^(-mt), where I limited t to [3,9].

After this I fitted m®=a-b/r+c*r; where r is limited to [2,6.5]
In this case I got fmin=0.267 and fedm=1.964e-013 and istat=3.

Ion
out.txt (5 KB)

Is fmin your chi2 ? How many degrees of freedom has your fit ?
Values too small are also not good, it means probably your error are over-estimated.

Lorenzo

Fmin is chi2. First fit has two parameters and second fit three parameters.