graphics - graph a function restricted in R -
i'd want plot function: f(x,y)=x^2-2*y, constraint: x+y=1 in graph functions overlap , not seen restricted function f(x,y). appreciate better if x+y-1=0 transparent. mi code in r:
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")
some overplotting might help. first plot suggested in comments above. de-select segments constraint violated assigning na, i.e. no plotting , overplot heavier color. ( found unless froze z-limits "shifted" @ last step. may need suppress z-axis labels, since still overlaying each other.)
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; 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()
Comments
Post a Comment