Solve A linear equation system b=0 Rstudio

514 Views Asked by At

i would solve a linear equation system like this:

x_1*3+x_2*4+x_3*5+x_4*6+x_6*2=0
x_1*21+x_2*23+x_3*45+x_4*37*+x_6*0=0
x_1*340+x_2*24+x_3*25+x_4*31+x_6*0=0
x_1*32+x_2*45+x_3*5+x_4*6+x_7*2=0
x_1*9+x_2*11+x_3*13+x_4*49+x_7*0=0
x_1*5+x_2*88+x_3*100+x_4*102+X_7*2=0



     [x_1][x_2][x_3] [x_4]  [,5]
[1,]   3    4     5     6     2
[2,]  21   23    45    37     0
[3,]  340  24    25    31     0
[4,]  32   45     5     6     2
[5,]    9  11    13    49     0
[6,]   5   88   100   102     2

i use solve this linear homogeneous equation system with MASS::null(t(M), but the problem is that find x_1....x_4, but x_5 find only one solution but i need different three value that is x_5,1,x_5,2 and x_5,3. value of matrix are random, and they can be changed

2

There are 2 best solutions below

0
On

With the update which shows you've got 5 equations with 7 unknowns, it's obvious that there is a multidimensional surface of solutions.
I fear I don't have code to calculate that surface, but let me toot my own horn and offer the ktsolve package. For any given set of inputs from your { $x_1, x_2, ...x_7$ } [ah rats no latex markdown] , enter a collection of known values and ktsolve will run a back-solver (usually BB ) to find the unknowns.
So if you can feed your problem a selected set of any two of {X_5, X_6, X_7}, you can find all five of the other values.

0
On

Ok, had to reactivate my rusted linear algebra knowledge, you can do this by using the Singular Value Decomposition, if all elements of the diagonal part of the SVD are non zero, only the trivial solutions exists:

solution_space <- function(A){
  my_svd <- svd(A)
  if(all(my_svd$d != 0)){
    return(rep(0, ncol(A)))
  } else {
    return(my_svd$u[,my_svd$d == 0, drop=F])
  }
}

A %*% solution_space(A)

You can try the code with these matrices:

A <- matrix(c(1,1,0,1,1,0,0,0,1), 3, 3)
A <- matrix(c(1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1), 4, 4)
A <- matrix(c(1,1,0,1,1,0,0,0,0), 3, 3)