How to solve a non-square linear system with R : A X = B
?
(in the case the system has no solution or infinitely many solutions)
Example :
A=matrix(c(0,1,-2,3,5,-3,1,-2,5,-2,-1,1),3,4,T)
B=matrix(c(-17,28,11),3,1,T)
A
[,1] [,2] [,3] [,4]
[1,] 0 1 -2 3
[2,] 5 -3 1 -2
[3,] 5 -2 -1 1
B
[,1]
[1,] -17
[2,] 28
[3,] 11
If the matrix A has more rows than columns, then you should use least squares fit.
If the matrix A has fewer rows than columns, then you should perform singular value decomposition. Each algorithm does the best it can to give you a solution by using assumptions.
Here's a link that shows how to use SVD as a solver:
http://www.ecse.rpi.edu/~qji/CV/svd_review.pdf
Let's apply it to your problem and see if it works:
Your input matrix
A
and known RHS vectorB
:Let's decompose your
A
matrix:Here's the key: the third eigenvalue in
d
is very small; conversely, the diagonal element inadiag
is very large. Before solving, set it equal to zero:Now let's compute the solution (see slide 16 in the link I gave you above):
Now that we have a solution, let's substitute it back to see if it gives us the same
B
:That's the
B
side you started with, so I think we're good.Here's another nice SVD discussion from AMS:
http://www.ams.org/samplings/feature-column/fcarc-svd
Aim is to solve Ax = b
where A is p by q, x is q by 1 and b is p by 1 for x given A and b.
Multiplying both sides of the equation, we get
A'Ax = A' b
where A' is the transpose of A. Note that A'A is q by q matrix now. One way to solve this now multiply both sides of the equation by the inverse of A'A. Which gives,
x = (A'A)^{-1} A' b
This is the theory behind generalized inverse. Here G = (A'A)^{-1} A' is pseudo-inverse of A.
@duffymo used SVD to obtain a pseudoinverse of A.
However, last elements of
svd(A)$d
may not be quite as small as in this example. So, probably one shouldn't use that method as is. Here's an example where none of the last elements of A is close to zero.One option would be to look as the singular values in
cor(A)
Now, it is clear there is only two large singular values are present. So, one now can apply svd on A to get pseudo-inverse as below.