S 5 連立一次方程式
5.1 例題5.1
## [,1]
## [1,] 1
## [2,] 3
## [3,] -2
c(det(cbind(b,A[,c(2,3)]))/det(A),
det(cbind(A[,1],b,A[,3]))/det(A),
det(cbind(A[,c(1,2)],b))/det(A)
)
## [1] 1 3 -2
5.2 問題5.1
## [,1]
## [1,] 5
## [2,] -4
## [1] 5 -4
## [,1]
## [1,] 3
## [2,] -2
## [3,] 1
c(det(cbind(b,A[,c(2,3)]))/det(A),
det(cbind(A[,1],b,A[,3]))/det(A),
det(cbind(A[,c(1,2)],b))/det(A)
)
## [1] 3 -2 1
5.3 問題5.2
## [1] 6
## [1] 1.953993e-14
## [,1] [,2] [,3]
## [1,] 4 -2 5
## [2,] 3 5 -6
## [3,] 5 3 -2
## [1] 2 0 1
5.4 例題5.3
掃き出し法をリテラルにやってみる.
Ab <- matrix(c(2,1,5,1,2,4,4,-2,-4,7,0,9),3,4)
Ab[1,] <- Ab[1,]/Ab[1,1]
Ab[2,] <- Ab[2,]-Ab[1,]*Ab[2,1]
Ab[3,] <- Ab[3,]-Ab[1,]*Ab[3,1]
Ab[2,] <- Ab[2,]/Ab[2,2]
Ab[1,] <- Ab[1,]-Ab[2,]*Ab[1,2]
Ab[3,] <- Ab[3,]-Ab[2,]*Ab[3,2]
Ab[3,] <- Ab[3,]/Ab[3,3]
Ab[1,] <- Ab[1,]-Ab[3,]*Ab[1,3]
Ab[2,] <- Ab[2,]-Ab[3,]*Ab[2,3]
Ab
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 3.0
## [2,] 0 1 0 -1.0
## [3,] 0 0 1 0.5
関数としてパッケージ化.
hakidashi <- function(Ab) {
d <- dim(Ab)[1]
for (i in 1:d) {
Ab[i,] <- Ab[i,]/Ab[i,i]
for (j in setdiff(1:d,i)) {
Ab[j,] <- Ab[j,]-Ab[i,]*Ab[j,i]
}
}
round(Ab, digits = 5)
}
Ab <- matrix(c(2,1,5,1,2,4,4,-2,-4,7,0,9),3,4)
hakidashi(Ab)
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 3.0
## [2,] 0 1 0 -1.0
## [3,] 0 0 1 0.5
5.5 問題5.3
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 1
## [2,] 0 1 0 2
## [3,] 0 0 1 3
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 3
## [2,] 0 1 0 4
## [3,] 0 0 1 -6
5.6 例題5.4
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 0 -1 0.0 2.0
## [2,] 0 1 0 3 1.0 -6.0
## [3,] 0 0 1 -1 -0.5 2.5
5.7 問題5.4
## [,1] [,2] [,3] [,4]
## [1,] 1 0 1.5 -0.5
## [2,] 0 1 -2.0 1.0
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 0 1 -1 0
## [2,] 0 1 0 0 1 -1
## [3,] 0 0 1 0 0 1
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 0 0 -0.75 0.625 -0.125
## [2,] 0 1 0 2.00 -2.000 1.000
## [3,] 0 0 1 -0.75 1.125 -0.625
5.8 演習問題5
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 0 6
## [2,] 0 1 0 0 2
## [3,] 0 0 1 0 1
## [4,] 0 0 0 1 -3
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 0 1
## [2,] 0 1 0 0 2
## [3,] 0 0 1 0 0
## [4,] 0 0 0 1 7