Question

In R I need to solve a system of linear equations (Ax=b), where b=0. By using solve() it just returns a zero vector for the answer, but I want the non-zero solutions of the system. Is there any way for it?

Was it helpful?

Solution

I think you are looking for the null space of a matrix A. Try :

library(MASS)
Null(t(A))

R > (A <- matrix(c(1,2,3,2,4,7), ncol = 3, byrow = T))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    4    7
R > Null(t(A))
              [,1]
[1,] -8.944272e-01
[2,]  4.472136e-01
[3,]  7.771561e-16
R > (A <- matrix(c(1,2,3,2,4,6), ncol = 3, byrow = T))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    4    6
R > Null(t(A))
           [,1]       [,2]
[1,] -0.5345225 -0.8017837
[2,]  0.7745419 -0.3381871
[3,] -0.3381871  0.4927193

Be careful. There are some rounding errors.

Also, denote r as the rank of matrix A, and q as the number of columns of A. If r = q, then zero vector is the only answer. If r > q, then there is no solution. If r < q, we can use the above Null function to get null space of A, but remember they are not unique, in terms of neither magnitude nor directions.

Reference : http://stat.ethz.ch/R-manual/R-patched/library/MASS/html/Null.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top