def leastsq(func, x0, args=(), Dfun=None, full_output=<NUM_LIT:0>,<EOL>col_deriv=<NUM_LIT:0>, ftol=<NUM_LIT>, xtol=<NUM_LIT>,<EOL>gtol=<NUM_LIT:0.0>, maxfev=<NUM_LIT:0>, epsfcn=None, factor=<NUM_LIT:100>, diag=None):
x0 = asarray(x0).flatten()<EOL>n = len(x0)<EOL>if not isinstance(args, tuple):<EOL><INDENT>args = (args,)<EOL><DEDENT>shape, dtype = _check_func('<STR_LIT>', '<STR_LIT>', func, x0, args, n)<EOL>m = shape[<NUM_LIT:0>]<EOL>if n > m:<EOL><INDENT>raise TypeError('<STR_LIT>' % (n, m))<EOL><DEDENT>if epsfcn is None:<EOL><INDENT>epsfcn = finfo(dtype).eps<EOL><DEDENT>if Dfun is None:<EOL><INDENT>if maxfev == <NUM_LIT:0>:<EOL><INDENT>maxfev = <NUM_LIT:200>*(n + <NUM_LIT:1>)<EOL><DEDENT>retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol,<EOL>gtol, maxfev, epsfcn, factor, diag)<EOL><DEDENT>else:<EOL><INDENT>if col_deriv:<EOL><INDENT>_check_func('<STR_LIT>', '<STR_LIT>', Dfun, x0, args, n, (n, m))<EOL><DEDENT>else:<EOL><INDENT>_check_func('<STR_LIT>', '<STR_LIT>', Dfun, x0, args, n, (m, n))<EOL><DEDENT>if maxfev == <NUM_LIT:0>:<EOL><INDENT>maxfev = <NUM_LIT:100> * (n + <NUM_LIT:1>)<EOL><DEDENT>retval = _minpack._lmder(func, Dfun, x0, args, full_output, col_deriv,<EOL>ftol, xtol, gtol, maxfev, factor, diag)<EOL><DEDENT>errors = {<NUM_LIT:0>: ["<STR_LIT>", TypeError],<EOL><NUM_LIT:1>: ["<STR_LIT>"<EOL>"<STR_LIT>" % ftol, None],<EOL><NUM_LIT:2>: ["<STR_LIT>"<EOL>"<STR_LIT>" % xtol, None],<EOL><NUM_LIT:3>: ["<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % (ftol, xtol), None],<EOL><NUM_LIT:4>: ["<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % gtol, None],<EOL><NUM_LIT:5>: ["<STR_LIT>"<EOL>"<STR_LIT>" % maxfev, ValueError],<EOL><NUM_LIT:6>: ["<STR_LIT>"<EOL>"<STR_LIT>""<STR_LIT>" % ftol,<EOL>ValueError],<EOL><NUM_LIT:7>: ["<STR_LIT>"<EOL>"<STR_LIT>" % xtol,<EOL>ValueError],<EOL><NUM_LIT:8>: ["<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % gtol, ValueError],<EOL>'<STR_LIT>': ["<STR_LIT>", TypeError]}<EOL>info = retval[-<NUM_LIT:1>]    <EOL>if info not in [<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:4>] and not full_output:<EOL><INDENT>if info in [<NUM_LIT:5>, <NUM_LIT:6>, <NUM_LIT:7>, <NUM_LIT:8>]:<EOL><INDENT>warnings.warn(errors[info][<NUM_LIT:0>], RuntimeWarning)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>raise errors[info][<NUM_LIT:1>](errors[info][<NUM_LIT:0>])<EOL><DEDENT>except KeyError:<EOL><INDENT>raise errors['<STR_LIT>'][<NUM_LIT:1>](errors['<STR_LIT>'][<NUM_LIT:0>])<EOL><DEDENT><DEDENT><DEDENT>mesg = errors[info][<NUM_LIT:0>]<EOL>if full_output:<EOL><INDENT>cov_x = None<EOL>if info in [<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:4>]:<EOL><INDENT>from numpy.dual import inv<EOL>from numpy.linalg import LinAlgError<EOL>perm = take(eye(n), retval[<NUM_LIT:1>]['<STR_LIT>'] - <NUM_LIT:1>, <NUM_LIT:0>)<EOL>r = triu(transpose(retval[<NUM_LIT:1>]['<STR_LIT>'])[:n, :])<EOL>R = dot(r, perm)<EOL>try:<EOL><INDENT>cov_x = inv(dot(transpose(R), R))<EOL><DEDENT>except (LinAlgError, ValueError):<EOL><INDENT>pass<EOL><DEDENT><DEDENT>return (retval[<NUM_LIT:0>], cov_x) + retval[<NUM_LIT:1>:-<NUM_LIT:1>] + (mesg, info)<EOL><DEDENT>else:<EOL><INDENT>return (retval[<NUM_LIT:0>], info)<EOL><DEDENT>
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.
x0 : ndarray
    The starting estimate for the minimization.
args : tuple
    Any extra arguments to func are placed in this tuple.
Dfun : callable
    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
    non-zero to return all optional outputs.
col_deriv : bool
    non-zero to specify that the Jacobian function computes derivatives
    down the columns (faster, because there is no transpose operation).
ftol : float
    Relative error desired in the sum of squares.
xtol : float
    Relative error desired in the approximate solution.
gtol : float
    Orthogonality desired between the function vector and the columns of
    the Jacobian.
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.
epsfcn : float
    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
    A parameter determining the initial step bound
    (``factor * || diag * x||``). Should be in interval ``(0.1, 100)``.
diag : sequence
    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
    Uses the fjac and ipvt optional outputs to construct an
    estimate of the jacobian around the solution. None if a
    singular matrix encountered (indicates very flat curvature in
    some direction).  This matrix must be multiplied by the
    residual variance to get the covariance of the
    parameter estimates -- see curve_fit.
infodict : dict
    a dictionary of optional outputs with the key s:

    ``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).

mesg : str
    A string message giving information about the cause of failure.
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.

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