To use persp
, you need to supply values of x
, values of y
, and values of z
for each combination of x
and y
. The easiest way to do this is to define x
and y
and then use outer
to create a matrix that crosses x
and y
. You need to specify the way the two variables should be combined as the third argument to outer
, in this case the function +
:
x <- seq(-3,3,length.out=100)
y <- seq(-3,3,length.out=100)
z <- outer(x^2,y^2,`+`)
persp(x,y,z, col='blue')
You may also be interested in rotating the results. Here are some examples using the theta
parameter:
par(mar=c(1,1,1,1))
layout(matrix(1:4, nrow=2))
s=lapply(c(0,30,60,90), function(t) persp(x,y,z, col='blue', theta=t))
EDIT: I understand from your comment you would like a 2D representation of this surface. The easiest way to get that in base R is with image
of your z
matrix:
image(z)