Main Content

# Nonlinear Least Squares (Curve Fitting)

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

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(xi) - yi||2), where F(xi) is a nonlinear function and yi 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

expand all

 `evaluate` Evaluate optimization expression `infeasibility` Constraint violation at a point `optimproblem` Create optimization problem `optimvar` Create optimization variables `solve` Solve optimization problem or equation problem
 `optim.coder.infbound` Infinite bound support for code generation `lsqcurvefit` Solve nonlinear curve-fitting (data-fitting) problems in least-squares sense `lsqnonlin` Solve nonlinear least-squares (nonlinear data-fitting) problems

## Live Editor Tasks

 Optimize Optimize or solve equations in the Live Editor