Fit

This implementation incorporates the capability of nonlinear least squares fitting using the Marquardt-Levenberg Algorithm. It may fit any user-defined function to any set of data points (x,y) or (x,y,z). x, y, z and the function's return type must be real! Any variable occurring in the function body may serve as a fit parameter (fitting functions without adjustable parameters make no sense).

Syntax:

     fit {[xrange]} {[yrange]} <function>
         '<datafile>' {datafile-modifiers}
          via {'<parameter file>' | <var1>,<var2>,...}

Notice that via is now a required keyword, to distinguish it from a 'scanf' format string.

[xrange] and [yrange] are of the form [{variable=}{<min>}{:<max>}], allowing the range of the fit to be limited temporarily in a manner analogous to plot. <function> is any valid gnuplot expression, although it is usual to use a previously user-defined function of the form f(x) or f(x,y). <datafile> is treated as in the plot command. All the modifiers for datafiles (using, every,...) in plot are available here (except smooth) — see plot datafile for full details. The default columns for x and y are 1 and 2. These may be changed by the using x:y mechanism. If using has a third entry (a column or an expression), it will be interpreted as the standard deviation of each y value and will be used to compute the weight; otherwise all data will be weighted equally. If four columns are specified, they are x:y:z:error — note that an error must be specified in order to perform a 3-d fit. If errors are not available, a constant value can be specified, e.g., using ...:(1).

Initial values for the parameters to be fit may be specified in a (load-)file wherein each line is of the form:

     varname = value

Comments, marked by '#', and blank lines are permissible. The special form

     varname = value       # FIXED

means that the variable is treated as a fixed parameter that is initialized but will not be adjusted. It is not necessary (but sometimes useful for clarity) to specify them at all. The keyword # FIXED has to appear in exactly this form.

The other means of specifying the adjustable parameters is to provide a comma-separated list of variable names after the via keyword. If any of these variables do not yet exist within the current gnuplot session, they are created with an initial value of 1.0, but the fit is more likely to converge if a more appropriate starting value is given. If this form is used, it may prove beneficial to iterate the fit, allowing only one or two variables to be adjusted at a time until a reasonably close fit is obtained, before allowing fit to vary all parameters.

After each iteration step, detailed information is given about the fit's state, both on the screen and on a logfile "fit.log". This file will never be erased but always appended to so that the fit's history isn't lost. After each iteration step, the fit may be interrupted by pressing Ctrl-C (any key but Ctrl-C under MSDOS and Atari Multitasking Systems). Then you have the options of stopping (and accepting the current parameter values), continuing the iteration of the fit, or executing a gnuplot command specified by an environment variable FIT_SCRIPT. A plot or load command may be useful in this context.

Special gnuplot variables:

     FIT_LIMIT
may be specified to change the default epsilon limit (1e-5). When the sum of squared residuals changes between two iteration steps by less than a factor of this number, the fit is considered to have 'converged'.

Once the fit is converged, the final values may be stored in (load-)file suitable for use as an initial-value file, as discussed above. Please see update for details.

     FIT_MAXITER
may be specified to limit the number of iterations performed without convergence by FIT_LIMIT. A value of 0 (or not defining it at all) means that there is no limit.

[FIT_SKIP was available in previous releases of gnufit. Its functionality is now obtained using the every modifier for datafiles. FIT_INDEX was previously available in order to allow multi-branch fitting. Multi-branch fitting in 2-d can now be done as a pseudo-3-d fit in which the y values are the dataline number (using 1:-1:...) or index (using 1:-2:...).]

Environment variables:

     FIT_LOG
changes the logfile's path from './fit.log' (write permission is necessary).

     FIT_SCRIPT
specifies a command to be executed after an user interrupt.

Examples:

     f(x) = a*x**2 + b*x + c
     FIT_LIMIT = 1e-6
     fit f(x) 'measured.dat' via 'start.par'
     fit f(x) 'measured.dat' using 3:($7-5) via 'start.par'
     fit f(x) './data/trash.dat' using 1:2:3 via a, b, c
     fit f(x,y) 'surface.dat' using 1:2:3:(1) via a, b, c


Subsections