5

I have a function in 3D - let's say De Jong function:

fdejong <- function (x, y) {
   return (x^2 + y^2)
}

How can I draw it's plot in 3D? I want to achieve an effect similar to this from wikipedia:

http://en.wikipedia.org/wiki/File:Sphere_function_in_3D.pdf

Schorsch
  • 7,761
  • 6
  • 39
  • 65
SathOkh
  • 826
  • 2
  • 12
  • 24

2 Answers2

9

try this :

fdejong <- function (x, y) {
   return (x^2 + y^2)
}


x <- seq(-10, 10, length= 30)
y <- x
z <- outer(x, y, fdejong)
z[is.na(z)] <- 1
op <- par(bg = "white")
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")

fdejong persp plot

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Vincent Guyader
  • 2,927
  • 1
  • 26
  • 43
8

You can also use the Lattice wireframe function. (Using @user1020027's data)

fdejong <- function (x, y) {
   return (x^2 + y^2)
}

x <- seq(-10, 10, length= 30)
y <- x
z <- outer(x, y, fdejong)
z[is.na(z)] <- 1

require(lattice)
wireframe(z, drape=T, col.regions=rainbow(100))

lattice wireframe color plot

MrFlick
  • 195,160
  • 17
  • 277
  • 295