R:作二元函数的图

用到的包

{rgl}和{plot3Drgl}、{emdbook}

{rgl}的下载比较麻烦,先是在R窗口中下载:

install.packages("rgl",dependencies=TRUE,repos="http://cran.rstudio.com")

下了一堆,在加载{rgl}是,提示

Error: package or namespace load failed for ‘rgl’:
 package ‘htmltools’ was installed by an R version with different internals; it needs to be reinstalled for use with this R version

移除htmltools,然后重新安装后,又提示mime、jsonlite同样的问题。于是全部卸载重装

remove.packages("html", "mime", "jsonlite", "rgl")
install.packages("html", "mime", "jsonlite", "rgl", dependencies=TRUE, repos="http://cran.rstudio.com")

最后安装{plot3Drgl}, {emdbook},比较顺利。

示例

library(rgl)

x <- y <- seq(-5,5,length=1000)
f <- function(x, y) 4*x*y
z <- outer(x, y, z)

persp(x, y, z, col="blue")

 R:作二元函数的图_第1张图片

#可拖拉旋转的3D图
library(plot3Drgl)
par(mai=c(0.8,0.8,0,0))

x <- y <- seq(-5,5,length=200)
f <- function(x,y) 4*x*y
z <- outer(x,y,f)

persp3Drgl(z=z)

 

library(emdbook)
par(mai=c(0.8,0.8,0,0))

f <- function(x,y) 4*x*y
# sys3d = "persp", "wireframe", "rgl", "contour", "image", or "none"
# n不要太大,运算太慢
curve3d(expr = f, from = c(-3,-3), to = c(3,3), n = 100, sys3d = "rgl")

 

你可能感兴趣的:(R,R,作图)