The least squares solution of a linear system: LSQ lsq

### 5.56.9 The least squares solution of a linear system: LSQ lsq

The lsq command finds the least squares solution to a matrix equation AX=b.
LSQ is a synonym for lsq.

- lsq takes two arguments:
  - A, a matrix.
  - b, a vector or matrix
- lsq(A,b) returns the least squares solution to the equation AX=b.

#### Examples

- Input:
  lsq([[1,2],[3,4]], [5,11])
  Output:

  ⎡ ⎢ ⎣ |
  1
  2
  | ⎤ ⎥ ⎦

- Input:
  lsq([[1,2], [3,4]], [[5,7], [11,9]])
  Output:

  ⎡ ⎢ ⎣ |
  1 | −5
  2 | 6
  | ⎤ ⎥ ⎦

- Note that:
  Input:
  linsolve([[1,2],[3,4],[3,6]]*[x, y] - [5,11,13],[x, y])
  Output:

  ⎡ ⎣ | ⎤ ⎦

  since the linear system has no solution. You can still find the least squares solution:
  Input:
  lsq([[1,2],[3,4],[3,6]],[5,11,13])
  Output:

  ⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
  11
  5
  11
  10
  | ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦

- The least squares solution:
  Input:
  lsq([[3,4]], [12])
  Output:

  ⎡ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎣ |
  36
  25
  48
  25
  | ⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦

  represents the point on the line 3x + 4y = 12 closest to the origin;
  Input:
  coordinates(projection(line(3*x+4*y=12),point(0)))
  (see Section 13.13.4, Section 13.15.8, Section 7.7.1 and Section 13.6.2)
  Output:

  ⎡ ⎢ ⎢ ⎣ |
  36
  25
  | , |
  48
  25
  | ⎤ ⎥ ⎥ ⎦