Computes the tangent plane to the graph of a scalar field z = f(x, y)
at a given point (x0, y0), together with the associated normal
vector. Optionally, it displays the surface, the tangent plane, two
orthogonal cross-sections and the normal vector using plotly.
tangent_plane3d(
f,
point,
h = 1e-04,
plot = FALSE,
x_window = 4,
y_window = 4,
z_window = 4,
grid = 50,
plane_window = 1,
vec_N_factor = 1,
surface_opacity = 0.85,
plane_opacity = 0.7,
colors = list(surface = "Viridis", plane = "Reds", xcut = "black", ycut = "black",
point = "blue", normal = "red"),
show_surface_grid = FALSE,
surface_grid_color = "rgba(0,0,0,0.35)",
surface_grid_width = 1,
show_axis_grid = TRUE,
axis_grid_color = "#e5e5e5",
axis_grid_width = 1,
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, fy: approximations of the partial derivatives
f_x(x0, y0) and f_y(x0, y0),
f0: value f(x0, y0),
normal_unit: unit normal vector at the point of tangency,
normal_raw: non-normalized normal vector
(-fx, -fy, 1),
plane_fun: function g(x, y) for the tangent plane,
plane_coeff: numeric vector c(a, b, c, d) such that
a x + b y + c z + d = 0 is the tangent-plane equation,
fig: a plotly figure when plot = TRUE,
otherwise NULL.
Function of two variables f(x, y) returning a numeric
scalar, representing the height z.
Numeric vector of length 2 giving the point of tangency
c(x0, y0).
Numeric step used in the centered finite-difference approximation of the partial derivatives. Must be strictly positive.
Logical; if TRUE, constructs a plotly figure
with the surface, tangent plane, cross-sections and normal vector.
Numeric half-width of the window in the x
direction used to draw the surface patch.
Numeric half-width of the window in the y
direction used to draw the surface patch.
Numeric half-height for the visible z range
around f(x0, y0).
Integer number of grid points used to sample the surface in each horizontal direction.
Numeric half-width of the square patch of the
tangent plane drawn around (x0, y0).
Numeric scale factor applied to the unit normal vector when drawing the segment that represents the normal.
Numeric value between 0 and 1 controlling the opacity of the surface patch.
Numeric value between 0 and 1 controlling the opacity of the tangent-plane patch.
List with named entries that control colors in the plot:
surface: colorscale for the original surface,
plane: colorscale for the tangent plane,
xcut: color for the intersection curve along
y = y0,
ycut: color for the intersection curve along
x = x0,
point: color for the tangency point marker,
normal: color for the normal vector segment.
Logical; if TRUE, draws a grid on the
surface patch.
Color and width of the surface grid lines.
Logical; if TRUE, draws grid lines on the
coordinate axes in the 3D scene.
Color and width of the axis grid lines.
List with 3D scene options passed to plotly::layout,
typically including axis titles and aspectmode.
List with background colors for the figure, with fields
paper and plot.
Given a differentiable function f(x, y) and a point
(x0, y0), the function:
approximates the partial derivatives f_x(x0, y0) and
f_y(x0, y0) using centered finite differences with step
h,
builds the tangent plane
g(x, y) = f(x0, y0) + f_x(x0, y0) * (x - x0) + f_y(x0, y0) * (y - y0),
constructs a normal vector n = (-f_x, -f_y, 1) and its
unit version,
encodes the plane in the form a x + b y + c z + d = 0,
with the coefficients returned in plane_coeff.
When plot = TRUE, the function produces an interactive figure
containing:
a patch of the original surface,
a patch of the tangent plane centered at (x0, y0),
two intersection curves of the surface along x = x0 and
y = y0,
the point of tangency and a segment in the direction of the normal vector.
if (interactive()) {
f <- function(x, y) x^2 + y^2
tp <- tangent_plane3d(
f,
point = c(1, 1),
plot = FALSE
)
tp$plane_coeff
}
Run the code above in your browser using DataLab