How can I add points and vectors to a plot-3d-surface plot using plotsy-3d?

How can I add points and vectors to the following plot in order to illustrate gradient descent?

#import "@preview/plotsy-3d:0.2.0": plot-3d-surface, plot-3d-parametric-curve

#let f(x1, x2) = calc.pow(x1, 2) - 2 * x1 * x2 + 2 * calc.pow(x2, 2) + 2*x1

#align(center)[
    #let size = 5
    #let scale-factor = 0.15
    #let (xscale,yscale,zscale) = (0.7,0.3,0.01)
    #let scale-dim = (xscale*scale-factor,yscale*scale-factor, zscale*scale-factor)  
    #let func(x,y) = x*x + y*y
    #let color-func(x, y, z, x-lo,x-hi,y-lo,y-hi,z-lo,z-hi) = {
      return blue.transparentize(20%).darken((y/(y-hi - y-lo))*100%).lighten((x/(x-hi - x-lo)) * 50%)
    }

    #plot-3d-surface(
      f,
      color-func: color-func,
      subdivisions: 1,
      subdivision-mode: "decrease",
      scale-dim: scale-dim,
      xdomain: (-size,size),
      ydomain:  (-size,size),
      pad-high: (0,0,0),
      pad-low: (0,0,0),
      axis-step: (2,2,50),
      dot-thickness: 0.05em,
      front-axis-thickness: 0.05em,
      front-axis-dot-scale: (0.05,0.05),
      rear-axis-dot-scale: (0.08,0.08),
      rear-axis-text-size: 0.5em,
      axis-label-size: 1em,
      xyz-colors: (black,black,black),
      axis-labels: ($x_1$, $x_2$, $y$),
      
    )
  ]

Hello @NeoPrim ,I am so sorry, but your code does not compile… could you please edit so we can at least reproduce? Thanks.

unknown variable: f

Oops, sorry!
I’ve updated the question to include the function!

1 Like