如何绘制 R 中两个变量函数的 3D 图
How to plot 3D graph of function of two variables in R
我的任务是绘制两个变量 f(x,y) 的函数,其中 m 是成熟度,从 1 到 15。这是我的代码:
f<-function(x,y)((x -y * ((1 - exp(-m / 4)) / (m / 4)) - 0.035 * ((1 - exp(-m / 4
)) / (m / 4) - exp(-m / 4))))
z <- outer(x, y, f)
persp(x, y, z)
open3d()
plot3d(f,xlab = "x", ylab = "y", zlab = "m", ylim = c(-1, 1), xlim = c(-1, 1), zlim = c(1, 15))
但是 3D grpah 看起来像这样:
谁能告诉我哪里错了?
您不能将函数作为参数传递给 plot3D
。而是传递您的变量 x
、y
和 f(x,y)
。这是一个简单的可重现的例子
set.seed(5)
x <- runif(100)
m <- 4
f<-function(x,y)((x -y * ((1 - exp(-m / 4)) / (m / 4)) - 0.035 * ((1 - exp(-m / 4
)) / (m / 4) - exp(-m / 4))))
z <- f(x,y)
library(rgl)
open3d()
plot3d(x,y,z,xlab = "x", ylab = "y", zlab = "m")
哪个给你
如果您使用另一个 type
符号来绘制您的数据,请查看 plot3D
的 type
参数。默认情况下设置为 "p"
,表示“点”,但有用于“线”、“线段”、球面 ecc 的方法(参见 ?plot3D
):
type
For the default method, a single character indicating the type of item to plot. Supported types are: 'p' for points, 's' for spheres, 'l' for lines, 'h' for line segments from z = 0, and 'n' for nothing. For the mesh3d method, one of 'shade', 'wire', or 'dots'. Partial matching is used.
我的任务是绘制两个变量 f(x,y) 的函数,其中 m 是成熟度,从 1 到 15。这是我的代码:
f<-function(x,y)((x -y * ((1 - exp(-m / 4)) / (m / 4)) - 0.035 * ((1 - exp(-m / 4
)) / (m / 4) - exp(-m / 4))))
z <- outer(x, y, f)
persp(x, y, z)
open3d()
plot3d(f,xlab = "x", ylab = "y", zlab = "m", ylim = c(-1, 1), xlim = c(-1, 1), zlim = c(1, 15))
但是 3D grpah 看起来像这样:
谁能告诉我哪里错了?
您不能将函数作为参数传递给 plot3D
。而是传递您的变量 x
、y
和 f(x,y)
。这是一个简单的可重现的例子
set.seed(5)
x <- runif(100)
m <- 4
f<-function(x,y)((x -y * ((1 - exp(-m / 4)) / (m / 4)) - 0.035 * ((1 - exp(-m / 4
)) / (m / 4) - exp(-m / 4))))
z <- f(x,y)
library(rgl)
open3d()
plot3d(x,y,z,xlab = "x", ylab = "y", zlab = "m")
哪个给你
如果您使用另一个 type
符号来绘制您的数据,请查看 plot3D
的 type
参数。默认情况下设置为 "p"
,表示“点”,但有用于“线”、“线段”、球面 ecc 的方法(参见 ?plot3D
):
type
For the default method, a single character indicating the type of item to plot. Supported types are: 'p' for points, 's' for spheres, 'l' for lines, 'h' for line segments from z = 0, and 'n' for nothing. For the mesh3d method, one of 'shade', 'wire', or 'dots'. Partial matching is used.