Equana

Back to Linear Algebra

lstsq

Least-squares solution to linear equation

Syntax

x = lstsq(A, b)
[x, residuals, rank, s] = lstsq(A, b)

Description

Computes the least-squares solution to the equation A*x = b. Returns the solution that minimizes ||A*x - b||. With one output, returns the solution vector x. With four outputs, also returns residuals, rank, and singular values.

Parameters

NameDescription
ACoefficient matrix (m×n)
bOrdinate values (m×1) or (m×k)

Returns

Solution vector x, or [x, residuals, rank, s]

Examples

Try It
>> A = [1 0; 1 1; 1 2]; b = [1; 2; 3];
x = lstsq(A, b)
Try It
>> [x, res, rnk, s] = lstsq(A, b)

See Also