Identifies stationary points of a function of two variables over a given rectangular domain. A set of initial points is generated on a regular grid together with additional random starting points. Each start is refined using numerical optimization applied to the squared gradient norm. Points with a sufficiently small gradient norm are kept and then merged if they are too close to each other.
critical_points_2d(
f,
xlim,
ylim,
start_n = c(7L, 7L),
n_rand = 40L,
h = NULL,
tol_grad = 1e-06,
tol_merge = 0.001,
tol_eig = 1e-06,
maxit = 200,
optim_method = c("BFGS", "Nelder-Mead"),
plot = TRUE,
grid_plot = c(60L, 60L),
surface_colorscale = "YlGnBu",
surface_opacity = 0.85,
cp_colors = list(minimum = "#2ca02c", maximum = "#d62728", saddle = "#1f77b4", flat =
"#ff7f0e"),
cp_size = 6,
scene = list(aspectmode = "data", xaxis = list(title = "x"), yaxis = list(title = "y"),
zaxis = list(title = "f(x,y)"))
)A list with:
critical_pointsA data frame with columns x, y, z, the gradient
norm, and the classification label.
figA plotly object if plot = TRUE, or NULL otherwise.
Function of two variables f(x, y) returning a numeric scalar.
Numeric vector c(xmin, xmax) defining the domain in the
x-direction.
Numeric vector c(ymin, ymax) defining the domain in the
y-direction.
Integer vector of length two. Number of regular starting points per axis for the grid.
Integer. Number of additional random starting points inside the domain.
Numeric step size for finite-difference gradients and Hessians.
If NULL, an automatic step size is used.
Numeric tolerance. Points with gradient norm below this threshold are treated as stationary.
Numeric tolerance for merging nearby solutions.
Numeric tolerance used when classifying eigenvalues of the Hessian.
Maximum number of iterations permitted for numerical optimization.
Character string naming an optimization method supported
by stats::optim, such as "BFGS" or "Nelder-Mead".
Logical. If TRUE, a plotly surface with critical
points is drawn.
Integer vector of length two. Resolution of the grid used when drawing the surface.
Character. Name of the Plotly colorscale for the surface.
Numeric between 0 and 1 giving the opacity of the surface.
Named list mapping each critical point type to a color.
Expected names: "minimum", "maximum", "saddle",
"flat".
Numeric. Size of the point markers.
List of Plotly scene options (axis titles, aspect mode, and so on).
Each surviving point is classified based on the eigenvalues of the numerical Hessian matrix. The Hessian classification uses four categories:
"minimum" - both eigenvalues positive,
"maximum" - both eigenvalues negative,
"saddle" - mixed signs,
"flat" - small eigenvalues, inconclusive classification.
Optionally, a 3D surface with the detected critical points can be displayed using plotly.
# \donttest{
f <- function(x, y) x^2 + y^2
critical_points_2d(f, xlim = c(-2, 2), ylim = c(-2, 2), plot = FALSE)
# }
Run the code above in your browser using DataLab