Fitting Functions

Any data fitting must have a function that describes the process you are analyzing.

All fitting packages provide a set of generic built-in functions. These typically include linear, polynomial, exponential and some other common dependencies. Generic functions are defined in the most common form with maximum number of unrestricted parameters, which makes them useable with a broad range of processes. Such flexibility, however, takes its toll on accuracy of fitting and often in efforts it takes to converge such generic fit.

How to choose correct function?

As described here, you must have reasonable understanding of the process you are analyzing. At the very least it is necessary to know general type of dependence (exponential, linear, titration, polynomial, mixed) and the number of independent variables. At this moment most data analyzed in this group involve only one meaningful independent variable – time –although other situations are possible, for example when both time and concentration change across a series.  

From visual, qualitative evaluation of data you need to estimate how many processes are involved and how many parameters you need to describe them. It is always a good idea to use the minimum number of parameters needed to describe your process adequately. Conventional wisdom goes that one can fit any process with sufficient number of exponents, typically over four. Thus, in kinetic analysis start with the minimum number of exponents and advance to higher order if you are sure you cannot describe data adequately with the selected order.

In some cases it also helps to limit the range of acceptable values of parameters, such as limiting kinetic rates to positive values. Such restrictions are known as constraints.

Why write custom fit functions?

While Igor allows imposing numerical constraints on coefficients of built-in functions and even setting them tem to a fixed value, you cannot change how parameters are used in calculations. It is also not possible to use multiple built-in functions, such as kinetics with with liner drift. To do this you need to write a user fitting function.

User fitting functions are similar to all other functions except that they must adhere to specific parameter format to make them useable in fitting. Such functions must take two arguments: a wave containing all parameters for calculation and the value of independent variable. Here is an example of a single exponent in general form and its variant suitable for fitting:

regular fitting

function MyExp(A, B, k, x)
variable A, B, K, x;
return A + B * exp(k*x)
end

function MyFitExp(w, x): FitFunc
wave w;
variable x;
return w[0] + w[1]*exp(w[2]*x)
end

Calculations in both cases are identical.


Writing custom fit functions

Before proceeding to write custom fitting functions familiarize yourself with the concept of function in general here and in Igor manual.

Devising custom fitting functions is easier than it may seem. You can either use New Fit Function dialog from within Curve Fitting dialog or you can write it manually.

Using New Fit Function dialog has advantages for writing simpler functions at the beginning, because it hides some house keeping steps from user. In writing fitting functions manually you will have to take care of such housekeeping, but you can devise more complex functions by dividing calculation in to several steps. Writing a series of analogous functions is also easier in manual mode by copying and pasting.


How to write a custom fitting function manually:

  1. On a piece of paper write expression for the function

  2. Transfer expression in to a regular user-defines function using letter abbreviations for parameters (A, B, C, D etc) and x for independent variable.

  3. Test your function:

    Make test data and calibration waves make /n=128 MyTestWave, MyTestClb
    Set calibration wave to cover representative test range MyTestClb = -5+p*0.2
    Set values of MyTestWave using your new function and MyTestClb as an independent variable and a set of reasonable coefficients MyTestWave = MyNewFucntion(1.5, 20, 0.1, MyTestClb)
    Make a plot of MyTestWave vs. MyTestClb and confirm that you got expected profile Display MyTestWave vs MyTestClb

    Add special comments to header that make it easer to understand you function in Curve Fitting dialog.

    Copy/paste code in to your fucntion just below variables declaration and change highlighted values leaving plain syntaxis as is. Depending on the number of coefficients you may need to add or delete w[x] = X lines at the end. You can name coefficients as you wish as long at such name is valid.

    //CurveFitDialog/ Equation:
    //CurveFitDialog/ f(x) = A+B*exp((x-t0)*k1)
    //CurveFitDialog/ End of Equation
    //CurveFitDialog/ Independent Variables 1
    //CurveFitDialog/ x
    //CurveFitDialog/ Coefficients 4
    //CurveFitDialog/ w[0] = A
    //CurveFitDialog/ w[1] = B
    //CurveFitDialog/ w[2] = t0
    //CurveFitDialog/ w[3] = k1

  4. Lastly, replace all parameters in function for letters to wave entries: A - w[0], B - w[1], C - w[2], except for dependent variable x; Replace all parameters but variable x in function header and declaration with wave w as in the example above.

Comments