def residuals(params,f_model,x_values,y_values):
    """Residual function for nonlinear fit.

    Calculates residuals d_i=y_i-f(x_i), as required for
    scipy.optimize.leastsq.  Typical signature for call will be

        fit = scipy.optimize.leastsq(residuals, c_guess, args=(f_model,x_values,y_values))

    where c_guess are the initial guesses for the parameters.

    The model function must "broadcast" over a vector of x arguments
    but need not be a full-fledged NumPy ufunc.

    Args:
        f_model (callable): model function f_model(x_values,params)
        params (NumPy vector): parameters [c0,c1]
        x_values (NumPy vector): independent variable values [x0,...,x_(M-1)]
        y_values (NumPy vector): dependent variable values [x0,...,x_(M-1)]

     Returns:
        (NumPy vector): residuals [d0,...,d_(M-1)]

    """

    d_values = y_values - f_model(x_values,params)
    return d_values