在在研发曲线
功能提供了一种简单的方法来绘制功能。例如,这将绘制的直线
F1< - 函数(x)x
曲线(F1,从= -1,为= 1)
有没有在研发等效的函数,它接受一个函数有两个参数(例如, X
和是
)和范围两个变量,并生成一个3D图?
例如,假设我有以下功能
F2< - 函数(X,Y)X + Y
是否有类似如下的命令?
curve_3d(F2,x_range = C(1,1),y_range = C(1,1))
解决方案
在 surface3d
功能包:RGL看起来像一个很好的匹配。这将是非常简单的创建一个包装,将采取你的功能,创建一个XY组向量与序列()
键,然后通过这些载体,以外
您F2为FUN参数,然后调用 surface3d
。
还有一个 persp3d
其中作者(邓肯默多克也许其他人)说的是更上一层楼,它不会出现在默认情况下它surface3d不添加轴
curve_3d< - 函数(F2,x_range = C(1,1),y_range = C(1,1),COL = 1:6){
如果(!需要(RGL)){停止(负载RGL)}
XVEC&所述; - 以次(x_range [1],x_range [2],LEN = 15)
yvec&所述; - 以次(y_range [1],y_range [2],LEN = 15)
FZ< - 外(XVEC,yvec,FUN = F2)
open3d()
persp3d(XVEC,yvec,FZ,COL = COL)}
curve_3d(F2)
snapshot3d(out3dplane.png)
现在,我想进一步,你可以做类似的东西与 persp()
或线框()
。 绝招是使用外(...,FUN =好玩)。当我仔细想想,甚至进一步...的能力,用它与外
取决于它被所有的矢量运算组成。如果他们没有矢量化,我们需要重写与矢量
或 mapply
。
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)
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))
解决方案
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
.
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")
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
.
相关推荐
最新文章