Close This Window

Please download official ILL logos here


For using on the web or on a screenFor printing in high resolutionWhite version, for dark backgrounds

Download PNG

Download AI

Download white PNG

Download JPG


Download white AI


The Computing for Science (CS) group supports ILL scientists, students and visitors in a number of activities including data analysis, instrument simulation and sample simulation.

Back to ILL Homepage
English French Deutsch 

All Software

Version 4.2. (1999)

Writing your own fitting routines

Non-linear fitting algorithms usually begin with a starting guess for the function parameters that minimize some target function (usually chi squared), and then attempt to improve upon the initial values by an iterative procedure. Different algorithms do this in different ways, and may also estimate the resulting uncertainties in the parameter values obtained in different ways. You can supply your own fitting routines which allows you to determine both the minimization algorithm and the target function.

The Gradient routine supplied with Mfit is adapted from the Levenberg-Marquardt algorithm leasqr.m by Richard I. Shrager, A.Jutan, and Ray Muzic. A simplex method adapted from the matlab fmins function is also provided. Des McMorrow has also written a FORTRAN .mex file interface to the excellent CERN package MINUIT, but this is machine dependent, and so is not included.

Function format

Fitting routines are .m files (or .mex files - see the Matlab external interface guide) with the syntax:

    function [p, std]=function_name(x,y,err,pin,dpin,func)

The input parameters are:

  • x, y, err - vectors containing the data. err is the unceratinty in y
  • pin - vector of initial parameter values
  • dpin - vector the same length as pin. If pin(n)=0 then parameter n is held at the same value during the fit
  • func - the name of the fitting function called. (See 'Writing fitting functions' for details)

and the function returns p and std, vectors containing the best fit estimates of the paramters, and the associated uncertainties respectively.