Nonlinear Least Squares (Curve Fitting)
Before you begin to solve an optimization problem, you must choose the appropriate approach: problem-based or solver-based. For details, see First Choose Problem-Based or Solver-Based Approach.
Nonlinear least-squares solves min(∑||F(x_{i}) - y_{i}||^{2}), where F(x_{i}) is a nonlinear function and y_{i} is data. The problem can have bounds, linear constraints, or nonlinear constraints.
For the problem-based approach, create problem variables, and then
represent the objective function and constraints in terms of these
symbolic variables. For the problem-based steps to take, see Problem-Based Optimization Workflow. To
solve the resulting problem, use solve
.
For the solver-based steps to take, including defining the objective
function and constraints, and choosing the appropriate solver, see Solver-Based Optimization Problem Setup. To solve
the resulting problem, use lsqcurvefit
or lsqnonlin
.
Functions
Live Editor Tasks
Optimize | Optimize or solve equations in the Live Editor |
Topics
Problem-Based Nonlinear Least Squares
- Nonlinear Least-Squares, Problem-Based
Basic example of nonlinear least squares using the problem-based approach. - Nonlinear Data-Fitting Using Several Problem-Based Approaches
Solve a least-squares fitting problem using different solvers and different approaches to linear parameters. - Fit ODE Parameters Using Optimization Variables
Fit parameters of an ODE using problem-based least squares. - Compare lsqnonlin and fmincon for Constrained Nonlinear Least Squares
Compare the performance oflsqnonlin
andfmincon
on a nonlinear least-squares problem with nonlinear constraints. - Write Objective Function for Problem-Based Least Squares
Syntax rules for problem-based least squares.
Solver-Based Nonlinear Least Squares
- Nonlinear Data-Fitting
Basic example showing several ways to solve a data-fitting problem. - Banana Function Minimization
Shows how to solve for the minimum of Rosenbrock's function using different solvers, with or without gradients. - lsqnonlin with a Simulink Model
Example of fitting a simulated model. - Nonlinear Least Squares Without and Including Jacobian
Example showing the use of analytic derivatives in nonlinear least squares. - Nonlinear Curve Fitting with lsqcurvefit
Example showing how to do nonlinear data-fitting with lsqcurvefit. - Fit an Ordinary Differential Equation (ODE)
Example showing how to fit parameters of an ODE to data, or fit parameters of a curve to the solution of an ODE. - Fit a Model to Complex-Valued Data
Example showing how to solve a nonlinear least-squares problem that has complex-valued data.
Code Generation
- Code Generation in Nonlinear Least Squares: Background
Prerequisites to generate C code for nonlinear least squares. - Generate Code for lsqcurvefit or lsqnonlin
Example of code generation for nonlinear least squares. - Optimization Code Generation for Real-Time Applications
Explore techniques for handling real-time requirements in generated code.
Parallel Computing
- What Is Parallel Computing in Optimization Toolbox?
Use multiple processors for optimization. - Using Parallel Computing in Optimization Toolbox
Perform gradient estimation in parallel. - Improving Performance with Parallel Computing
Investigate factors for speeding optimizations.
Algorithms and Options
- Write Objective Function for Problem-Based Least Squares
Syntax rules for problem-based least squares. - Least-Squares (Model Fitting) Algorithms
Minimizing a sum of squares in n dimensions with only bound or linear constraints. - Optimization Options Reference
Explore optimization options.