Produces a Plotly animation showing level curves of a scalar field together with the gradient direction at a point and a rotating unit direction vector. The directional derivative value is displayed on screen for each frame. A highlight is shown when the rotating direction aligns with the gradient, which corresponds to the maximum directional derivative.
gradient_direction2d(
f,
x0,
y0,
xlim,
ylim,
n_grid = 70L,
theta_vals = NULL,
h = 1e-04,
arrow_scale = NULL,
frame_ms = 220L,
transition_ms = 220L,
title = NULL,
safe_mode = TRUE,
align_tol = 0.08
)A plotly object (htmlwidget) with animation frames.
Function. A real-valued function f(x,y). It must accept two numeric arguments and return numeric values.
Numeric scalar. x-coordinate of the base point.
Numeric scalar. y-coordinate of the base point.
Numeric vector of length 2. Range for x in the contour plot.
Numeric vector of length 2. Range for y in the contour plot.
Integer. Grid size per axis for the contour computation.
Numeric vector. Angles (radians) used as frames. If NULL, a default sequence from 0 to 2*pi is used.
Numeric scalar. Step size for central differences.
Numeric scalar. Scale factor for drawing arrows. If NULL, an automatic scale based on the plot window is used.
Integer. Frame duration in milliseconds.
Integer. Transition duration in milliseconds.
Character. Plot title. If NULL, a default title is used.
Logical. If TRUE, use calmer animation defaults intended to reduce flicker and visual stress.
Numeric scalar. Angular tolerance (radians) used to decide when the rotating direction is considered aligned with the gradient.
The scalar field is $$ z = f(x,y). $$ At the point $$ (x_0,y_0), $$ the gradient vector is $$ \nabla f(x_0,y_0) = \left(\frac{\partial f}{\partial x}(x_0,y_0), \frac{\partial f}{\partial y}(x_0,y_0)\right). $$ For a unit direction $$ \mathbf{u}(\theta) = (\cos\theta,\sin\theta), $$ the directional derivative is $$ D_{\mathbf{u}} f(x_0,y_0) = \nabla f(x_0,y_0)\cdot \mathbf{u}(\theta). $$ The maximum value over unit directions is $$ \max_{\|\mathbf{u}\|=1} D_{\mathbf{u}} f(x_0,y_0) = \|\nabla f(x_0,y_0)\|, $$ and it occurs when $$ \mathbf{u}(\theta) $$ points in the same direction as $$ \nabla f(x_0,y_0). $$
Partial derivatives are approximated numerically by central differences.
# \donttest{
library(plotly)
f <- function(x, y) x^2 + 2*y^2
gradient_direction2d(
f = f,
x0 = 0.6,
y0 = 0.4,
xlim = c(-1.5, 1.5),
ylim = c(-1.5, 1.5),
safe_mode = TRUE,
align_tol = 0.06
)
# }
Run the code above in your browser using DataLab