MPFIT - Robust non-linear least squares curve fitting
These IDL routines provide a robust and
relatively fast way to perform least-squares curve and surface
fitting. The algorithms are translated from MINPACK-1, which is a rugged
minimization routine found on Netlib, and distributed with
permission. This algorithm is more desirable than CURVEFIT because it
is generally more stable and less likely to crash than the brute-force
approach taken by CURVEFIT, which is based upon Numerical
MPFIT has additional capabilities not found in CURVEFIT. Model
parameters can be "frozen" (that is, held constant during the fitting
process). Simple boundary constraints can be imposed on parameter
values, which can be helpful to keep parameters from becoming
negative, for example. Please see the documentation for the PARINFO
keyword if you wish to use this facility.
You can cite MPFIT in your papers! See the FAQ entry on citations for more
A C version of the library is available! See
the special page on the C Version of MPFIT.
Craig's presentation at the IDL Visualize 2009
workshop in Washington DC in April 2009 explains how equation
solving and parameter constraints work. This zip file (Markwardt-MPFIT-Visualize2009.zip)
contains the presentation as well as example codes relevant to the
talk (the PDF and Powerpoint presentations
are also available separately).
Frequently Asked Questions
There are several files included in the MPFIT package. Not all of
them are required, but you will probably wish to download more than
one. This chart can help you decide
between the choices. I recommend the combination of MPFIT.PRO and
MPFITFUN.PRO as being suitable for most 1-dimensional problems.
MPCURVEFIT.PRO is a drop-in replacement for IDL's CURVEFIT.PRO, but
with the additional features of the MPFIT family of functions.
You can download the entire MPFIT package as a gzip'ed tar file or zip
Specialized Applications - Additional functions which aid in specialized fitting applications
This section contains functions for specialized applications related
to fitting with the MPFIT family of functions. Generally you will use
thse functions in conjunction with the core routines above to achive
The function MPFITCOVAR is used fit small to modest-sized problems
where the measurement variance-covariance matrix is non-diagonal.
This occurs when measurement sample errors are correlated with each
The function MPPROPERR can be used to estimate the uncertainty of your
model function, via standard propagation of error formulae. For
example, if you have a best fit model with parameter uncertainties,
then MPPROPERR can be used to see where these parameter uncertainties
cause the most uncertainty in the model function that is compared to
your data. See the function for more documentation and examples.
Statistical Tests - Hypothesis Testing and Confidence Interval Estimation for Chi-square, F, and Normal Distributions
The following functions perform statistical tests and estimate
confidence limits. MPCHITEST, MPFTEST and MPNORMTEST are useful for
hypothesis testing for variables distributed as the chi-square, F, and
normal distributions. The F distribution is used in testing ratios of
variances and for addition of parameters in chi square fitting. MPCHILIM
and MPNORMLIM estimate confidence limits for chi-square and normal
These functions are based on the CEPHES library of special
functions by Stephen Moshier, available from Netlib, and do not depend on
any special function implementations from IDL.
Likelihoods - Alternate likelihood functions
When fitting a linear model to data with errors in both X and Y,
the standard least-squares likelihood function is not appropriate,
since it considers only errors in Y. LINFITEX is an alternate
likelihood function for fitting a linear model to data with errors in
both X and Y, based on Numerical Recipes by Press et al. You
use it as your model function with MPFIT (not MPFITFUN), and specify
the data values using the FUNCTARGS keyword. Please see the
documentation within the module for more information and examples of
how to use it.
Peak Fitting - Specialized Fitting for Peak and Ellipse Applications
These are a few specialized fitting applications which use the
MPFIT library of routines. The functions MPFITPEAK and MPFIT2DPEAK
replace the built-in IDL functions GAUSSFIT and GAUSS2DFIT.
The function MPFITELLIPSE is used to fit an elliptical shape to a
set of X-Y scatter points. Users should still consult the ODRPack Page which is more general
and robust for these types of problems, however MPFITELLIPSE is good
for quick and dirty problems where the measurement error is small
compared to the ellipse axes.
These functions are distinguished in several ways. First, they use
the MPFIT library, and so the fitting should be more robust. That
also means you can use the PARINFO keyword to hold parameters
constant, etc. Second, I use a different technique to estimate the
starting parameter values, which I think will be more robust under
Finally, for the peak fitters, you are not limited to a Gaussian
function; the Lorentzian and Moffat peaks are available as well. [ The
Moffat function is a modified Lorentzian with variable power law
index. (Moffat, A. F. J. 1969, Astronomy & Astrophysics, v. 3,
p. 455-461). ] However, you may notice that the fitting process is
slower. That is simply the nature of MPFIT -- it is definitely more
TNMIN - Non-linear optimization
TNMIN is a general non-linear function minimizer based on a
truncated-Newton method by Stephen Nash. TNMIN will minimize any
function that can be expressed in IDL. TNMIN can use
explicitly-calculated partial derivatives, or estimate them
TNMIN has all of the enhancements that MPFIT
has, including the ability to fix parameters, to place simple bounds
on parameters, and to tie one parameter value to other parameter
values. Both MPFIT and TNMIN share same PARINFO keyword mechanism to
enable this feature.
MPFIT is still recommended for curve fitting applications, but
TNMIN will be useful when general minimization is desired.