View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Generalized Nonlinear Non-analytic Chi-Square Fitting

4.7 | 7 ratings Rate this file 18 Downloads (last 30 days) File Size: 42 KB File ID: #9592 Version: 1.1
image thumbnail

Generalized Nonlinear Non-analytic Chi-Square Fitting



11 Jan 2006 (Updated )

Performs chi-square fit with uncertainty estimation when measurement errors are known.

| Watch this File

File Information

fitChiSquare is a generalized chi-square fitting routine for any model function when data measurement errors are known; it returns the model parameters and their uncertainties at the delta chi-square = 1 boundary (68% confidence interval). It also returns the chi-square and degrees of freedom (dof) of the fit. The goodness-of-fit may be estimated by comparing chi-square/dof to 1 (<1 is a good fit; >>1 indicates a poor fit). Alternatively, it returns the fit and measurement errors when the model is known - see ErrorUnknown option.

Type "help fitChiSquare" or see the header for usage.

This function calculates the data variance from reported measurement errors, then calculates the chi-square fit. Then the function finds the projection of the delta chi^2 = 1 contour onto each parameter. In the case that the parameter uncertainties are normally distributed, the delta chi^2 = 1 method gives the 68% confidence limit for the parameters. Monte Carlo or investigations of many data sets should be used to confirm the parameter uncertainties are normally distributed.

Note that when used solely as a model fitter, fitChiSquare will generally run more slowly than fminsearch or lsqnonlin. If you are only interested in data optimization, it is recommended that you use one of the built-in functions.

If one encounters the following error message:

    Unexpected termination flag 0 in non-estimating variable
    minimization during uncertainty estimation

this is because the non-varying parameter minimization routine has encountered its iteration or evaluation limit. Raise UncOptions.MaxFunEvals or UncOptions.MaxIter and try again.

Note: If the user can not use lsqnonlin (i.e., the optimization toolbox is not installed), the program will use the built-in function fminsearch instead. This may reduce the robustness of the calculation.

1. W.H. Press, B.P. Flannery, S.A. Teukolsky, W.T. Vetterling. Numerical Recipes; The Art of Scientific Computing. (Cambridge University Press: Cambridge). 1986.
2. P.R. Bevington, D.K. Robinson. Data Reduction and Error Analysis for the Physical Sciences. (McGraw-Hill: New York). 1992.


This file inspired Gui For Generalized Nonlinear Non Analytic Chi Square Fitting.

MATLAB release MATLAB 7.0.1 (R14SP1)
Other requirements Java 2 run-time environment required to use FitChiTool.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (14)
08 Jan 2015 Christoph

Can someone help me with editing the functions? even using the ModelEditor (very convenient) I am not sure how actually add a model.

Any attempt results in that the modeleditor gets broken (error attached below).

I want to add a double exponential fit to the existing fit functions, e.g. p(1)*exp(p(2)*x)+p(3)*exp(p(4)*x).

>> ModelEditor
Index exceeds matrix dimensions.

Error in ModelEditor>ModelEditor_OpeningFcn (line 62)
if nargin && ischar(varargin{1})

Error in gui_mainfcn (line 221)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure),

Error in ModelEditor (line 43)
gui_mainfcn(gui_State, varargin{:});

05 May 2012 Stephan Koehler

Question: Could you also provide 90% confidence intervals for the fitting parameters, instead of just the 68%?

05 May 2012 Stephan Koehler

17 May 2011 Giorgio De Nunzio

Hi all! Thanks, Nathaniel, for this really useful tool! I have a problem with one of the options: FitIndex. It looks like this option is not used anywhere. Setting it has no effect on the result (all the parameters undergo fitting, no matter what the value of op.FitIndex is). Investigating the code led me to discover that FitIndex is not explicitly used in fitChiSquare.m, and that "options" is then passed to lsqnonlin or fminsearch, which imho cannot use FitIndex. Can you please enlighten me? In the meantime, I'll try fixing a parameter by giving strict bounds for its value. Thanks! Giorgio De Nunzio

18 Nov 2010 J West

J West (view profile)

Any thoughts on why this sometimes returns parameter uncertainties = 0 (i.e. dParams(i).du = 0, and dParams(i).lVal = params(i))?

This seems to occur for me in cases where the uncertainties are definitely significant, but are also significantly skewed rather than normally distributed (when tested by Monte Carlo simulation).


Comment only
14 Apr 2009 Dunno

Dunno (view profile)

The output uncertainty of fit parameters is incorrect. The function is also slow compare to fminsearch. I actually trust the best fit value from fminsearch more than the one from this code, parameter uncertainty can probably be estimated using algorithm of Jone D'Errico.

Comment only
27 Oct 2008 sissi sissi

if the result turn out :"Adjusted R-square:NAN "
what does it mean ? and how to get a better fitting ?
thank you

Comment only
13 Jun 2008 Christian G

Great stuff! I only used the core function fitChiSquare so far, because I couldn't get fitChiTool to work. I have R14 SP 1 with all relevant toolboxes, but get the following error:

??? Undefined command/function 'FitChiToolModel'.

Error in ==> FitChiTool>makeParamTable at 152
model = FitChiToolModel(data,namevec);

11 Nov 2006 ingmar Riedel-Kruse

Very useful!

07 Apr 2006 Nathaniel Brahms

fitChiSquareExample now works properly

Comment only
09 Mar 2006 F Moisy

When I run the example, fitChiSquareExample.m, I obtain the following error (using R14):

??? Undefined command/function 'fitLibrary'.

Error in ==> fitChiSquareExample at 29

Comment only
31 Jan 2006 Rob deCarvalho

This is an extremely useful function for anyone who routinely does least squares fitting. It provides the ability of fitting data to a user defined function having both an arbitrary number of independent variables and an arbitrary number of fit parameters. Unlike most fitting routines, it contains an algorithm for estimating the uncertainties in the fit parameters. The statistical methods required to estimate parameter errors for non-linear function are generally quite complex, and it is appearant that a good deal of thought went into creating the fitChiSquare mfile. Once the syntax is figured out, the routine seems to be quite well behaved. Since most people will use this as a "black-box" routine, my one suggestion might be to see a general description of the algorithm and a full discloser of the statistical properties the data must posses for the error estimations to be rigorously correct.

13 Jan 2006 Nathaniel Brahms

Working example is now included

Comment only
13 Jan 2006 lousy squat

need to include a working example

Comment only
13 Jan 2006

New version (2.0)

Release includes missing between function and an example function

16 Jan 2006

Description change

26 Jan 2006

New version:
Added bound feature, some error checking, updated documentation

03 Feb 2006

New version (2.2) including ErrorUnknown flag.

13 Feb 2006

Expanded plotting features and options. See help.

dParams is now a normal array, not a cell array.

06 Mar 2006

New version (2.4.1) including:
1. Output syntax change (gof struct)
2. Added eta-square (correlation index)
3. Workaround for MATLAB struct array bug, which could cause a crash

fitChiSquareExample now includes an example model library file

10 Mar 2006

Added missing fitLibrary() function to zip file

28 Apr 2006

Added Scale option

09 Jun 2006

Updated model library

01 Sep 2009 1.1

Minor bug fix, support for 4-parameter syntax.

Contact us