Main Content

# Nonlinear Least Squares (Curve Fitting)

Solve nonlinear least-squares (curve-fitting) problems in serial or parallel

Nonlinear least-squares is solving the problem min(∑||F(xi) - yi||2), where F(xi) is a nonlinear function and yi is data. The problem can have bounds, linear constraints, or nonlinear constraints. These problems come from fitting curves to experimental data, estimating parameters for physical models, and others.

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.

For the problem-based approach, create problem variables, and then represent the objective function and constraints in terms of these symbolic variables. To formulate a least-squares problem, follow the instructions in Write Objective Function for Problem-Based Least Squares. 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

expand all

 `evaluate` Evaluate optimization expression or objectives and constraints in problem `infeasibility` Constraint violation at a point `optimproblem` Create optimization problem `optimvar` Create optimization variables `solve` Solve optimization problem or equation problem
 `lsqcurvefit` Solve nonlinear curve-fitting (data-fitting) problems in least-squares sense `lsqnonlin` Solve nonlinear least-squares (nonlinear data-fitting) problems `checkGradients` Check first derivative function against finite-difference approximation (Since R2023b) `optim.coder.infbound` Infinite bound support for code generation (Since R2022b)

## Live Editor Tasks

 Optimize Optimize or solve equations in the Live Editor (Since R2020b)