我必须计算特征值和特征向量,但我总是有这个警告。我希望有人能告诉我发生了什么。
#masa en kg
m1=559.3e-3
m2=419.4e-3
m3=m2
m4=m2
m5=m2
m6=m2
m7=m2
m8=m2
#rigidez en N/m
k=56.7e3
#matrices de masa y rigidez
M=matrix(c(m1,0,0,0,0,0,0,0,0,m2,0,0,0,0,0,0,0,0,m3,0,0,0,0,0,0,0,0,m4,0,0,0,0,0,0,0,0,m5,0,0,0,0,0,0,0,0,m6,0,0,0,0,0,0,0,0,m7,0,0,0,0,0,0,0,0,m8), 8, 8, byrow=TRUE)
K=matrix(c(k,-k,0,0,0,0,0,0,-k,2*k,-k,0,0,0,0,0,0,-k,2*k,-k,0,0,0,0,0,0,-k,2*k,-k,0,0,0,0,0,0,-k,2*k,-k,0,0,0,0,0,0,-k,2*k,-k,0,0,0,0,0,0,-k,2*k,-k,0,0,0,0,0,0,-k,k), 8, 8, byrow=TRUE)
#calculo valores y vectores propios
a=eigen(K,M)
>Warning message:
In if (symmetric) { :
the condition has length > 1 and only the first element will be used
此错误意味着变量symmetric
(可能是eigen
函数的内部变量(正在计算长度大于 1 的逻辑向量。当这种情况发生在 if 语句的条件中时,仅使用第一个元素。
该文档使我认为您希望将每个矩阵分别传递给函数。
a <- eigen(K)
b <- eigen(M)
使用?eigen
查看此函数的帮助,这表明它只期望一个参数是矩阵。
编辑:您可以在下面找到定义eigen()
函数的代码。在 RStudio IDE 中,可以将鼠标悬停在某个函数上,然后按 F2 获取该函数的代码。基于此,当您将M
作为eigen()
函数的第二个参数时,它将其解释为eigen(x = K, symmetric = M)
。它期望symmetric
TRUE
、FALSE
或缺失,但它得到的是一个矩阵。当它到达将symmetric
用作if
语句条件的行时,它会引发此警告。
function (x, symmetric, only.values = FALSE, EISPACK = FALSE)
{
x <- unname(as.matrix(x))
n <- nrow(x)
if (!n)
stop("0 x 0 matrix")
if (n != ncol(x))
stop("non-square matrix in 'eigen'")
n <- as.integer(n)
if (is.na(n))
stop("invalid nrow(x)")
complex.x <- is.complex(x)
if (!all(is.finite(x)))
stop("infinite or missing values in 'x'")
if (missing(symmetric))
symmetric <- isSymmetric.matrix(x)
if (symmetric) { # Here is the line generating the error
z <- if (!complex.x)
.Internal(La_rs(x, only.values))
else .Internal(La_rs_cmplx(x, only.values))
ord <- rev(seq_along(z$values))
}
else {
z <- if (!complex.x)
.Internal(La_rg(x, only.values))
else .Internal(La_rg_cmplx(x, only.values))
ord <- sort.list(Mod(z$values), decreasing = TRUE)
}
if (only.values)
list(values = z$values[ord], vectors = NULL)
else structure(class = "eigen", list(values = z$values[ord],
vectors = z$vectors[, ord, drop = FALSE]))
}