Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

plot3d - R 3D surface plot - add a path on the surface

I have made a surface plot in R using the plot3D-package and the following code

library(plot3D)

# Plot Quadratic Function
u <- function(x1,x2,A,b)
    {
        n_row <- length(x1)
        n_col <- length(x2)
        X <- cbind(rep(x1,each=n_col),rep(x2,n_row))
        f <- function(x)
            {   
                x <- cbind(x)
                out <- 0.5*t(x)%*%A%*%x + t(x)%*%b
                return(out)
            }
        out <- cbind(X,apply(X,FUN=f,MAR=1))
    }

x1 <- seq(-4,4,length.out=30)
x2 <- x1
A <- matrix(c(1,0,0,1),2,2)
b <- cbind(c(.5,.5))

-solve(A)%*%b
dt <- u(x1,x2,A,b)
x <- dt[,1]
y <- dt[,2]
z <- matrix(dt[,3],ncol=length(x2),nrow=length(x1))
persp3D(x1,x2, z = z, theta = 50, phi = 30, box = T, axes=TRUE, 
nticks=5, ticktype="detailed", 
scale = FALSE, expand = 0.3, contour =  list(nlevels = 15, col = "white"),
image = list(col = grey (seq(0.2, 0.8, length.out = 100))),shade=0.1,
zlim = range(z)+c(-8,1), clim = range(z), plot = TRUE)

looking like this

enter image description here

I would like to add a path on the surface illustrating a gradient descend algorithm but do not know how to do that.

By a path I mean a sequence of points c(x_1,y_1),...,c(x_n,y_n) plotted on the surface.

Any tips?

question from:https://stackoverflow.com/questions/65646040/r-3d-surface-plot-add-a-path-on-the-surface

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...