解决R中的欠定/超定系统 [英] Solving under/overdetermined systems in R

查看:224
本文介绍了解决R中的欠定/超定系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用R(相对于人工高斯-乔丹/高斯消元法)求解方程组的一般步骤是什么?

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

$Ax = b$

如果确定得过高或不确定,我不确定该怎么做.我认为以上有时会根据排名给出答案,但是解决方案并不总是唯一的.如何获得所有解决方案?我认为如果没有解决方案,R会给出错误吗?

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 (e.g. solve) for over/underdetermined systems rather than lengthy programs/functions.

我尝试使用solve(a,b),但是我认为仍然不能提供所有解决方案.

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

这是一个不确定的例子(由于a不是正方形,R无法给出答案):

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)

推荐答案

我在Wikipedia线性系统文章的Matrix solution部分给出的链接显示了如何获得所需的信息.
像这样定义矩阵A和向量b

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

您可以使用

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

其中向量w是任意向量.
您可以手动将解决方案简化为您提供的解决方案.我把它留给你练习.据我所知,您不能自动获得该解决方案,但包Ryacas可以做到.

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.

您可以通过使用软件包MASS或软件包pracma获得所需的内容. 例如.与MASS:

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

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

那么解决方法是

xb + N * q

其中q是任意标量.

使用pracma:

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

具有与上述解决方案相同的表达式.

with the same expression as above for the solution.

这篇关于解决R中的欠定/超定系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆