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
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