如何求解R中的矩阵方程

时间:2017-12-19 10:42:58

标签: r matrix vector equation equation-solving

我的朋友和我(两位非R专家)都试图在R中求解矩阵方程。我们有矩阵y,其定义如下:

 y=matrix(c(0.003,0.977,0,0,0,0,0,0,0,0,0,0.02,0,0.0117,0.957,0,0,0,0,0,0,0,0,0.03,0,0,0.0067,0.917,0,0,0,0,0,0,0,0.055,0,0,0,0.045,0.901,0,0,0,0,0,0,0.063,0,0,0,0,0.0533,0.913,0,0,0,0,0,0.035,0,0,0,0,0,0.05,0,0,0,0,0,0.922,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.01,0,0,0,0,0,0,0,0,0,0,0,0,0.023,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
nrow=12, ncol=12, byrow=TRUE)

这个矩阵模拟了我们学校的学生传递到下一年的方式。通过将此矩阵与包含每年学生数量的向量相乘,我们将获得一年后每年的学生数量。

使用功能:

sumfun<-function(x,start,end){
 return(sum(x[start:end]))

我们将每年的学生数量相加,以获得我们学校的学生总数。我们希望用当前在学校的学生数量填充向量(我们用数组乘以矩阵),并将新学生的数量(向量的第一个数字)作为变量X

例如:

sumfun(colSums(y*c(x,200,178,180,201,172,0,0,200,194,0,0)),2,6)

我们希望将这个等式等同于1000,这是我们学校建筑可容纳的最大学生数量。通过这样做,我们可以计算出我们学校可以接受多少新生。我们不知道该怎么做。我们预制X是100到300之间的东西。如果有人可以帮助我们,我们将非常感激!

2 个答案:

答案 0 :(得分:0)

我建议探测各种x值并查看结果答案。从那里,你可以看到趋势,并用它来找出答案。这是一个例子:

# Sample data
y=matrix(c(0.003,0.977,0,0,0,0,0,0,0,0,0,0.02,0,0.0117,0.957,0,0,0,0,0,0,0,0,0.03,0,0,0.0067,0.917,0,0,0,0,0,0,0,0.055,0,0,0,0.045,0.901,0,0,0,0,0,0,0.063,0,0,0,0,0.0533,0.913,0,0,0,0,0,0.035,0,0,0,0,0,0.05,0,0,0,0,0,0.922,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.01,0,0,0,0,0,0,0,0,0,0,0,0,0.023,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
         nrow=12, ncol=12, byrow=TRUE)

# funciton f will return a total number of students in the school for a given 'x'
f <- function(x) {
  z <- c(x,200,178,180,201,172,0,0,200,194,0,0)
  sum(t(y[,2:6]) %*% z)
}

# Let's see the plot
px <- 1:1000
py <- sapply(px,f) # will calculate the total number of students for each x from 1 to 1000
plot(px,py,type='l',lty=2)

# Analyze the matrices (the analysis is not shown here) and reproduce the linear trend
lines(px,f(0)+sum(y[1,2:6])*px,col='red',lty=4)

# obtain the answer using the linear trend
Xstudents <- (1000-f(0))/sum(y[1,2:6])
floor(Xstudents)

答案 1 :(得分:0)

我不熟悉R,但我可以指导解决这个矩阵方程的主要过程。假设你的矩阵被称为P

Matrix

让当前的学生向量被称为s0

s0 = {x, 200, 178, 180, 201, 172, 0, 0, 200, 194, 0, 0};

请注意,我们要保留x未定义,因为我们稍后要为此变量求解。请注意,即使x未知,我们仍然可以将s0P相乘。我们称之为新的向量s1

s1 = s0.P = {0.003*x, 2.34 + 0.977*x, 192.593, 173.326, 177.355, 192.113, 0, 0, 0, 0, 0, 192.749 + 0.02*x}

我们可以在学生2 - 6年确认这是正确的,只有第2年受新生数量(x)的影响。因此,如果现在总结2-6岁,就像你的例子一样,我们发现总和是:

s1[2:6] = 737.727 + 0.977*x

剩下的就是解决s1[2:6] == 1000

这个微不足道的等式
s1[2:6] == 1000
737.727 + 0.977*x == 1000
x = 268.447

如果这是正确的,请告诉我!这一切都在Mathematica中完成。

以下代码显示了如何在R中执行此操作:

y=matrix(c(0.003,0.977,0,0,0,0,0,0,0,0,0,0.02,0,0.0117,0.957,0,0,0,0,0,0,0,0,0.03,0,0,0.0067,0.917,0,0,0,0,0,0,0,0.055,0,0,0,0.045,0.901,0,0,0,0,0,0,0.063,0,0,0,0,0.0533,0.913,0,0,0,0,0,0.035,0,0,0,0,0,0.05,0,0,0,0,0,0.922,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.01,0,0,0,0,0,0,0,0,0,0,0,0,0.023,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
nrow=12, ncol=12, byrow=TRUE)


sumfun<-function(x,start,end){
  return(sum(x[start:end]))
}

students <- function(x) {
  students = sumfun(colSums(y*c(x,200,178,180,201,172,0,0,200,194,0,0)),2,6);
  return(students - 1000);
}

uniroot(students, lower=100, upper=300)$root;

只要函数为uniroot,函数0就会找到。因此,如果您定义一个函数,该函数返回值为x的学生数量并减去1000,则会找到学生人数为1000的x

注意:这仅描述了学生总数的短期行为。要使长期其他方程中的学生人数达到1000,必须解决。