Functions > Data Analysis > Curve Fitting > Example: Nonlinear Regression 2
  
Example: Nonlinear Regression 2
Use the LeastSquaresFit function to perform nonlinear regression. The LeastSquaresFit solver gives the most flexibility for solving nonlinear regression problems. It allows you to enter constraint equations for any of the dependent parameters, lower and upper bounds on parameters, standard deviations on the x values, as well as a confidence limit for the calculation.
LeastSquaresFit
1. Define a data set.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
This data is taken from an example discussed on the NIST website. The data was generated to 14-digits of accuracy using the following equation:
Click to copy this expression
2. Define a fitting function.
Click to copy this expression
The individual parameters above are elements of a vector β. You can also specify the input function with individual variable names, rather than elements of a vector:
Click to copy this expression
3. Provide guess values.
Click to copy this expression
4. Define the confidence limit on the parameters.
Click to copy this expression
5. Call the LeastSquaresFit function.
Click to copy this expression
The solver uses sequential quadratic programming (SQP) to solve the resulting least-squares problem. By introducing additional variables, the original problem is transformed into a general equality constrained nonlinear programming problem, which is, in general, faster and more stable than some other methods.
6. View the output vector returned by the LeastSquaresFit function.
Click to copy this expression
The first column of output contains the values for the fitted parameters. The second column contains the left and the third column the right boundary for the confidence interval on the parameters.
The 95% confidence limits on the parameters cover a fairly wide range, showing that the fit is difficult and the individual parameters can vary widely. As a result, the values of the fitted parameters do differ from the correct values recorded on the NIST website:
Click to copy this expressionClick to copy this expression
7. Plot the data, the Least Square fit and the NIST fit.
Click to copy this expression
Click to copy this expression
Click to copy this expression
8. Compare the Least Square fit to the original data:
Click to copy this expression
Click to copy this expression
The fit is approaching convergence, but could benefit from an adjustment in the convergence tolerance. That can be done through one of the optional arguments of the LeastSquaresFit function.
Constraints, Standard Deviation, and Tolerance
There are several optional arguments to LeastSquaresFit:
Standard deviation vector
Lower and upper bounds matrix
Accuracy
You can use any of the optional arguments alone, but if more than one argument is entered, the order of the arguments matters.
1. Perturb one of the data values, to simulate a mismeasurement.
Click to copy this expression
Click to copy this expression
2. Specify lower and upper bounds to constrain the fit values.
Click to copy this expression
The bounds are set well outside the expected parameter values, because no particular bounds are known in this case.
3. Set a vector of standard deviations for each y value to mask the outlier.
Click to copy this expression
Click to copy this expression
Click to copy this expression
The large value of 108 in the standard deviation effectively removes the mismeasurement from the calculation.
When a vector of standard deviation is entered as an argument to the LeastSquaresFit function, the solver minimizes the following function
Click to copy this expression
If the standard deviation is 0 for a point, then the original, undeviated function is used at that point, that is, StdYi is set to 1.
4. Set the accuracy to apply a more stringent convergence tolerance to the calculation (the default is 10-7).
Click to copy this expression
5. Call the LeastSquaresFit function with and without the standard deviation.
Click to copy this expression
Click to copy this expression
The unmasked calculation fails because the confidence limits are too large.
The new parameters are closer to the NIST values:
Click to copy this expression
Click to copy this expression
6. Plot the data with the outlier and the masked fit.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Reference
The data in this example were drawn from Lanczos, C., Applied Analysis, Prentice Hall, 1956, pages 272-280, as documented in the NIST Statistical Reference Dataset Archive, http://www.itl.nist.gov/div898/strd/