This program demonstrates one possilbe way to fit 2D data to a user-supplied model function, using the Gnu Scientific Library (GSL).


The idea behind this project is to create a general extension to the GSL for fitting. It is my test platform for using the GSL in other programs, and as such, it does many things that may not be useful for other applications.

As long as you can express a function, $Q$, to be minimized with respect to a set of parameters $\mathbf{a}=[a_1,a_2,\ldots]$, then the GSL can fit data using several methods. The function $Q$ is usually given by

\[ Q = \sum_{i} \left(\frac{f(x_i;\mathbf{a}) - y_i}{\sigma_i}\right)^2, \]

where $(x_i, y_i,\sigma_i)$ are the set of data points and errors in $y$, and for a single point $x$, and paramter set $\mathbf{a}$, the user-supplied model function $y = f(x;\mathbf{a})$ returns a single value $y$.

The gsl_fit_container struct

What makes all this possible is the fact that the GSL passes a void pointer to the required functions for evaluation. This means we can store all the data and paramters in a single container struct in memory and pass around pointer to it. So the gsl_fit_container struct contains three other structs, each of which contain the data, the parameters and the model functions, respectivly.

Multiple functions

In this example program, the total model function can be a sum of other functions, i.e.

\[ f(x_i;\mathbf{a}) = \sum_{j} f_j(x_i;\mathbf{a}_j) \]

This becomes useful, for example, when the data is a sum of gaussian peaks. Instead of putting all the peaks into one function call, you can write a single general function that evaluates a single gaussian function and call the function gsl_fit_add_model_* as many times as you need.

Multiple data sets

It is also possible to either fit a single data set, or several data sets to the same set of parameters and functions. For example, parameter $a_j$ may be positive for one data set and negative for the other, but the magnitude should still be the same. Another example may be the case where the same peak may be shifted between two data sets by a exact, fixed amount, but it's width and amplitude are the same.

In these cases, it becomes necessary that when the program calls the user-supplied function for evaluating $f(x;\mathbf{a})$, additional paramters are passed which indicate which data set and which peak are being calculated.

To use this feature, as you add data sets, they are numbered as you add them, starting from 0. Likewise, as you add functions, they are numbered as you add them.

Possible methods

The method of fitting can be chosen to be one of the following:

For more information on these, see the GSL documentation. It may be possible to add optional GSL extension minimization routines.


All comments and questions are welcome, see my main page for my email address. Feel free to browse these documentation pages.

Generated on Fri Jan 19 14:54:26 2007 for gsl_fit by  doxygen 1.4.7