Previous Up Next

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.

Examples

To solve AX=b for A=[

12
34

] and b=[

5
11

], input:

lsq([[1,2],[3,4]],[5,11])
     
⎡
⎢
⎣
1
2
⎤
⎥
⎦
          

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

57
119

], input:

lsq([[1,2],[3,4]],[[5,7],[11,9]])
     
⎡
⎢
⎣
1−5
26
⎤
⎥
⎦
          

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


Previous Up Next