 
 
 
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=[
]
and b=[
], input:
| lsq([[1,2],[3,4]],[5,11]) | 
To solve AX=B for A as above and B=[
], input:
| lsq([[1,2],[3,4]],[[5,7],[11,9]]) | 
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]) | 
|  | | |  | | ⎡ ⎢
 ⎢
 ⎢
 ⎢
 ⎢
 ⎢
 ⎢
 ⎣
 |  | ⎤ ⎥
 ⎥
 ⎥
 ⎥
 ⎥
 ⎥
 ⎥
 ⎦
 | 
 | 
 |  |  |  |  |  |  |  |  |  |  | 
 | 
The least squares solution:
|  | | |  | | ⎡ ⎢
 ⎢
 ⎢
 ⎢
 ⎢
 ⎢
 ⎢
 ⎣
 |  | ⎤ ⎥
 ⎥
 ⎥
 ⎥
 ⎥
 ⎥
 ⎥
 ⎦
 | 
 | 
 |  |  |  |  |  |  |  |  |  |  | 
 | 
represents the point on the line 3x+4y=12 closest to the origin. Indeed:
| coordinates(projection(line(3*x+4*y=12),point(0))) | 
(See Section 26.12.4, Section 26.14.8,
Section 19.3.1 and Section 26.5.2.)
 
 
