Solving under/overdetermined systems in R

2019-08-02 05:37发布

问题:

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

Must I first determine if the system is determined/under/overdetermined?

If a system is determined, I just use

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

to get $x$ in $Ax = b$

If it overdetermined or underdetermined, I am not quite sure what to do. I think 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's no solution, will R just give an error?

Context: I am planning to recommend to my Stochastic Calculus professor that we use R in our upcoming exam (as opposed to tedious calculators/by-hand computation) so I have a feeling only simple functions will do (e.g. solve) for over/underdetermined systems rather than lengthy programs/functions.

Edit: I tried using solve(a,b), but I think that still doesn't give me all the solutions.

Here is an underdetermined example (R cannot give an answer since a is not 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:

The link I gave in section Matrix solution in the Wikipedia article on linear systems shows how to get what you want.
Define matrix A and vector b like this

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

The following code will give you the general solution to your underdetermined system

library(MASS)

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

You can check that this is correct with

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

where the vector w is any arbitrary vector.
You can simplify the solution further manually to what you gave; I leave that as an exercise for you. As far as I know you can't get that solution automatically but maybe package Ryacas can do it.

You can get what you want by using package MASS or package pracma. E.g. with MASS:

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

Then the solution is

xb + N * q

where q is an arbitrary scalar.

With pracma:

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

with the same expression as above for the solution.



回答2:

Try qr.solve(A,b). That should work for both under- and over-determined systems.