# Linear Least Squares

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`

.

## Functions

## Live Editor Tasks

Optimize | Optimize or solve equations in the Live Editor |

## Topics

### Problem-Based Linear Least Squares

**Shortest Distance to a Plane**

Shows how to solve a linear least-squares problem using the problem-based approach.**Nonnegative Linear Least Squares, Problem-Based**

Shows how to solve a nonnegative linear least-squares problem using the problem-based approach and several solvers.**Large-Scale Constrained Linear Least-Squares, Problem-Based**

Solves an optical deblurring problem using the problem-based approach.**Write Objective Function for Problem-Based Least Squares**

Syntax rules for problem-based least squares.

### Solver-Based Linear Least Squares

**Optimize Live Editor Task with lsqlin Solver**

Example showing the Optimize Live Editor task and linear least squares.**Nonnegative Linear Least Squares, Solver-Based**

This example shows how to use several algorithms to solve a linear least-squares problem with the bound constraint that the solution is nonnegative.**Jacobian Multiply Function with Linear Least Squares**

Example showing how to save memory in a large structured linear least-squares problem.**Warm Start Best Practices**

Describes how best to use warm start for speeding repeated solutions.**Large-Scale Constrained Linear Least-Squares, Solver-Based**

Solves an optical deblurring problem using the solver-based approach.

### Code Generation

**Code Generation in Linear Least Squares: Background**

Prerequisites to generate C code for linear least squares.**Generate Code for lsqlin**

Example of code generation for linear least squares.**Optimization Code Generation for Real-Time Applications**

Explore techniques for handling real-time requirements in generated code.

### Problem-Based Algorithms

**Write Objective Function for Problem-Based Least Squares**

Syntax rules for problem-based least squares.**Problem-Based Optimization Algorithms**

Learn how the optimization functions and objects solve optimization problems.**Supported Operations for Optimization Variables and Expressions**

Explore the supported mathematical and indexing operations for optimization variables and expressions.

### Algorithms and Options

**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.