Computes a numerical directional derivative of a multivariate function
at a given point, along a specified direction. The derivative is
approximated using centered finite differences. If the dimension is two
and plot = TRUE, the function displays a local visualization of
the surface defined by z = f(x, y), including the evaluation point,
the direction, and the curve traced along that direction.
directional_derivative3d(
f,
x0,
v,
h = 1e-06,
plot = FALSE,
x_window = 2,
y_window = 2,
n_s = 180,
n_r = 50,
show_strip = TRUE,
strip_colorscale = "Blues",
strip_opacity = 0.3,
show_surface_grid = TRUE,
surface_grid_color = "rgba(60,80,200,0.25)",
surface_grid_width = 1,
curve_line = list(color = "red", width = 1),
point_marker = list(color = "black", size = 3, symbol = "circle"),
u_line = list(color = "black", width = 1),
w_line = list(color = "black", width = 0.5),
t_range = c(-2, 2),
scene = list(aspectmode = "data", xaxis = list(title = "x"), yaxis = list(title = "y"),
zaxis = list(title = "z")),
bg = list(paper = "white", plot = "white"),
tol = 1e-12
)A list containing:
DThe directional derivative at x0 along the
normalized direction.
v_hatThe normalized direction vector.
fx, fyCentered partial derivatives in two
dimensions (only returned when length(x0) == 2).
figA plotly visualization when plot = TRUE,
otherwise NULL.
A function returning a numeric scalar. It may be defined either
as f(x, y, ...) with several numeric arguments, or as a function
of a numeric vector, f(x_vec).
Numeric vector giving the evaluation point. Its length determines the dimension of the problem.
Numeric vector giving the direction along which the directional
derivative is computed. Must be nonzero and have the same length as
x0.
Numeric step size used for centered finite-difference approximations.
Logical; if TRUE and length(x0) == 2, displays a
3D visualization of the local surface using plotly.
Numeric half-widths defining the size of the rectangular window around the evaluation point in the 2D case.
Integer giving the number of samples along the direction line in the 2D visualization.
Integer giving the number of samples across the strip in the 2D visualization.
Logical; if TRUE, draws a surface strip around
the evaluation point.
Character string specifying a plotly colorscale for the strip.
Numeric value between 0 and 1 determining the opacity of the strip.
Logical; if TRUE, overlays a grid onto
the surface strip.
Character string giving the grid line color.
Numeric value giving the grid line width.
Lists with plotly style options for the directional curve, evaluation point, and auxiliary lines.
Numeric vector of length two giving the parameter range for the directional curve in the 2D visualization.
Lists specifying 3D scene and background options when plotting.
Numeric tolerance used for detecting numerical degeneracies.
The function accepts two types of input for the function f:
a function of several numeric arguments, for example
f(x, y, z, ...), or
a function that takes a single numeric vector, such as
f(x_vec).
At the evaluation point x0, the function:
normalizes the direction vector v to obtain a unit
direction,
computes forward and backward perturbations along this unit direction,
evaluates the function at those perturbed points,
estimates the directional derivative using a centered finite-difference formula.
In two dimensions, if plot = TRUE, the function builds a small
rectangular window around x0 and evaluates the function over a
fine grid to produce a strip of the surface. It then overlays:
the base point,
the selected direction,
the trajectory of the directional curve,
(optionally) a surface grid and other plot elements.
# General n-dimensional usage:
f3 <- function(x, y, z) x^2 + y^2 + z
directional_derivative3d(f3, x0 = c(1, 0, 0), v = c(1, 1, 0))
# Two-dimensional example without plotting (fast, no plotly required):
f2 <- function(x, y) x^2 + y^2
directional_derivative3d(f2, x0 = c(0, 0), v = c(1, 2), plot = FALSE)
Run the code above in your browser using DataLab