Domanda

My inequality:

x^2 + y^2 + z^2 < 1 AND x^2 + y^2 < z^2

How to plot this logical combination of inequalities in R? (range for all three axis = -1,1)

È stato utile?

Soluzione

Using rgl:

x <- y <- z <- seq(-1, 1, by=0.01)
df <- setNames(expand.grid(x, y, z), c("x", "y", "z"))
df <- transform( df, ueq = (x^2 + y^2 + z^2 < 1) & (x^2 + y^2 < z^2))
df$color <- ifelse(df$ueq == TRUE, "green" , "red")
require(rgl)
with(df[df$ueq == TRUE, ], plot3d(x=x, y=y, z=z, col=color, type="p", size=5))
grid3d(c("x", "y+", "z"))

enter image description here

Altri suggerimenti

Here's a rgl solution, to make a 3D-plot and adding points colored by your condition.

# some data on a grid
x = seq(-1, 1, len = 10); 
df <- expand.grid(x=x,y=x,z=x)
# indicator for color
df$ind <- with(df, x^2 + y^2 + z^2 < 1 & x^2 + y^2 < z^2)

require(rgl)
# empty plot
plot3d(df$x, df$y, df$z, type = 'n')
# add points
with(df[df$ind, ], points3d(x, y, z, color = 'red', size = 10))
with(df[!df$ind, ], points3d(x, y, z, color = 'blue', size = 10))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top