Fitting a function to data with nonlinear least squares