Computes a numerical line integral of a scalar field along a parametric
curve in the plane. The function integrates the quantity
f(r(t)) * speed(t), where r(t) is the parametric curve
and speed(t) is the length of its derivative. Optionally, it
produces a 3D visualization that includes a surface representing
z = f(x, y), the curve itself in the plane, a lifted version of
the curve showing z = f(x(t), y(t)), and a vertical curtain
over the curve.
line_integral2d(
f,
r,
a,
b,
plot = TRUE,
n_curve = 400,
n_curtain_v = 40,
n_surf_x = 80,
n_surf_y = 80,
colorscale = "Viridis",
surface_opacity = 0.65,
show_surface_grid = TRUE,
surface_grid_color = "rgba(80,80,80,0.25)",
surface_grid_width = 1,
curtain = 0.4,
curtain_colorscale = c("white", "#2a9d8f"),
curve_color = "black",
curve_width = 3,
lifted_color = "red",
lifted_width = 2,
h = NULL,
method = c("adaptive", "simpson"),
n_simpson = 1000,
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:
value: the numeric value of the line integral,
fig: a plotly figure when plot = TRUE, or
NULL otherwise.
A scalar field, given as function(x, y) returning a
numeric value.
A parametric curve, given as function(t) that returns a
numeric vector of length two, interpreted as c(x, y).
Numeric scalars giving the parameter limits, with b > a.
Logical; if TRUE, produces a visualization using
plotly.
Number of sample points along the curve for plotting.
Number of subdivisions in the vertical direction for the curtain.
Grid resolution for sampling the surface
z = f(x, y).
Color scale for the surface. It may be a plotly scale name, a single color, or a vector of colors defining a gradient.
Numeric opacity for the surface, between 0 and 1.
Logical; if TRUE, overlays grid lines on
the surface.
Character string giving the color of surface grid lines.
Numeric width of surface grid lines.
Numeric value between 0 and 1 giving the opacity of the vertical curtain. A value of zero disables the curtain.
Color scale for the curtain, using the same
formats accepted by colorscale.
Color for the curve drawn at height zero.
Width of the curve drawn at height zero.
Color for the lifted curve whose height is
f(x(t), y(t)).
Width of the lifted curve.
Step size used for centered finite differences when computing
the derivative of r(t). If NULL, a default is chosen
automatically.
Integration method; may be "adaptive" (using
stats::integrate) or "simpson" (using a composite Simpson
rule).
Number of subintervals for the Simpson method. This value is automatically adjusted to be even.
List configuring the 3D scene in plotly.
List with background color settings for the figure, with entries
such as paper and plot.
The function evaluates the scalar field along the curve and computes an
approximation of the derivative of r(t) using centered finite
differences. The integral can be computed either through a built-in
adaptive integration routine or via a composite Simpson rule with a user
specified number of subintervals.
For visualization, the function:
builds a rectangular surface z = f(x, y) using only the
endpoints of the curve,
plots the curve in the plane,
plots a lifted copy of the curve where the height is given by the scalar field,
optionally constructs a vertical curtain over the curve by extruding the height values.
f <- function(x, y) x^2 + y^2
r <- function(t) c(t*cos(t), t*sin(3*t))
line_integral2d(f, r, a = 0, b = 2*pi, plot = FALSE)
Run the code above in your browser using DataLab