用R求解欠定线性系统



R可以求解欠定线性系统:

A = matrix((1:12)^2,3,4,T)
B = 1:3
qr(A)$rank  # 3
qr.solve(A, B)  # solutions will have one zero, not necessarily the same one
# 0.1875 -0.5000  0.3125  0.0000
solve(qr(A, LAPACK = TRUE), B)
# 0.08333333 -0.18750000  0.00000000  0.10416667

(它给出了无穷多个解中的一个解(。

然而,如果秩(这里是2(低于行数(这里是3(,它就不起作用:

A = matrix(c((1:8)^2,0,0,0,0),3,4,T)
B = c(1,2,0)
A
#      [,1] [,2] [,3] [,4]
# [1,]    1    4    9   16
# [2,]   25   36   49   64
# [3,]    0    0    0    0
qr.solve(A, B)  # Error in qr.solve(A, B) : singular matrix
solve(qr(A, LAPACK = TRUE), B)  # Error in qr.coef(a, b) : error code 3 

但这个系统确实有解决方案!

我知道一般的解决方案是使用SVD或A的广义/伪逆(见这个问题及其答案(,但是:

对于solveqr.solve,是否存在将系统AX=B自动缩减为仅列(a(行的等效系统CX=D的平均值,对于该系统,qr.solve(C, D)将简单地开箱即用?

示例:

C = matrix(c((1:8)^2),2,4,T)
D = c(1,2)
qr.solve(C, D)
# -0.437500  0.359375  0.000000  0.000000

qr.coefqr似乎起到了作用:

(A <- matrix(c((1:8)^2, 0, 0, 0, 0), nrow = 3, ncol = 4, byrow = TRUE))
#     [,1] [,2] [,3] [,4]
# [1,]    1    4    9   16
# [2,]   25   36   49   64
# [3,]    0    0    0    0
(B <- c(1, 2, 0))
# [1] 1 2 0
(X0 <- qr.coef(qr(A), B))
# [1] -0.437500  0.359375        NA        NA
X0[is.na(X0)] <- 0
X0
# [1] -0.437500  0.359375  0.000000  0.000000
# Verification:
A %*% X0
#      [,1]
# [1,]    1
# [2,]    2
# [3,]    0

第二个例子:

(A<-matrix(c(1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 1, 0), nrow = 3, ncol = 4, byrow = TRUE))
#      [,1] [,2] [,3] [,4]
# [1,]    1    2    0    0
# [2,]    1    2    0    0
# [3,]    1    2    1    0
(B<-c(1, 1, 2))
# [1] 1 1 2
qr.solve(A, B)
# Error in qr.solve(A, B) : singular matrix 'a' in solve
(X0 <- qr.coef(qr(A), B))
# [1]  1 NA  1 NA
X0[is.na(X0)] <- 0
X0
# [1] 1 0 1 0
A %*% X0
#      [,1]
# [1,]    1
# [2,]    1
# [3,]    2

相关内容

  • 没有找到相关文章

最新更新