How do you use Erf in a TF1 fitter?

Hello,

I have a custom function I would like to fit. It uses TMath::Erf. However, I can’t find the correct syntax to use.

Here is the function:

TF1 *vsfit = new TF1("vsfit","[0]*TMath::Erf((-[1]-x)/[2])-[0]*TMath::Erf(([1]-x)/[2])",-12,13);

I tried adding the TMath::, which did not make any difference. I have used custom functions previously and they worked with using the [0], [1] method, but this is not working.

What is the correct way to write this?

You don’t write what error you get.
Your definition seems fine to me.
I get a nice picture when I try:
vsfit->SetParameters(1, 2, 3); vsfit->Draw();

[quote=“Wile E. Coyote”]You don’t write what error you get.
Your definition seems fine to me.
I get a nice picture when I try:
vsfit->SetParameters(1, 2, 3); vsfit->Draw();[/quote]

I don’t get any result for the fitted parameters. Here is my data:

-10.16 0.0610822360 -8.89 0.2498000000 -7.62 0.4424769620 -6.35 0.6099413665 -5.08 0.7745726368 -3.81 0.9352315789 -2.54 1.0685637280 -1.27 1.1437790524 0.00 1.1682100000 1.27 1.1395995006 2.54 1.0682873592 3.81 0.9459954831 5.08 0.7963516209 6.35 0.6207590512 7.62 0.4473838107 8.89 0.3062699875 10.16 0.1556398010 11.43 0.0503145342 12.70 0.0069847690

Here is the subroutine:

int vsfit(double* coef) { double vscanx[19]; double vscany[19]; double vspar[3]; ifstream vsfile; vsfile.open("vscan.dat"); for(int k = 0; k < 19; k++) { vsfile >> vscanx[k]; vsfile >> vscany[k]; } TGraph* VSCAN = new TGraph(19,vscanx,vscany); TF1 *vsfit = new TF1("vsfit","[0]*TMath::Erf((-[1]-x)/[2])-[0]*TMath::Erf(([1]-x)/[2])",-11,13); VSCAN->Fit(vsfit,"qr"); vsfit->GetParameters(vspar); *coef = vspar[2]; delete VSCAN; delete vsfit; return(0); }

I get very large values for [0], and zeroes for [1] and [2]. This is not right.

You MUST set initial values for all parameters (i.e. after you create the function and before you try to use it in a fit), otherwise expect problems.
For example, try:
vsfit->SetParameters(-1, 1, 1);

[quote=“Wile E. Coyote”]You MUST set initial values for all parameters (i.e. after you create the function and before you try to use it in a fit), otherwise expect problems.
For example, try:
vsfit->SetParameters(-1, 1, 1);[/quote]

I was wondering if I could initialize them without fixing them. Thank you, this solves it!