我想画一个函数: f(x,y)=x^2-2*y,有一个约束: x+y=1在我的图函数中重叠,很好地没有看到限制函数f(x,y)。如果x+y-1=0是透明的,会更好。R中的Mi代码:
x <- seq(-5, 5, length= 10)
y <- x
fun1<-function(x,y){x^2-2*y}
m <- outer(x, y, fun1)
m[is.na(m)] <- 1
persp(x, y, m, theta = 30, phi = 30,
expand = 0.5, col = "royalblue", ltheta = 120,
shade = 0.75, ticktype = "detailed")
par(new=TRUE)
fun1<-function(x,y){x+y-1}
m <- outer(x, y, fun2)
m[is.na(m)] <- 1
persp(x, y, m, theta = 30, phi = 30,
expand = 0.5, col = "red", ltheta = 120,
shade = 0.75, ticktype = "detailed")

发布于 2014-12-02 00:27:38
一些过高的计划可能会有帮助。上面评论中建议的第一个情节。然后,通过赋值NA来取消选择违反约束的段,即不使用颜色较重的绘图和过度绘制。(我发现,除非我冻结他们在最后一步“移动”的z限制。您可能需要抑制z轴标签,因为它们仍然相互重叠。)
png(); x <- seq(-5, 5, length= 10)
y <- x
fun1<-function(x,y){x^2-2*y}
m1 <- outer(x, y, fun1)
m1[is.na(m)] <- 1
persp(x, y, m1, theta = 30, phi = 30,
expand = 0.5, col = "#4169E155", ltheta = 120,
shade = 0.75, ticktype = "detailed",zlim=c(-15,35))
par(new=TRUE)
fun2<-function(x,y){x+y-1}
m2 <- outer(x, y, fun2)
m2[is.na(m)] <- 1
persp(x, y, m2, theta = 30, phi = 30,
expand = 0.5, col = adjustcolor("red", alpha.f=0.5), ltheta = 120,
shade = 0.75, ticktype = "detailed",zlim=c(-15,35))
par(new=TRUE)
fun3<-function(x,y){x^2-2*y}
m3 <- outer(x, y, fun3)
m3[ m3 < m2 ] <- NA # <--- logical indexing; this is the key step
persp(x, y, m3, theta = 30, phi = 30,
expand = 0.5, col = "#4169E1", ltheta = 120, # solid-blue
shade = 0.75, ticktype = "detailed",zlim=c(-15,35));dev.off()

https://stackoverflow.com/questions/27239168
复制相似问题