2
使用R包optR求解線性方程給出了Gauss-seidel方法的不正確結果。 我試着用不同的方法解決,它看起來不錯。例如解決或使用optR,但使用方法高斯。 示例就在這裏。或者我做錯了什麼?用Gauss-Seidel方法求解線性方程給出了錯誤的結果
library(optR)
A<-matrix(c(5,-1,2, 3, 8,-2,1, 1,4), nrow=3,ncol=3, byrow = TRUE)
b<-matrix(c(12,-25,6), nrow=3,ncol=1,byrow=TRUE)
Z1<-optR(A, b,method="gaussseidel", iter=500, tol=1e-7)
Z2 <-optR(A, b, method="gauss", iter=500, tol=1e-7)
Z3 <- solve(A,b)
print(Z1)
print(Z2)
print(Z3)
A %*% Z1$beta - b
A %*% Z2$beta - b
A %*% Z3 - b
係數爲Z1是不正確的:提前
> print(Z1)
call:
optR.default(x = A, y = b, method = "gaussseidel", iter = 500,
tol = 1e-07)
Coefficients:
[,1]
[1,] 4.183908
[2,] -2.379310
[3,] -1.781609
> print(Z2)
call:
optR.default(x = A, y = b, method = "gauss", iter = 500, tol = 1e-07)
Coefficients:
[,1]
[1,] 1
[2,] -3
[3,] 2
> print(Z3)
[,1]
[1,] 1
[2,] -3
[3,] 2
>
> A %*% Z1$beta - b
[,1]
[1,] 7.735632
[2,] 22.080460
[3,] -11.321839
> A %*% Z2$beta - b
[,1]
[1,] 0.000000e+00
[2,] -3.552714e-15
[3,] 0.000000e+00
> A %*% Z3 - b
[,1]
[1,] 0.000000e+00
[2,] -3.552714e-15
[3,] 0.000000e+00
感謝。