如何求解R中的联立线性方程式?
联立方程中的数据可以读取为矩阵,然后我们可以求解这些矩阵以找到变量的值。例如,如果我们有三个等式-
x + y + z = 63x + 2y + 4z = 9
2x + 2y – 6z = 3
然后将这些方程式转换为矩阵,并使用R中的求解函数对其求解。
例1
> A<-matrix(c(1,1,2,3,2,4,2,3,-6),nrow=3,byrow=TRUE)> A
输出结果
[,1] [,2] [,3][1,] 1 1 2
[2,] 3 2 4
[3,] 2 3 -6
> b<-matrix(c(6,9,3))> b
输出结果
[,1][1,] 6
[2,] 9
[3,] 3
> solve(A,b)
输出结果
[,1][1,] -3.0
[2,] 6.0
[3,] 1.5
因此,答案是x = -3,y = 6和z = 1.5。
4x - 3y + x = -102x + y + 3z = 0
-1x + 2y - 5z = 17
例2
> A<-matrix(c(4,-3,1,2,1,3,-1,2,-5),nrow=3,byrow=TRUE)> A
输出结果
[,1] [,2] [,3][1,] 4 -3 1
[2,] 2 1 3
[3,] -1 2 -5
> b<-matrix(c(-10,0,17))> b
输出结果
[,1][1,] -10
[2,] 0
[3,] 17
> solve(A,b)
输出结果
[,1][1,] 1
[2,] 4
[3,] -2
例子3
4x – 2y + 3z = 1x + 3y – 4z = -7
3x + y + 2z = 5
> A<-matrix(c(4,-2,3,1,3,-4,3,1,2),nrow=3,byrow=TRUE)> A
输出结果
[,1] [,2] [,3][1,] 4 -2 3
[2,] 1 3 -4
[3,] 3 1 2
> b<-matrix(c(1,-7,5))> b
输出结果
[,1][1,] 1
[2,] -7
[3,] 5
> solve(A,b)
输出结果
[,1][1,] -1
[2,] 2
[3,] 3
例子4
x + 2y – 3z + 4t = 122x + 2y – 2z + 3t = 10
y + z = -1
x - y + z – 2t = -4
> A<-matrix(c(1,2,-3,4,2,2,-2,3,0,1,1,0,1,-1,1,-2),nrow=4,byrow=TRUE)> A
输出结果
[,1] [,2] [,3] [,4][1,] 1 2 -3 4
[2,] 2 2 -2 3
[3,] 0 1 1 0
[4,] 1 -1 1 -2
> b<-matrix(c(12,10,-1,-4))> b
输出结果
[,1][1,] 12
[2,] 10
[3,] -1
[4,] -4
> solve(A,b)
输出结果
[,1][1,] 1
[2,] 0
[3,] -1
[4,] 2
以上是 如何求解R中的联立线性方程式? 的全部内容, 来源链接: utcz.com/z/335361.html