Markwardt IDL Page

What's New

   Curve Fitting
   Fitting Tutorial
   Save Files
   Full Listing


Markwardt IDL Pages
Entire Web

Craig's techie blog.

Home Page of Craig Markwardt


IDL Curve Fitting and Function Optimization

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 Recipes.

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 information.

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 ( 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.

Jan 03 2017141 kb mpfit.proREQUIRED  
Oct 02 201236 kb mpfitfun.proRecommended (1D)  
May 19 201628 kb mpfit2dfun.proRecommended (2D)  
May 19 201631 kb  

MPCURVEFIT.PRO is a drop-in replacement for IDL's CURVEFIT.PRO, but with the additional features of the MPFIT family of functions.

May 19 201625 kb  

You can download the entire MPFIT package as a gzip'ed tar file or zip file.

Jan 03 2017154 kb mpfit.tar.gz  
Jan 03 2017194 kb  
Jun 22 20104 kb MPREADMEGeneral Documentation  
Dec 06 20031 kb INSTALLInstallation Instructions  
Feb 18 200926 kb cmpfit-1.1.tar.gz  
Feb 18 200929 kb  
Nov 13 201029 kb cmpfit-1.2.tar.gz  
Nov 13 201033 kb  
Jun 02 201629 kb cmpfit-1.3a.tar.gz  
Jun 02 201633 kb  

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 your result.

NEW! (2016-05-19) 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 other.

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.

Dec 21 20119 kb  
Jan 03 201727 kb  

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 distributions.

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.

Jun 22 20108 kb mpchitest.proChi-square  
Jun 22 201015 kb mpchilim.proChi-square  
Jun 22 201014 kb mpftest.proF distribution  
Jun 22 20108 kb mpnormtest.proNormal  
Nov 24 20068 kb mpnormlim.proNormal  

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.

Apr 14 20093 kb  

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 more circumstances.

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 compute intensive.

Dec 21 201122 kb mpfitpeak.pro1D Gaussian  
May 19 201618 kb mpfit2dpeak.pro2D Gaussian  
Jan 03 2017141 kb mpfit.proREQUIRED  
Oct 02 201236 kb mpfitfun.proREQUIRED  
May 19 201628 kb mpfit2dfun.proREQUIRED  
Jun 22 201012 kb  

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 numerically.

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.

May 19 201670 kb  

Copyright © 1997-2010 Craig B. Markwardt
Last Modified on 2017-01-03 13:57:28 by Craig Markwardt