Curve Fitting

I am relatively new to Igor. I figured out how to graph, and fit very basic curves.
One problem I was just confronted with was fitting a curve to a custom fit.
Instead of any of the Igor provided fits, I would like to make one.

Specifically, my data follows the heating and cooling of a sample.
I tried fitting the cooling part of the curve to an exponential, but that did not fit well.

I was then told to try to fit it to an "error function". I do not know how to make a fit though.
I attached the formula for the error function.

Any advice?
This should do it.
Function erffunc(w,x):fitfunc
    Wave w
    variable x
    //w[0] = background
    //w[1] = direction and magnitude of function
    //w[2] = position of centre
    //w[3] = width of erf
    if(abs(w[3])<1e-40)
        w[3] = 1e-40
    endif

    return w[0]+w[1]*erf((w[2]-x)/(abs(w[3])*sqrt(2)))
End
And running Andy's code through the Edit Fit Function dialog, you can get the following code with comments that will cause curve fits to be labelled with nice mnemonic coefficient names:

Function erffunc(w,x) : FitFunc
    Wave w
    Variable x

    //CurveFitDialog/ These comments were created by the Curve Fitting dialog. Altering them will
    //CurveFitDialog/ make the function less convenient to work with in the Curve Fitting dialog.
    //CurveFitDialog/ Equation:
    //CurveFitDialog/ //w[0] = background
    //CurveFitDialog/ //w[1] = direction and magnitude of function
    //CurveFitDialog/ //w[2] = position of centre
    //CurveFitDialog/ //w[3] = width of erf
    //CurveFitDialog/ if(abs(w)<1e-40)
    //CurveFitDialog/   w = 1e-40
    //CurveFitDialog/ endif
    //CurveFitDialog/
    //CurveFitDialog/ f(x) = y0+a*erf((x0-x)/(abs(w)*sqrt(2)))
    //CurveFitDialog/ End of Equation
    //CurveFitDialog/ Independent Variables 1
    //CurveFitDialog/ x
    //CurveFitDialog/ Coefficients 4
    //CurveFitDialog/ w[0] = y0
    //CurveFitDialog/ w[1] = A
    //CurveFitDialog/ w[2] = x0
    //CurveFitDialog/ w[3] = w

    //w[3][0] = background
    //w[3][1] = direction and magnitude of function
    //w[3][2] = position of centre
    //w[3][3] = width of erf
    if(abs(w[3])<1e-40)
        w[3] = 1e-40
    endif
   
    return w[0]+w[1]*erf((w[2]-x)/(abs(w[3])*sqrt(2)))
End


Execute this command on Igor's command line to read all about curve fitting using the dialog:
DisplayHelpTopic "Curve Fitting Using the Curve Fitting Dialog"

That includes a section on doing user-defined fits:
DisplayHelpTopic "Fitting to a User-Defined Function"

Of course, part of your problem is that you have the fundamental defining equation for the error function, which doesn't tell you that Igor includes a built-in function that saves you having to figure out how to do that integration yourself.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Now where do I use this code?
In the command window?
I tried using the Edit Fit Function, but that button was not enabled, only New Fit Function was.
I believe that if you paste it into the procedure window (select Windows->Procedure Windows->Procedure Window), then the Curve Fitting dialog will recognize it. The Curve Fitting dialog looks in the procedure window for functions declared as fit functions.

If you get a compile error after copying from your browser and pasting into the procedure window, this is because the browser (Safari on Macintosh I think) uses non-breaking spaces instead of regular spaces. When this happens, Igor will select the offending character in the procedure window. Merely replace it with a space.


Thanks, you guys were a lot of help.

Just out of curiosity, where did you get that formula for the error function?
That looks quite a bit different from the error function I posted earlier.
Actually, I need the complimentary error function (erfc),
but that is simply:
erfc(x) = 1 - erf(x)
Erf.png (1.3 KB)
THe function posted above should fit both situations, it converts erf to erfc via the magnitude parameter w[1]
Kramer51 wrote:
Thanks, you guys were a lot of help.

Just out of curiosity, where did you get that formula for the error function?
That looks quite a bit different from the error function I posted earlier.
Actually, I need the complimentary error function (erfc),
but that is simply:
erfc(x) = 1 - erf(x)


The integral you posted is the fundamental definition of the error function. Igor has a built-in function that returns values of the integral, but without actually having to do any integration.

The "extra" stuff added to the fitting function modifies the standard error function so that the fit can change vertical offset (w[0]), the amplitude (w[1]), the X position (w[2]) and the width (w[3]). My version of the function simply adds some comments to the function that Igor's curve fitting code will interpret so that it can use mnemonic names for the fitting coefficients. That is, Y0 for vertical offset, A for amplitude, x0 for X position, and w for width.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com