R中曲线函数的3D等价物? [英] 3D equivalent of the curve function in R?

查看:23
本文介绍了R中曲线函数的3D等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R 中的curve 函数提供了一种绘制函数的简单方法.例如,这将绘制一条直线

The curve function in R provides a simple way to plot a function. For example, this will plot a straight line

f1 <- function(x) x
curve(f1, from=-1, to=1)

R 中是否有一个等效的函数,它接受一个带有两个参数(例如,xy)和两个变量范围的函数,并生成一个 3D情节?

Is there an equivalent function in R which takes a function with two argument (e.g., x and y) and ranges for both variables and produces a 3D plot?

例如,假设我有以下功能

For example, imagine I had the following function

f2 <- function(x, y) x + y

有没有类似下面的命令?

Is there a command similar to the following?

curve_3d(f2, x_range=c(-1, 1), y_range=c(-1, 1))

推荐答案

package:rgl 中的 surface3d 函数看起来很匹配.创建一个包装器来获取您的函数,使用 seq() 创建一组 xy 向量,然后将这些向量传递给 outer 与您的 f2 作为将非常简单FUN 参数,然后调用 surface3d.

The surface3d function in package:rgl looks like a good match. It would be very simple to create a wrapper that would take your function, create an x-y set of vectors with seq() and then pass those vectors to outer with your f2 as the FUN argument, and then call surface3d.

还有一个 persp3d,作者(Duncan Murdoch 和其他人)说它是更高级别的",并且它似乎默认添加了轴,而 Surface3d 没有.

There is also a persp3d which the authors (Duncan Murdoch and perhaps others) say is "higher level" and it does appear to add axes by default which surface3d does not.

curve_3d <- function(f2, x_range=c(-1, 1), y_range=c(-1, 1), col=1:6 ){ 
       if (!require(rgl) ) {stop("load rgl")}
       xvec <- seq(x_range[1], x_range[2], len=15)
        yvec <- seq(y_range[1], y_range[2], len=15)
       fz <- outer(xvec, yvec, FUN=f2)
       open3d()
       persp3d( xvec, yvec, fz, col=col) }
curve_3d(f2)
snapshot3d("out3dplane.png")

现在我进一步考虑,你可以用 persp()wireframe() 做类似的事情.技巧"是使用外(...,FUN=fun).当我进一步考虑它时……将它与 outer 一起使用的能力取决于它由所有矢量化操作组成.如果它们没有被矢量化,我们将需要用 Vectorizemapply 重写.

Now that I think about it further, you could have done something similar with persp() or wireframe(). The "trick" is using outer(..., FUN=fun). And as I think about it even further ... the ability to use it with outer depends on it being composed of all vectorized operations. If they were not vectorized, we would need to rewrite with Vectorize or mapply.

这篇关于R中曲线函数的3D等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆