Functions > Solving and Optimization > Solve Block Functions > Example: Using minerr for Nonlinear Least Squares Fitting
  
Example: Using minerr for Nonlinear Least Squares Fitting
The minerr function is similar to the find function, except that it returns an approximate solution for some cases for which find reports that no solution exists.
1. Define two vectors.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Define a fitting function (Weibull density with unknown parameters).
Click to copy this expression
3. Define initial guess values for the two parameters.
Click to copy this expression
Click to copy this expression
4. Use an equation to minimize inside a solve block.
Click to copy this expression
5. Add a solve block and use minerr to solve the problem. The minerr function uses the Levenberg-Marquardt method to minimize this problem. The Levenberg-Marquardt method does its own summing and squaring of the residuals.
Click to copy this expression
The parameters for best fit are the calculated values:
Click to copy this expression
The find function fails to find a solution to the above problem.
Click to copy this expression
6. Calculate the sum of squares implicitly minimized by this method.
Click to copy this expression
7. Plot the best Weibull fit versus the x-y data.
Click to copy this expression
Click to copy this expression
8. Evaluate the mean squared error. If the mean is zero, then a true solution exists:
Click to copy this expression
You can minimize directly by using the SSE equation and the minimize function.