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. See Nonlinear Least Squares (Curve Fitting).

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.


expand all

evaluateEvaluate optimization expression
infeasibilityConstraint violation at a point
optimproblemCreate optimization problem
optimvarCreate optimization variables
solveSolve optimization problem or equation problem
lsqcurvefitSolve nonlinear curve-fitting (data-fitting) problems in least-squares sense
lsqnonlinSolve nonlinear least-squares (nonlinear data-fitting) problems

Live Editor Tasks

OptimizeOptimize or solve equations in the Live Editor


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, Problem-Based

Fit parameters on an ODE using problem-based least squares.

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.