Parametric calibration
Estimate coefficients in a nonlinear physical, statistical, or engineering model from paired measurements.
SciPy · Function
Fits a callable model to measured data using nonlinear least squares. It returns optimized parameters and an approximate covariance matrix, with options for uncertainty weighting, bounds, optimization methods, and diagnostics.
curve_fit estimates parameters for a callable model by minimizing residuals between model predictions and observed data. It returns the best-fit parameter vector and an approximate covariance matrix, while supporting measurement uncertainties, parameter bounds, optimization methods, Jacobians, missing-value policies, and optional diagnostic output.
scipy.optimize.curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, check_finite=None, bounds=(-inf, inf), method=None, jac=None, *, full_output=False, nan_policy=None, **kwargs)| Name | Type | Description |
|---|---|---|
f | callable | The model function, f(x, …). Its first argument is the independent variable; remaining arguments are fitted parameters. |
xdata | array_like | Independent-variable measurements. |
ydata | array_like | Dependent observations, nominally f(xdata, ...). |
p0 | array_like, optional | Initial parameter estimate; defaults to ones when parameters can be inferred. |
sigma | array_like, optional | Measurement uncertainty as a scalar, sequence, or covariance matrix. |
absolute_sigma | bool, optional | Whether sigma should be interpreted in absolute terms when estimating covariance. |
check_finite | bool, optional | Whether to check input finiteness; defaults depend on nan_policy. |
bounds | 2-tuple of array_like or Bounds, optional | Lower and upper parameter bounds. |
method | {‘lm’, ‘trf’, ‘dogbox’}, optional | Optimization method: lm, trf, or dogbox. |
jac | callable or string, optional | Jacobian of the model with respect to parameters, or a finite-difference scheme name. |
full_output | bool, optional | Return additional optimization information. |
nan_policy | {None, ‘raise’, ‘omit’}, optional | How to handle NaN values: raise or omit. |
Returns popt, a one-dimensional array of optimized parameter values, and pcov, a two-dimensional approximate covariance matrix. With full_output=True, also returns infodict, errmsg, and ier.
import numpy as np
from scipy.optimize import curve_fit
x = np.arange(6.0)
y = 2.0 * x + 1.0
def model(x, slope, intercept):
return slope * x + intercept
popt, pcov = curve_fit(model, x, y)
print(popt)
print(np.sqrt(np.diag(pcov)))Fits a straight-line model and prints the optimized parameters and their approximate standard errors.
Estimate coefficients in a nonlinear physical, statistical, or engineering model from paired measurements.
Incorporate known observation uncertainty into the objective and covariance estimate.
Restrict fitted parameters to physically meaningful intervals.
curve_fit performs nonlinear least-squares fitting. It assumes the observations follow ydata = f(xdata, *params) + eps and returns parameters that minimize the squared residuals.
Follow-up: How are bounds handled? With bounds, the default method is trf; without bounds, it is lm.
f must be callable with the independent variable as its first argument and fitted parameters as separate remaining arguments. xdata contains measured independent-variable values, and ydata contains the corresponding dependent values.
Follow-up: What must change for a model with three unknowns? Add three parameter arguments after x in f and provide data compatible with the model output.
The function returns popt, the parameter values minimizing the residual sum of squares, and pcov, an approximate covariance matrix for popt. Standard deviations can be derived from the diagonal of pcov.
Follow-up: What does sigma affect? It weights residuals, and absolute_sigma controls whether its absolute scale is used when estimating covariance.
scipy.optimize.least_squaresscipy.optimize.leastsqscipy.optimize.OptimizeWarning