我需要在 R 中求解一个线性方程组——我已经能够做得很好。请看下面的代码:
A<-matrix(c(1:5,2,1,2:4,3,2,1:3,4:2,1,2,5:1),nrow=5) #Creates a matrix of the coefficients
A #Displays the matrix of coefficients (below)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 2 1 2 3 4
[3,] 3 2 1 2 3
[4,] 4 3 2 1 2
[5,] 5 4 3 2 1
kv<-matrix(c(7,-1,-3,5,17),nrow=5) #Creates a column vector of the known values
kv #Displays the column vector
[,1]
[1,] 7
[2,] -1
[3,] -3
[4,] 5
[5,] 17
solve(A,kv) #Solves the continuous equation
[,1]
[1,] -2
[2,] 3
[3,] 5
[4,] 2
[5,] -4
问题是我现在需要概括我的解决方案,它可以用于具有相同结构但更大尺寸的方程组 - 无需像上面那样键入矩阵 A 的所有值。
有没有人能指出我如何做系数矩阵的正确方向,但该程序可以用来解决其他系统?
任何帮助将不胜感激谢谢