R - "不能强迫类型'关闭'矢量类型'任何' "当使用外(x,f)函数时

时间:2017-12-10 14:00:21

标签: r plot

我尝试绘制功能的3D视角,但是当我使用z <- outer(x, egg)

时我收到此错误
Error in as.vector(x, mode) : 
  cannot coerce type 'closure' to vector of type 'any'

我的剧本:

egg <- function(x) {
  return (-(x[2] + 47)*sin(sqrt(abs((x[1]/2) + x[2] + 47))) - x[1]*sin(sqrt(abs(x[1] - (x[2] + 47)))))
}

# Plotting eggholder
x1 <- seq(-512, 512, length.out=100)
x2 <- seq(-512, 512, length.out=100)
x <- c(x1, x2)
z <- outer(x, egg)
par(mar=c(1,1,1,1))
layout(matrix(1:4, nrow=2))
lapply(c(0,30,60,90), function(t) persp(x1,x2,z, col='blue', theta=t))

1 个答案:

答案 0 :(得分:1)

这是代码的工作原理。感谢LyzandeR。

egg <- function(x1, x2) {
  return (-(x2 + 47)*sin(sqrt(abs((x2/2) + x2 + 47))) - 
  x2*sin(sqrt(abs(x1 - (x2 + 47))))) 
}

# Plotting eggholder
x1 <- seq(-512, 512, length.out=100)
x2 <- seq(-512, 512, length.out=100)
z <- outer(x1, x2, egg)
par(mar=c(1,1,1,1))
layout(matrix(1:4, nrow=2))
lapply(c(0,30,60,90), function(t) persp3D(x1, x2, z, theta=t))