# \donttest{
scalar_field <- function(u){
x <- u[1]; y <- u[2]
(x + y)^2 + 4*(x - y)^2 - 8*(.5)^2
}
gradient_field <- function(u) numDeriv::grad(scalar_field, u)
s <- seq(-1, 1, length.out = 51)
expand.grid("x" = s, "y" = s) |>
transform(phi = apply(cbind(x, y), 1, scalar_field)) |>
ggplot(aes(x, y)) + geom_raster(aes(fill = phi))
ggplot() + geom_potential(fun = gradient_field)
ggplot() + geom_potential(fun = gradient_field, verify_conservative = TRUE)
# }
Run the code above in your browser using DataLab