When working with an optimization problem, like fitting a function to a given dataset, one often needs to find a global minimum instead of a local one (see: this MATLAB page). This can be done using the Global Optimization Toolbox. However, by vectorizing the optimization problem, the global minimum can often be calculated a lot faster, because parameter combinations can be calculated simultaneously..
f = global_fit(x, y, fun, ranges, nGrid) does fit x and y to fun, trying out nGrid values for every free parameter in fun within the defined ranges. It returns the fit as a function of x.
This function creates a grid of parameter combinations and calculates all of the combinations simultaneously and vectorized, which results in good computational speeds. After the mse between the given model and data is calculated for the grid, the result is polished using fminsearch, to find the local minimum at the optimal grid position.