Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Data Manipulation > Numerical Data > Curve Fitting & Approximate Functions >
Mathematica > Data Manipulation > Statistics > Curve Fitting & Approximate Functions >
Mathematica > Mathematics and Algorithms > Statistics > Curve Fitting & Approximate Functions >

FindFit

FindFit[data, expr, pars, vars]
finds numerical values of the parameters pars that make expr give a best fit to data as a function of vars. The data can have the form {{x1, y1, ..., f1}, {x2, y2, ..., f2}, ...}, where the number of coordinates x, y, ... is equal to the number of variables in the list vars. The data can also be of the form {f1, f2, ...}, with a single coordinate assumed to take values 1, 2, ....
FindFit[data, {expr, cons}, pars, vars]
finds a best fit subject to the parameter constraints cons.
  • FindFit returns a list of replacements for par1, par2, ....
  • The expression expr must yield a numerical value when pars and vars are all numerical.
  • The expression expr can depend either linearly or nonlinearly on the pari.
  • In the linear case, FindFit finds a globally optimal fit.
  • In the nonlinear case, it finds in general only a locally optimal fit.
  • FindFit[data, expr, {{par1, p1}, {par2, p2}, ...}, vars] starts the search for a fit with {par1->p1, par2->p2, ...}.
  • FindFit by default finds a least-squares fit.
  • The option NormFunction->f specifies that the norm f[residual] should be minimized.
  • The constraints cons can contain equations, inequalities or logical combinations of these.
  • The following options can be given:
AccuracyGoalAutomaticthe accuracy sought
EvaluationMonitorNoneexpression to evaluate whenever expr is evaluated
GradientAutomaticthe list of gradient components for expr
MaxIterationsAutomaticmaximum number of iterations to use
MethodAutomaticmethod to use
NormFunctionNormthe norm to minimize
PrecisionGoalAutomaticthe precision sought
StepMonitorNoneexpression to evaluate whenever a step is taken
WorkingPrecisionAutomaticthe precision used in internal computations
  • Possible settings for Method include "ConjugateGradient", "Gradient", "LevenbergMarquardt", "Newton", "NMinimize" and "QuasiNewton", with the default being Automatic.
Find a nonlinear fit to a list of primes:
Fit the best-fit parameters a, b, and c:
Evaluate the fitted function:
Find a nonlinear fit to a list of primes:
In[1]:=
Click for copyable input
Out[1]=
Fit the best-fit parameters a, b, and c:
In[2]:=
Click for copyable input
Out[2]=
Evaluate the fitted function:
In[3]:=
Click for copyable input
Out[3]=
Fit data to a model of exponential decay:
The resulting model function:
Show the data with the model:
Separate the time from the response:
Looking at residuals gives a good indication whether the model is a good fit:
Use a linear fit on the logarithm of the data for a model of exponential decay:
The logarithm of the exponential model is a linear model for the logarithm of the data:
The exponential model formed by exponentiating the model for the logarithm:
Exponential fit as parameters in a differential equation:
The model function is the solution of a differential equation:
Using caching is faster:
Find a constrained fit:
Fit to the model with positive amplitude and frequency between 1 and 2:
Compare the fitted model to the data:
The residuals show a pattern, indicating that the frequency constraint is too strict:
Give starting values for some parameters to get a better fit:
Search with all parameters starting at 1:
Search with a better starting value for the parameter c:
Compare the quality of the two fits:
Fit a surface to data in two-dimensions:
Find the fit starting from an approximate position for the peak:
Show the fitted surface with the data:
Fit a model to data in four dimensions:
The residual is comparable in size to the randomness added to the data:
Count evaluations of the model with numerical values of the parameters:
Specify the model gradient to avoid problems with a removable singularity:
With symbolic derivatives FindFit fails since the derivative for Sinc is given as a generic formula:
The gradient has a singularity when x0=1 since there is a point in the data with x=1:
Specify finite differences to avoid the removable singularity:
Show the data with the fit:
When convergence is slow, increasing MaxIterations may allow convergence to the best fit:
With the default, convergence is not reached:
Convergence is reached before 1000 iterations are used:
Use Method->NMinimize to search more exhaustively for a global minimum:
Find a local minimum:
Search for a global minimum:
Compare the quality of the two fits (the global best fit is shown in red):
The default is to find the best least-squares fit:
Use the Infinity-norm instead:
Show steps taken in parameter space to fit the Michaelis-Menten model to experimental data:
Data from an experiment on the rate of an enzymatic reaction:
Show how the parameters evolve during the search:
Use a higher WorkingPrecision to get a more precise parameter estimate:
Fit a list of Fibonacci numbers:
The exponent is approximately the golden ratio:
Generate data with some noise present:
Fit the data:
Determine the approximate complexity of a computation:
Fit to the theoretical model for the complexity of an FFT:
Show the timing data and the fit in a log-log plot:
Fit a model for lubricant viscosity to experimental data:
Model for lubricant viscosity as a function of temperature x_1 and pressure/1000 x_2:
Scale the pressure to improve the conditioning of the model:
Find the parameters theta_i that best fit the scaled data:
Show a plot of the fit with the scaled experimental data:
Fit a model for the rate of isomerization of n-pentane to isopentane with experimental data:
Nonlinear model for rate as a function of hydrogen (x_1), n-pentane (x_2), and isopentane (x_3):
Find the parameters theta_i that best fit the data starting with values having the right size:
Find parameters in a differential equation model that gives the best fit:
Define a model so the solution for a set of parameters {gamma,a,b} is reused for all x values:
Find the fit with tolerance appropriate to the precision of the solutions of the ODE:
Show the fitted model with the data and the residuals:
Find how a nonsoliton initial condition for the KdV equation breaks down into two solitons:
The initial wave breaks into two separate waves:
Get the spatial grid used by NDSolve to solve the PDE:
The discretized solution at the final time:
Model of two solitons with different velocities:
Find fit with starting conditions estimated from the graph of the discretized solution:
Plots of the two-soliton model and residuals at intermediate times:
FindFit gives the parameter estimates:
NonlinearModelFit allows for extraction of additional information about the fitting:
Extract the parameter estimates:
Extract additional results and diagnostics:
For linear models, Fit and FindFit are equivalent and use LeastSquares:
Use Fit with the basis {1, x, x2}:
This is equivalent to FindFit with the model function a linear combination of the basis:
Construct a design matrix and use LeastSquares to find the coefficients:
LinearModelFit fits linear models and gives additional results about the fitting:
FindFit effectively uses FindMinimum to minimize a norm of the residual function:
The residual is the difference between response and model prediction:
FindFit gives a result consistent with normally distributed responses by default:
LogitModelFit assumes binomially distributed responses:
The fits are not identical:
The same is true for ProbitModelFit:
GeneralizedLinearModelFit allows for binomial and other distributional structures:
Model gamma-distributed responses:
Compare with the result for normally distributed responses:
Good starting values may be necessary to get a reasonable fit:
Find the fit with the default starting values of 1. The local minimum is far from correct:
Typically, better starting values are required to get a good fit:
New in 5 | Last modified in 6
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team