Logo

A physics toolbox for laser spectroscopy.

Static Badge Static Badge Static Badge

Static Badge Static Badge

© 2025 Patrick Müller. All rights reserved.

qspec.analyze.curve_fit  (  f x y p0 = None p0_fixed = None sigma = None absolute_sigma = False check_finite = True bounds = (-inf, inf) method = None jac = None full_output = False report = False ** kwargs  )[source]

Use non-linear least squares to fit a function, $f$, to data. Assumes ydata = f(xdata, *params) + eps. This is a reimplementation of scipy.optimize.curve_fit with the additional features of fixing parameters with p0_fixed and the option to use the parameter sigma dynamically as a function g(x, y, f(x, *params), *params) -> sigma.

Parameters:
fCallable

The model function $f$ to fit to the data.

xarray_like | object

The $x$ data.

yarray_like

The $y$ data.

p0ndarray | Iterable

A numpy array or an Iterable of the initial guesses for the parameters. Must have at least the same length as the minimum number of parameters required by the function f. If p0 is None, 1 is taken as an initial guess for all non-keyword parameters.

p0_fixedndarray | Iterable

A numpy array or an Iterable of bool values specifying whether to fix a parameter. Must have the same length as p0.

sigmandarray | Iterable | Callable

The $1\sigma$ uncertainty of the y data. This can also be a function $g$ such that g(x, y, f(x, *params), *params) -> sigma.

absolute_sigmabool

Assumes ydata = f(xdata, *params) + eps

check_finitebool

See scipy.optimize.curve_fit.

bounds(ndarray, ndarray)

See scipy.optimize.curve_fit.

methodstr

See scipy.optimize.curve_fit.

jacCallable | str

See scipy.optimize.curve_fit. Must not be callable if sigma is callable.

full_outputbool

See scipy.optimize.curve_fit.

reportbool

Whether to print the result of the fit.

kwargsNone

See scipy.optimize.curve_fit.

Returns:
(popt, pcov)(ndarray, ndarray, Optional[dict], Optional[str], Optional[int])

The optimal parameters and their covariance matrix. Additional output if full_output == True. See scipy.optimize.curve_fit.

Raises:
(ValueError, RuntimeError, OptimizeWarning)

If either x or y contain NaNs, or if incompatible options are used. If the least-squares minimization fails. If covariance of the parameters can not be estimated.

API navigation