L iKddlZddlmZddlZddlmZmZmZmZm Z m Z m Z m Z m Z mZmZmZmZddlmZddlmZmZmZmZddlmZmZddlmZddlmcm Z!dd l"m#Z#m$Z$m%Z%dd l&m'Z'dd l(m)Z)dd l*m+Z+gd Z, d dZ- d!dZ. d"dZ/gdZ0gdZ1 d#dZ2dZ3dZ4dZ5dZ6ddddej ejfddfddddZ7d$dZ8dZ9dZ:dZ;d%dZc fdd_||||| | | d} t||fd|i| } j| _|r<| d}dDcic]}|| vr|| j|}}| d|d<||| d | d fS| d }| d }|dk(r t||d k(r | dS|d vr!t j |t d | dSt|cc}w)a Find the roots of a function. Return the roots of the (non-linear) equations defined by ``func(x) = 0`` given a starting estimate. Parameters ---------- func : callable ``f(x, *args)`` A function that takes at least one (possibly vector) argument, and returns a value of the same length. x0 : ndarray The starting estimate for the roots of ``func(x) = 0``. args : tuple, optional Any extra arguments to `func`. fprime : callable ``f(x, *args)``, optional A function to compute the Jacobian of `func` with derivatives across the rows. By default, the Jacobian will be estimated. full_output : bool, optional If True, return optional outputs. col_deriv : bool, optional Specify whether the Jacobian function computes derivatives down the columns (faster, because there is no transpose operation). xtol : float, optional The calculation will terminate if the relative error between two consecutive iterates is at most `xtol`. maxfev : int, optional The maximum number of calls to the function. If zero, then ``100*(N+1)`` is the maximum where N is the number of elements in `x0`. band : tuple, optional If set to a two-sequence containing the number of sub- and super-diagonals within the band of the Jacobi matrix, the Jacobi matrix is considered banded (only for ``fprime=None``). epsfcn : float, optional A suitable step length for the forward-difference approximation of the Jacobian (for ``fprime=None``). If `epsfcn` is less than the machine precision, it is assumed that the relative errors in the functions are of the order of the machine precision. factor : float, optional A parameter determining the initial step bound (``factor * || diag * x||``). Should be in the interval ``(0.1, 100)``. diag : sequence, optional N positive entries that serve as a scale factors for the variables. Returns ------- x : ndarray The solution (or the result of the last iteration for an unsuccessful call). infodict : dict A dictionary of optional outputs with the keys: ``nfev`` number of function calls ``njev`` number of Jacobian calls ``fvec`` function evaluated at the output ``fjac`` the orthogonal matrix, q, produced by the QR factorization of the final approximate Jacobian matrix, stored column wise ``r`` upper triangular matrix produced by QR factorization of the same matrix ``qtf`` the vector ``(transpose(q) * fvec)`` ier : int An integer flag. Set to 1 if a solution was found, otherwise refer to `mesg` for more information. mesg : str If no solution is found, `mesg` details the cause of failure. See Also -------- root : Interface to root finding algorithms for multivariate functions. See the ``method='hybr'`` in particular. Notes ----- ``fsolve`` is a wrapper around MINPACK's hybrd and hybrj algorithms. Examples -------- Find a solution to the system of equations: ``x0*cos(x1) = 4, x1*x0 - x1 = 5``. >>> import numpy as np >>> from scipy.optimize import fsolve >>> def func(x): ... return [x[0] * np.cos(x[1]) - 4, ... x[1] * x[0] - x[1] - 5] >>> root = fsolve(func, [1, 1]) >>> root array([6.50409711, 0.90841421]) >>> np.isclose(func(root), [0.0, 0.0]) # func(root) should be almost 0.0. array([ True, True]) c8xjdz c_|S)zc Wrapped `func` to track the number of times the function has been called. r)nfev)fargs _wrapped_funcfuncs r6r=zfsolve.._wrapped_funcs aU|r8r) col_derivxtolmaxfevbandepsfactordiagjacx)r;njevfjacrqtffunfvecstatusmessager)rP stacklevel)r; _root_hybrgetr)warningswarnRuntimeWarning)r>r.r/fprime full_outputr?r@rArBepsfcnrDrEoptionsr2rGkinforNr3r=s` @r6r r .s'VM% G ]B D& DG DC!!CH HAOQ#X3771: OO5zV $H s9~55X)n Q;C. q[  3x | # MM#~! <3xC. OsCc t| |} t|j}t|} t |t s|f}t dd|||| | f\}}| t|j} |}|=|d\}}n|dd\}}|dk(rd| dzz}tj|||d||||| | | }n>t dd |||| | | f|dk(rd | dzz}tj||||d|||| | }|d|d }}d d d|dd|dddddd}|d}|jd|d<t||dk(|d}|j| |||d<|S#t$r |d|d<Y|SwxYw)a~ Find the roots of a multivariate function using MINPACK's hybrd and hybrj routines (modified Powell method). Options ------- col_deriv : bool Specify whether the Jacobian function computes derivatives down the columns (faster, because there is no transpose operation). xtol : float The calculation will terminate if the relative error between two consecutive iterates is at most `xtol`. maxfev : int The maximum number of calls to the function. If zero, then ``100*(N+1)`` is the maximum where N is the number of elements in `x0`. band : tuple If set to a two-sequence containing the number of sub- and super-diagonals within the band of the Jacobi matrix, the Jacobi matrix is considered banded (only for ``jac=None``). eps : float A suitable step length for the forward-difference approximation of the Jacobian (for ``jac=None``). If `eps` is less than the machine precision, it is assumed that the relative errors in the functions are of the order of the machine precision. factor : float A parameter determining the initial step bound (``factor * || diag * x||``). Should be in the interval ``(0.1, 100)``. diag : sequence N positive entries that serve as a scale factors for the variables. r r>N)rbrPrrr[dz'Improper input parameters were entered.zThe solution converged.z5The number of calls to function has reached maxfev = r&xtol=fzO is too small, no further improvement in the approximate solution is possible.ztThe iteration is not making good progress, as measured by the improvement from the last five Jacobian evaluations.ziThe iteration is not making good progress, as measured by the improvement from the last ten iterations.zAn error occurred.)rrrPrQrRrSunknownrMrLhybr)rGsuccessrNmethodrOrh)rr flattenr' isinstancetupler7rrCr_hybrd_hybrjpoprupdateKeyError)r>r.r/rFr?r@rArBrCrDrEunknown_optionsr]nrrDfunmlmuretvalrGrNerrorsr`sols r6rVrVsL?+ F    B BA dE "wxr4QDILE5 ~u!! D | <FB"1XFB Q;AE]Fr4D&!#R? Hhb$Aq6B aKAE]FtRq!*D&&$Hq 6":vA:*%%+HA/a!??*$. /F !9D((6"DK 1v{F & (CJJt+I J + *I J+s EE)(E)rrPrQrR)rSFc t|j}t|} t|ts|f}t dd|||| \}}|d}| |kDrt d| d|| t|j} |-| dk(rd| dzz} tj|||||||| | | | }nV|rt dd |||| | |fnt dd |||| || f| dk(rd | dzz} tj|||||||||| | | }d t gd |d dgd|d dgd |d d|d dgd|d ddgd| dtgd|d dtgd|d dtgd|d dtgd }|d}|rd}|tvr|dd}t|} tt|ddd| ddf}t!j"d|f} ||\}}|dk7rt%d||j'||<||j(z}|d|f|ddz||d|fzS|t*vr#t-j.||dt0d !n|dk(r||d||d|d|fS#t$tf$rYtwxYw)"a Minimize the sum of squares of a set of equations. :: x = arg min(sum(func(y)**2,axis=0)) y Parameters ---------- func : callable Should take at least one (possibly length ``N`` vector) argument and returns ``M`` floating point numbers. It must not return NaNs or fitting might fail. ``M`` must be greater than or equal to ``N``. x0 : ndarray The starting estimate for the minimization. args : tuple, optional Any extra arguments to func are placed in this tuple. Dfun : callable, optional A function or method to compute the Jacobian of func with derivatives across the rows. If this is None, the Jacobian will be estimated. full_output : bool, optional If ``True``, return all optional outputs (not just `x` and `ier`). col_deriv : bool, optional If ``True``, specify that the Jacobian function computes derivatives down the columns (faster, because there is no transpose operation). ftol : float, optional Relative error desired in the sum of squares. xtol : float, optional Relative error desired in the approximate solution. gtol : float, optional Orthogonality desired between the function vector and the columns of the Jacobian. maxfev : int, optional The maximum number of calls to the function. If `Dfun` is provided, then the default `maxfev` is 100*(N+1) where N is the number of elements in x0, otherwise the default `maxfev` is 200*(N+1). epsfcn : float, optional A variable used in determining a suitable step length for the forward- difference approximation of the Jacobian (for Dfun=None). Normally the actual step length will be sqrt(epsfcn)*x If epsfcn is less than the machine precision, it is assumed that the relative errors are of the order of the machine precision. factor : float, optional A parameter determining the initial step bound (``factor * || diag * x||``). Should be in interval ``(0.1, 100)``. diag : sequence, optional N positive entries that serve as a scale factors for the variables. Returns ------- x : ndarray The solution (or the result of the last iteration for an unsuccessful call). cov_x : ndarray The inverse of the Hessian. `fjac` and `ipvt` are used to construct an estimate of the Hessian. A value of None indicates a singular matrix, which means the curvature in parameters `x` is numerically flat. To obtain the covariance matrix of the parameters `x`, `cov_x` must be multiplied by the variance of the residuals -- see curve_fit. Only returned if `full_output` is ``True``. infodict : dict a dictionary of optional outputs with the keys: ``nfev`` The number of function calls ``fvec`` The function evaluated at the output ``fjac`` A permutation of the R matrix of a QR factorization of the final approximate Jacobian matrix, stored column wise. Together with ipvt, the covariance of the estimate can be approximated. ``ipvt`` An integer array of length N which defines a permutation matrix, p, such that fjac*p = q*r, where r is upper triangular with diagonal elements of nonincreasing magnitude. Column j of p is column ipvt(j) of the identity matrix. ``qtf`` The vector (transpose(q) * fvec). Only returned if `full_output` is ``True``. mesg : str A string message giving information about the cause of failure. Only returned if `full_output` is ``True``. ier : int An integer flag. If it is equal to 1, 2, 3 or 4, the solution was found. Otherwise, the solution was not found. In either case, the optional output variable 'mesg' gives more information. See Also -------- least_squares : Newer interface to solve nonlinear least-squares problems with bounds on the variables. See ``method='lm'`` in particular. Notes ----- "leastsq" is a wrapper around MINPACK's lmdif and lmder algorithms. cov_x is a Jacobian approximation to the Hessian of the least squares objective function. This approximation assumes that the objective function is based on the difference between some observed target data (ydata) and a (non-linear) function of the parameters `f(xdata, params)` :: func(params) = ydata - f(xdata, params) so that the objective function is :: min sum((ydata - f(xdata, params))**2, axis=0) params The solution, `x`, is always a 1-D array, regardless of the shape of `x0`, or whether `x0` is a scalar. Examples -------- >>> from scipy.optimize import leastsq >>> def func(x): ... return 2*(x-3)**2+1 >>> leastsq(func, 0) (array([2.99999999]), 1) r!r>rz+Improper input: func input vector length N=z- must not exceed func output vector length M=NrcrrvrdzImproper input parameters.zRBoth actual and predicted relative reductions in the sum of squares are at most rgz?The relative error between two consecutive iterates is at most zG and the relative error between two consecutive iterates is at most zTThe cosine of the angle between func(x) and any column of the Jacobian is at most z in absolute valuez1Number of calls to function has reached maxfev = r&zftol=zH is too small, no further reduction in the sum of squares is possible.rfzP is too small, no further improvement in the approximate solution is possible.zgtol=z[ is too small, func(x) is orthogonal to the columns of the Jacobian to machine precision.) rrrPrQrRrSr}r~rreipvtrItrtriztrtri returned info rPrT)r rlr'rmrnr7r)rrCr_lmdif_lmder ValueErrorLEASTSQ_SUCCESSrrrget_lapack_funcsrcopyTLEASTSQ_FAILURErXrYrZ)r>r.r/rvr\r?ftolr@gtolrAr]rDrErurrmryrzr`cov_xpermrJinv_triuinvR trtri_infos r6r!r!$sD    B BA dE "wy&$D!DLE5 aA1uEaSICCD#GH H~u!! | Q;!a%[Fr4dD!%vvvtE   64T1q!f E  64T1q!f E Q;AE]FtR{!*D$f!'// :::>qCDHJ))-a237977;Ah?--1!H68<=::>qB##$(*$XQ()35$q"::$q"==$q"EEFPR)SF0 ":D ? "!9V$DD AYvay01"1"a%89A..w=H #+A; j?%(a H QY!&,q/&,q/2 2ay$ ,  s;AII0/I0c@fdd_d_d_S)Ncjr|Stjj|k(r jSjd_|}j!tj |_|_|S)NT) skip_lookupnpall last_paramslast_valr)paramsval_memoized_funcrgs r6rz-_lightweight_memoizer.._memoized_funcs  % %V9  66.,,6 7!** *  ' ' 3)-N &i  % % -)+N &&)N # r8F)rrr)rgrs`@r6_lightweight_memoizerrs(""&N"N!&N r8c fd}|Sjdk(sjdk(r fd}|Sfd}|S)Ncg|z SN)rr>xdataydatas r6 func_wrappedz _wrap_func..func_wrappeds''%/ /r8rc"g|z zSrrrr> transformrrs r6rz _wrap_func..func_wrappedsU 4V 4u <= =r8c4tg|z dSNTlower)rrs r6rz _wrap_func..func_wrapped)s"#ItE/CF/Ce/KSWX Xr8)sizendim)r>rrrrs```` r6 _wrap_funcrsH 0  1  ! 3 >  Y r8c^fd}|Sjdk(r fd}|Sfd}|S)Ncg|Srr)rrFrs r6 jac_wrappedz_wrap_jac..jac_wrapped0su&v& &r8rclddtjftjg|zSr)rnewaxisr rrFrrs r6rz_wrap_jac..jac_wrapped3s.Q ]+bjjU9LV9L.MM Mr8c Tttjg|dSr)rrr rs r6rz_wrap_jac..jac_wrapped6s+#I$&JJs5/B6/B$C*.0 0r8)r)rFrrrs``` r6 _wrap_jacr.s? '  1  N   0 r8ctj|}tj|}tj|}||z}d||||zz||<||z}||dz||<||z}||dz ||<|S)N?r)r ones_likeisfinite)lbubp0 lb_finite ub_finitemasks r6_initialize_feasibler=s b B BI BI y DbhD)*BtH  z !D$x!|BtH : !D$x!|BtH Ir8)r\ nan_policyc  |?t|} | j}t|dkr tdt|dz }n!t j |}|j }t|tr|j|j}}nt||\}}| t||}t j|tj kD|tjkz}||rd}nd}|dk(r |r td|| dnd }|rt j|t }nt j"|t }t|t$t&ztj(zr7|rt j|t }nt j"|t }|j d k(r td |s| | d k(r d }t|| dvr d}t|t+|| }t+|| }|s|r| dk(rt j,|}|jt't/|j0dz }|t j,|z}|d|f}||}|Nt j"|}|j0dk(r||}n#|j0dk(r||ddf}|dd|f}|t j"|}|j dk(s|j2|j fk(rd|z }nA|j2|j |j fk(r t5|d}n tdd}t9t;||||}t=| rt9t?| ||} n | |dk7rd} d| vr td|dk(r|j dk7r*||j kDrtAd|d|j tC||f| dd| }|\}}}}}t|d} t jD|ddz}!|dvrftGd|zd | vr| jId!d| d <tK||f| ||d"| }|jLstGd|jNztQ|jR|jT#}|jV}|jN}t|jT} d|jXz}!|jZ}t]|j^d $\}"}#}$t j`t jbte|j^j2z|#d z}%|#|#|%kD}#|$d|#j }$t jf|$jh|#dzz |$}d }&|#t j,|jr=tkt|t|ft %}|jmtd}&n@|s>| |j kDr|!| |j z z }'||'z}n|jmtd}&|&rtojpd&trd'| r|||||fS||fS#t6$r}td|d}~wwxYw)(aK4 Use non-linear least squares to fit a function, f, to data. Assumes ``ydata = f(xdata, *params) + eps``. Parameters ---------- f : callable The model function, f(x, ...). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments. xdata : array_like The independent variable where the data is measured. Should usually be an M-length sequence or an (k,M)-shaped array for functions with k predictors, and each element should be float convertible if it is an array like object. ydata : array_like The dependent data, a length M array - nominally ``f(xdata, ...)``. p0 : array_like, optional Initial guess for the parameters (length N). If None, then the initial values will all be 1 (if the number of parameters for the function can be determined using introspection, otherwise a ValueError is raised). sigma : None or scalar or M-length sequence or MxM array, optional Determines the uncertainty in `ydata`. If we define residuals as ``r = ydata - f(xdata, *popt)``, then the interpretation of `sigma` depends on its number of dimensions: - A scalar or 1-D `sigma` should contain values of standard deviations of errors in `ydata`. In this case, the optimized function is ``chisq = sum((r / sigma) ** 2)``. - A 2-D `sigma` should contain the covariance matrix of errors in `ydata`. In this case, the optimized function is ``chisq = r.T @ inv(sigma) @ r``. .. versionadded:: 0.19 None (default) is equivalent of 1-D `sigma` filled with ones. absolute_sigma : bool, optional If True, `sigma` is used in an absolute sense and the estimated parameter covariance `pcov` reflects these absolute values. If False (default), only the relative magnitudes of the `sigma` values matter. The returned parameter covariance matrix `pcov` is based on scaling `sigma` by a constant factor. This constant is set by demanding that the reduced `chisq` for the optimal parameters `popt` when using the *scaled* `sigma` equals unity. In other words, `sigma` is scaled to match the sample variance of the residuals after the fit. Default is False. Mathematically, ``pcov(absolute_sigma=False) = pcov(absolute_sigma=True) * chisq(popt)/(M-N)`` check_finite : bool, optional If True, check that the input arrays do not contain nans of infs, and raise a ValueError if they do. Setting this parameter to False may silently produce nonsensical results if the input arrays do contain nans. Default is True if `nan_policy` is not specified explicitly and False otherwise. bounds : 2-tuple of array_like or `Bounds`, optional Lower and upper bounds on parameters. Defaults to no bounds. There are two ways to specify the bounds: - Instance of `Bounds` class. - 2-tuple of array_like: Each element of the tuple must be either an array with the length equal to the number of parameters, or a scalar (in which case the bound is taken to be the same for all parameters). Use ``np.inf`` with an appropriate sign to disable bounds on all or some parameters. method : {'lm', 'trf', 'dogbox'}, optional Method to use for optimization. See `least_squares` for more details. Default is 'lm' for unconstrained problems and 'trf' if `bounds` are provided. The method 'lm' won't work when the number of observations is less than the number of variables, use 'trf' or 'dogbox' in this case. .. versionadded:: 0.17 jac : callable, string or None, optional Function with signature ``jac(x, ...)`` which computes the Jacobian matrix of the model function with respect to parameters as a dense array_like structure. It will be scaled according to provided `sigma`. If None (default), the Jacobian will be estimated numerically. String keywords for 'trf' and 'dogbox' methods can be used to select a finite difference scheme, see `least_squares`. .. versionadded:: 0.18 full_output : boolean, optional If True, this function returns additional information: `infodict`, `mesg`, and `ier`. .. versionadded:: 1.9 nan_policy : {'raise', 'omit', None}, optional Defines how to handle when input contains nan. The following options are available (default is None): * 'raise': throws an error * 'omit': performs the calculations ignoring nan values * None: no special handling of NaNs is performed (except what is done by check_finite); the behavior when NaNs are present is implementation-dependent and may change. Note that if this value is specified explicitly (not None), `check_finite` will be set as False. .. versionadded:: 1.11 **kwargs Keyword arguments passed to `leastsq` for ``method='lm'`` or `least_squares` otherwise. Returns ------- popt : array Optimal values for the parameters so that the sum of the squared residuals of ``f(xdata, *popt) - ydata`` is minimized. pcov : 2-D array The estimated approximate covariance of popt. The diagonals provide the variance of the parameter estimate. To compute one standard deviation errors on the parameters, use ``perr = np.sqrt(np.diag(pcov))``. Note that the relationship between `cov` and parameter error estimates is derived based on a linear approximation to the model function around the optimum [1]_. When this approximation becomes inaccurate, `cov` may not provide an accurate measure of uncertainty. How the `sigma` parameter affects the estimated covariance depends on `absolute_sigma` argument, as described above. If the Jacobian matrix at the solution doesn't have a full rank, then 'lm' method returns a matrix filled with ``np.inf``, on the other hand 'trf' and 'dogbox' methods use Moore-Penrose pseudoinverse to compute the covariance matrix. Covariance matrices with large condition numbers (e.g. computed with `numpy.linalg.cond`) may indicate that results are unreliable. infodict : dict (returned only if `full_output` is True) a dictionary of optional outputs with the keys: ``nfev`` The number of function calls. Methods 'trf' and 'dogbox' do not count function calls for numerical Jacobian approximation, as opposed to 'lm' method. ``fvec`` The residual values evaluated at the solution, for a 1-D `sigma` this is ``(f(x, *popt) - ydata)/sigma``. ``fjac`` A permutation of the R matrix of a QR factorization of the final approximate Jacobian matrix, stored column wise. Together with ipvt, the covariance of the estimate can be approximated. Method 'lm' only provides this information. ``ipvt`` An integer array of length N which defines a permutation matrix, p, such that fjac*p = q*r, where r is upper triangular with diagonal elements of nonincreasing magnitude. Column j of p is column ipvt(j) of the identity matrix. Method 'lm' only provides this information. ``qtf`` The vector (transpose(q) * fvec). Method 'lm' only provides this information. .. versionadded:: 1.9 mesg : str (returned only if `full_output` is True) A string message giving information about the solution. .. versionadded:: 1.9 ier : int (returned only if `full_output` is True) An integer flag. If it is equal to 1, 2, 3 or 4, the solution was found. Otherwise, the solution was not found. In either case, the optional output variable `mesg` gives more information. .. versionadded:: 1.9 Raises ------ ValueError if either `ydata` or `xdata` contain NaNs, or if incompatible options are used. RuntimeError if the least-squares minimization fails. OptimizeWarning if covariance of the parameters can not be estimated. See Also -------- least_squares : Minimize the sum of squares of nonlinear functions. scipy.stats.linregress : Calculate a linear least squares regression for two sets of measurements. Notes ----- Users should ensure that inputs `xdata`, `ydata`, and the output of `f` are ``float64``, or else the optimization may return incorrect results. With ``method='lm'``, the algorithm uses the Levenberg-Marquardt algorithm through `leastsq`. Note that this algorithm can only deal with unconstrained problems. Box constraints can be handled by methods 'trf' and 'dogbox'. Refer to the docstring of `least_squares` for more information. Parameters to be fitted must have similar scale. Differences of multiple orders of magnitude can lead to incorrect results. For the 'trf' and 'dogbox' methods, the `x_scale` keyword argument can be used to scale the parameters. `curve_fit` is for local optimization of parameters to minimize the sum of squares of residuals. For global optimization, other choices of objective function, and other advanced features, consider using SciPy's :ref:`tutorial_optimize_global` tools or the `LMFIT `_ package. References ---------- .. [1] K. Vugrin et al. Confidence region estimation techniques for nonlinear regression in groundwater flow: Three case studies. Water Resources Research, Vol. 43, W03423, :doi:`10.1029/2005WR004804` Examples -------- >>> import numpy as np >>> import matplotlib.pyplot as plt >>> from scipy.optimize import curve_fit >>> def func(x, a, b, c): ... return a * np.exp(-b * x) + c Define the data to be fit with some noise: >>> xdata = np.linspace(0, 4, 50) >>> y = func(xdata, 2.5, 1.3, 0.5) >>> rng = np.random.default_rng() >>> y_noise = 0.2 * rng.normal(size=xdata.size) >>> ydata = y + y_noise >>> plt.plot(xdata, ydata, 'b-', label='data') Fit for the parameters a, b, c of the function `func`: >>> popt, pcov = curve_fit(func, xdata, ydata) >>> popt array([2.56274217, 1.37268521, 0.47427475]) >>> plt.plot(xdata, func(xdata, *popt), 'r-', ... label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt)) Constrain the optimization to the region of ``0 <= a <= 3``, ``0 <= b <= 1`` and ``0 <= c <= 0.5``: >>> popt, pcov = curve_fit(func, xdata, ydata, bounds=(0, [3., 1., 0.5])) >>> popt array([2.43736712, 1. , 0.34463856]) >>> plt.plot(xdata, func(xdata, *popt), 'g--', ... label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt)) >>> plt.xlabel('x') >>> plt.ylabel('y') >>> plt.legend() >>> plt.show() For reliable results, the model `func` should not be overparametrized; redundant parameters can cause unreliable covariance matrices and, in some cases, poorer quality fits. As a quick check of whether the model may be overparameterized, calculate the condition number of the covariance matrix: >>> np.linalg.cond(pcov) 34.571092161547405 # may vary The value is small, so it does not raise much concern. If, however, we were to add a fourth parameter ``d`` to `func` with the same effect as ``a``: >>> def func2(x, a, b, c, d): ... return a * d * np.exp(-b * x) + c # a and d are redundant >>> popt, pcov = curve_fit(func2, xdata, ydata) >>> np.linalg.cond(pcov) 1.13250718925596e+32 # may vary Such a large value is cause for concern. The diagonal elements of the covariance matrix, which is related to uncertainty of the fit, gives more information: >>> np.diag(pcov) array([1.48814742e+29, 3.78596560e-02, 5.39253738e-03, 2.76417220e+28]) # may vary Note that the first and last terms are much larger than the other elements, suggesting that the optimal values of these parameters are ambiguous and that only one of these parameters is needed in the model. If the optimal parameters of `f` differ by multiple orders of magnitude, the resulting fit can be inaccurate. Sometimes, `curve_fit` can fail to find any results: >>> ydata = func(xdata, 500000, 0.01, 15) >>> try: ... popt, pcov = curve_fit(func, xdata, ydata, method = 'trf') ... except RuntimeError as e: ... print(e) Optimal parameters not found: The maximum number of function evaluations is exceeded. If parameter scale is roughly known beforehand, it can be defined in `x_scale` argument: >>> popt, pcov = curve_fit(func, xdata, ydata, method = 'trf', ... x_scale = [1000, 1, 1]) >>> popt array([5.00000000e+05, 1.00000000e-02, 1.49999999e+01]) NrPz-Unable to determine number of fit parameters.rtrflmzQMethod 'lm' only works for unconstrained problems. Use 'trf' or 'dogbox' instead.TFrz`ydata` must not be empty! propagatez;`nan_policy='propagate'` is not supported by this function.)raiseomitz1nan_policy must be one of {None, 'raise', 'omit'}raxis.g?rz"`sigma` must be positive definite.z`sigma` has incorrect shape.z2-pointr/z+'args' is not a supported keyword argument.zThe number of func parameters=z+ must not exceed the number of data points=)rvr\rMr|zOptimal parameters not found: max_nfevrA)rFboundsrk)r;rM) full_matrices)rz3Covariance of the parameters could not be estimated)categoryrU):_getfullargspecr/r'rrrrrmrrrrranyr asarray_chkfiniter*r listrnndarrayrisnanrangerrrrrrcallablerr)r!sum RuntimeErrorrqrrjrOdictr;rLrNcostrGrrFrrCmaxdotrr fillrXrYr)(rgrrrsigmaabsolute_sigma check_finiterrkrFr\rkwargssigr/rurrbounded_problemr3x_contains_nany_contains_nanhas_nanrer>r2poptpcovinfodicterrmsgierysizer_sVT thresholdwarn_covs_sqs( r6r#r#Nsp  za xx t9q=LM M IM ]]2  GG&&!FIIB*B z !"b )ffbBFF7lrBFF{;-B'CkDG rxx &G#x-(E7(OE  5)::?!7(OEZZ1_!7(A+.E!!gX+.E  5! ::?ekkejj]:e I[[UZZ4 4 N$U$7 ;< < AueY!G HD}#Ic5)$DE 4 FGG ~ ::?q5::~T8VS00Tz] N !EFAM Ns) Y Y( Y##Y(c \t|}t|}|j|f}t||g|}t|}|j|f}|} t||g|} | j||f} |dk(r t| } t |ft } t |ft } d} t j||||| | | | d| t|| g|} | j|f} t j||||| | | | d| tt| dd}|| fS)z=Perform a simple check on the gradient for correctness. rNrrPrr) rr'reshaperr r*r_chkderr r )fcnDfcnr.r/r?rGrurMrldfjacrIxperrfvecpgoods r6check_gradientr5s) 2A AA 1$A c!mdm $D D A << D F d1ntn %D <<A DA~ tU B e C E Q1dD&"eQD s2~~ &E MM1$ E Q1dD&"eQD c" +D #;r8c>|tj||z |z z Sr)rsquare)rp1ds r6_del2rTs  "r'"Q& &&r8c||z |z Srr)actualdesireds r6_relerrrXs W  ''r8c||}t|D]}||g|}|r8||g|} | d|zz |z} tj| dk7||| ft| } n|} tj|dk7| |ft| } t j t j| |kr| cS| }d|d } t| )Ng@r) fill_valuezFailed to converge after z iterations, value is ) rxpx apply_whererrrrabsr)r>r.r/r@maxiter use_accelrirp2rprelerrr3s r6_fixed_point_helperr\s B 7^  "_t_ b4BS2X "AQR UrJAAq1b'7qI 66"&&.4' (H   &gY.DQC HC s r8cLddd|}t|d}t||||||S)a' Find a fixed point of the function. Given a function of one or more variables and a starting point, find a fixed point of the function: i.e., where ``func(x0) == x0``. Parameters ---------- func : function Function to evaluate. x0 : array_like Fixed point of function. args : tuple, optional Extra arguments to `func`. xtol : float, optional Convergence tolerance, defaults to 1e-08. maxiter : int, optional Maximum number of iterations, defaults to 500. method : {"del2", "iteration"}, optional Method of finding the fixed-point, defaults to "del2", which uses Steffensen's Method with Aitken's ``Del^2`` convergence acceleration [1]_. The "iteration" method simply iterates the function until convergence is detected, without attempting to accelerate the convergence. References ---------- .. [1] Burden, Faires, "Numerical Analysis", 5th edition, pg. 80 Examples -------- >>> import numpy as np >>> from scipy import optimize >>> def func(x, c1, c2): ... return np.sqrt(c1/(x+c2)) >>> c1 = np.array([10,12.]) >>> c2 = np.array([3, 5.]) >>> optimize.fixed_point(func, [1.2, 1.3], args=(c1,c2)) array([ 1.4920333 , 1.37228132]) TF)del2 iteration) as_inexact)rr)r>r.r/r@r rkrs r6r"r"ns5TE26:I B4 0B tRtWi HHr8r) rNrrJP>rNNrdN) rNrrrNNrdN) rNFFrrgrNrdN)rr)rg:0yE>ir)=rXrnumpyrrrrrr r r r r rrrrscipyr scipy.linalgrrrrscipy._lib._utilrrrrscipy._lib.array_api_extra_libarray_api_extrar  _optimizerrr_lsqr_lsq.least_squaresrscipy.optimize._minimizer__all__r7r rVrrr!rrrrr#rrrrr"rr8r6r&s6666EE>F((NN.+ ;"0898<)-Qh'+GK $[|7<3=>BWt6* "#'d5"&&"&&(9$d',dN>'($,Ir8