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.

FindFit[data,{expr,cons},pars,vars]

finds a best fit subject to the parameter constraints cons.

Details and Options

  • 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 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 leastsquares 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
    EvaluationMonitor Noneexpression to evaluate whenever expr is evaluated
    FitRegularizationNoneregularization for pars for models
    Gradient Automaticthe list of gradient components for expr
    MaxIterations Automaticmaximum number of iterations to use
    Method Automaticmethod to use
    NormFunction Normthe norm to minimize
    PrecisionGoalAutomaticthe precision sought
    StepMonitor Noneexpression to evaluate whenever a step is taken
    WorkingPrecision Automaticthe precision used in internal computations
  • The default settings for AccuracyGoal and PrecisionGoal are WorkingPrecision/2.
  • The settings for AccuracyGoal and PrecisionGoal specify the number of digits to seek in both the values of the parameters returned, and the value of the NormFunction.
  • With NormFunction->f and FitRegularization->rfun, Fit finds the values of pars that minimize normf[{expr(pars,x1,y1,)-f1,}]+rfun[pars].
  • The setting for NormFunction can be given in the following forms:
  • fa function f that is applied to the residual
    {"HuberPenalty",α}sum of the Huber penalty function for each component
    {"DeadzoneLinearPenalty",α}sum of deadzone linear penalty function for each component
  • The setting for FitRegularization may be given in the following forms:
  • Noneno regularization
    rfunregularize with rfun[a]
    {"Tikhonov", λ}regularize with
    {"LASSO",λ}regularize with
    {"Variation",λ}regularize with lambda||TemplateBox[{Differences, paclet:ref/Differences}, RefLink, BaseStyle -> {2ColumnTableMod}][a]||^2
    {"TotalVariation",λ}regularize with lambda||TemplateBox[{Differences, paclet:ref/Differences}, RefLink, BaseStyle -> {2ColumnTableMod}][a]||_1
    {"Curvature",λ}regularize with lambda||TemplateBox[{Differences, paclet:ref/Differences}, RefLink, BaseStyle -> {2ColumnTableMod}][a,2]||^2
    {r1,r2,}regularize with the sum of terms from r1,
  • FindFit continues until either of the goals specified by AccuracyGoal or PrecisionGoal is achieved.
  • Possible settings for Method include "ConjugateGradient", "PrincipalAxis", "LevenbergMarquardt", "Newton", "NMinimize", and "QuasiNewton", with the default being Automatic.

Examples

open allclose all

Basic Examples  (1)

Find a nonlinear fit to a list of primes:

Fit the best-fit parameters , , and :

Evaluate the fitted function:

Scope  (7)

Simple Exponential Fitting  (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 of 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:

Constraints and Starting Values  (2)

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:

Multivariate Data  (2)

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:

Options  (7)

EvaluationMonitor  (1)

Count evaluations of the model with numerical values of the parameters:

Gradient  (1)

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 since there is a point in the data with :

Specify finite differences to avoid the removable singularity:

Show the data with the fit:

MaxIterations  (1)

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:

Method  (1)

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):

NormFunction  (1)

The default is to find the best least-squares fit:

Use the -norm instead:

StepMonitor  (1)

Show steps taken in parameter space to fit the MichaelisMenten model to experimental data:

Data from an experiment on the rate of an enzymatic reaction:

Show how the parameters evolve during the search:

WorkingPrecision  (1)

Use a higher WorkingPrecision to get a more precise parameter estimate:

Applications  (7)

Mathematical Data  (2)

Fit a list of Fibonacci numbers:

The base is approximately the golden ratio:

Generate data with some noise present:

Fit the data:

Experimental Data  (3)

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 and pressure/1000 :

Scale the pressure to improve the conditioning of the model:

Find the parameters 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 , n-pentane , and isopentane :

Find the parameters that best fit the data starting with values having the right size:

Differential Equations  (2)

Find parameters in a differential equation model that give the best fit:

Define a model so the solution for a set of parameters is reused for all 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:

Properties & Relations  (6)

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:

FindFit will use the time stamps of a TimeSeries as variables:

Rescale the time stamps and fit again:

Find fit for the values:

FindFit acts pathwise on a multipath TemporalData:

Possible Issues  (1)

Starting Values  (1)

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:

Wolfram Research (2003), FindFit, Wolfram Language function, https://reference.wolfram.com/language/ref/FindFit.html (updated 2019).

Text

Wolfram Research (2003), FindFit, Wolfram Language function, https://reference.wolfram.com/language/ref/FindFit.html (updated 2019).

CMS

Wolfram Language. 2003. "FindFit." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2019. https://reference.wolfram.com/language/ref/FindFit.html.

APA

Wolfram Language. (2003). FindFit. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FindFit.html

BibTeX

@misc{reference.wolfram_2024_findfit, author="Wolfram Research", title="{FindFit}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/FindFit.html}", note=[Accessed: 21-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_findfit, organization={Wolfram Research}, title={FindFit}, year={2019}, url={https://reference.wolfram.com/language/ref/FindFit.html}, note=[Accessed: 21-November-2024 ]}