简体   繁体   English

解决R中的欠定/超定系统

[英]Solving under/overdetermined systems in R

What is the general procedure in solving systems of equations using R (as opposed to manual Gauss-Jordan/Gaussian elimination)? 使用R求解方程组的一般步骤是什么(与手动的Gauss-Jordan / Gaussian消除方法相反)?

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$ 得到$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? 我认为如果没有解决方案,R会给出错误吗?

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 (eg solve) for over/underdetermined systems rather than lengthy programs/functions . 内容:我打算建议我的随机微积分教授在即将到来的考试中使用R(而不是乏味的计算器/手工计算),因此我觉得只有简单的函数才能(例如求解)完成/过高的确定系统,而不是冗长的程序/功能

Edit: I tried using solve(a,b) , but I think that still doesn't give me all the solutions. 编辑:我尝试使用solve(a,b) ,但我认为仍然不能给我所有解决方案。

Here is an underdetermined example (R cannot give an answer since a is not square): 这是一个不确定的示例(R由于a不为正方形而无法给出答案):

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)

The link I gave in section Matrix solution in the Wikipedia article on linear systems shows how to get what you want. 我在Wikipedia上有关线性系统的文章中的Matrix solution部分中给出的链接显示了如何获得所需的内容。
Define matrix A and vector b like this 像这样定义矩阵A和向量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

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. 其中向量w是任意向量。
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. 据我所知,您不能自动获得该解决方案,但也许Ryacas包可以做到。

You can get what you want by using package MASS or package pracma . 您可以使用软件包MASS或软件包pracma Eg with MASS : 例如,使用MASS

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

Then the solution is 那么解决方案是

xb + N * q

where q is an arbitrary scalar. 其中q是任意标量。

With pracma : 随着pracma

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

with the same expression as above for the solution. 具有与上述解决方案相同的表达式。

Try qr.solve(A,b). 尝试qr.solve(A,b)。 That should work for both under- and over-determined systems. 这对于欠定和超定的系统都应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM