### 15.7.9 Least squares solution of a linear system

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

- 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

To solve AX=b for A=[

1 | 2
3 | 4

] and b=[

5
11

], input:

lsq([[1,2],[3,4]],[5,11])

⎡ ⎢ ⎣ |
1
2
| ⎤ ⎥ ⎦

To solve AX=B for A as above and B=[

5 | 7
11 | 9

], input:

lsq([[1,2],[3,4]],[[5,7],[11,9]])

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

Note that:

linsolve([[1,2],[3,4],[3,6]]*[x,y]-[5,11,13],[x, y])

| ⎡ ⎣ | ⎤ ⎦

since the linear system has no solution. You can still find the least squares solution:

lsq([[1,2],[3,4],[3,6]],[5,11,13])

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

The least squares solution:

lsq([[3,4]],[12])

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

represents the point on the line 3x+4y=12 closest to the origin. Indeed:

coordinates(projection(line(3*x+4*y=12),point(0)))

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

(See Section 26.12.4, Section 26.14.8, Section 19.3.1 and Section 26.5.2.)