Solution in / redefined systems in R

What is the general procedure for solving systems of equations using R (as opposed to the manual exclusion of Gauss-Jordan / Gauss)?

Should I first determine if the system is defined / sub / redefined?

If a system is defined, I just use

solve(t(a)%*%a)%*%t(a)%*%b

to get $x$in$Ax = b$

If it is overridden or underdetermined, I'm not quite sure what to do. I think that the above sometimes gives an answer depending on the rank, but the solution is not always unique. How can I get all the solutions? I think if there is no solution, R will just give an error?

Context: I plan to recommend to my professor of stochastic calculus that we use R in our upcoming exam (as opposed to tedious calculators / calculations in manual mode), so I feel that only simple functions will be performed (e.g. solve) for over / underdetermined systems, not long programs / functions .

Edit: I tried to use solve(a,b), but I think it still does not give me all the solutions.

Here is an underdetermined example (R cannot give an answer since a is not a square):

a=matrix(c(1,1,1,3,2,1),byrow=T,nrow=2)
a
b=matrix(c(1,2),byrow=T,nrow=2)
b
solve(a,b)
+1
source share
2 answers

, Matrix solution , , , .
A vector b

A <- matrix(c(1,1,1,3,2,1),byrow=T,nrow=2)
A
b <- matrix(c(1,2),byrow=T,nrow=2)
b

library(MASS)

Ag <- ginv(A)
Ag
xb <- Ag %*% b
xb
Aw <- diag(nrow=nrow(Ag)) - Ag %*% A
Aw

,

w <- runif(3)
z <- xb + Aw %*% w
A %*% z - b

w - . , ; . , , Ryacas .

, , MASS pracma. . MASS:

library(MASS)
N <- Null(t(A))

xb + N * q

q - .

pracma:

N <- null(A)  # or nullspace(A)

, .

+4

qr.solve(A, b). , .

+2

Source: https://habr.com/ru/post/1661747/


All Articles