Main Content

Linear Least Squares

Solve linear least-squares problems with bounds or linear constraints

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.

Linear least-squares solves min||C*x - d||2, possibly with bounds or linear 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 lsqlin or, for nonnegative least squares, you can also use lsqnonneg.


expand all

evaluateEvaluate optimization expression
infeasibilityConstraint violation at a point
optimproblemCreate optimization problem
optimvarCreate optimization variables
solveSolve optimization problem or equation problem
lsqlinSolve constrained linear least-squares problems
lsqnonnegSolve nonnegative linear least-squares problem
optim.coder.infboundInfinite bound support for code generation (Since R2022b)
mldivide, \Solve systems of linear equations Ax = B for x
optimwarmstartCreate warm start object (Since R2021a)

Live Editor Tasks

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


Problem-Based Linear Least Squares

Solver-Based Linear Least Squares

Code Generation

Problem-Based Algorithms

Algorithms and Options