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.
 x 0.132 0.322 0.511 0.701 0.891 1.081 1.27 1.46 1.65 1.839 2.029 2.219 y 0.1 0.258 0.543 0.506 0.606 0.622 0.569 0.453 0.438 0.316 0.29 0.195 n length y 1 i 1 n
2. Define a fitting function (Weibull density with unknown parameters).
3. Define initial guess values for the two parameters.
4. Use an equation to minimize inside a solve block.
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.
The parameters for best fit are the calculated values:
The find function fails to find a solution to the above problem.
6. Calculate the sum of squares implicitly minimized by this method.
7. Plot the best Weibull fit versus the x-y data.
8. Evaluate the mean squared error. If the mean is zero, then a true solution exists:
You can minimize directly by using the SSE equation and the minimize function.