Numerically approximates the partial derivatives \(f_x(x_0, y_0)\) and \(f_y(x_0, y_0)\) of a scalar field \(z = f(x, y)\) at a given point \((x_0, y_0)\) using central finite differences.
partial_derivatives_surface(
f,
x0,
y0,
h = NULL,
xlim = NULL,
ylim = NULL,
nx = 60L,
ny = 60L,
plot = TRUE,
scene = list(aspectmode = "data", xaxis = list(title = "x"), yaxis = list(title = "y"),
zaxis = list(title = "z")),
bg = list(paper = "white", plot = "white")
)A list with components:
fx: numeric scalar, approximation of \(f_x(x_0, y_0)\).
fy: numeric scalar, approximation of \(f_y(x_0, y_0)\).
f0: numeric scalar, \(f(x_0, y_0)\).
fig: a plotly object if plot = TRUE and
plotly is available; otherwise NULL.
Function function(x, y) returning a numeric scalar f(x, y).
Numeric scalars; coordinates of the point where the partial derivatives are evaluated.
Numeric step for the central finite differences. If NULL,
a default value is chosen as 1e-4 * (1 + max(abs(x0), abs(y0))).
Numeric length-2 vector c(x_min, x_max). Range for the
\(x\)-axis used to draw the surface. If NULL, a symmetric window
around x0 is used.
Numeric length-2 vector c(y_min, y_max). Range for the
\(y\)-axis used to draw the surface. If NULL, a symmetric window
around y0 is used.
Integer grid sizes (number of points) along x and y
for the surface plot. Recommended values are at least 20.
Logical; if TRUE, builds and returns a plotly
surface plot. If FALSE, only the numeric derivatives are returned.
List with plotly 3D scene options (axis titles, aspect
mode, and so on) passed to plotly::layout().
List with background colors for plotly, with components
paper and plot.
Optionally, it builds a 3D plotly surface for \(z = f(x, y)\) on a rectangular window around \((x_0, y_0)\) and overlays:
the intersection curve of the surface with the plane \(y = y_0\) and its tangent line given by \(f_x(x_0, y_0)\);
the intersection curve with the plane \(x = x_0\) and its tangent line given by \(f_y(x_0, y_0)\);
the base point \((x_0, y_0, f(x_0, y_0))\).
if (interactive()) {
f <- function(x, y) x^2 + 3 * x * y - y^2
res <- partial_derivatives_surface(
f,
x0 = 1, y0 = -1,
xlim = c(-1, 3),
ylim = c(-3, 1),
nx = 60, ny = 60
)
res$fx
res$fy
}
Run the code above in your browser using DataLab