Learn R Programming

vectorialcalculus (version 1.0.5)

line_integral2d: Line integral of a scalar field along a planar curve, with optional 3D visualization

Description

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.

Usage

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")
)

Value

A list with:

  • value: the numeric value of the line integral,

  • fig: a plotly figure when plot = TRUE, or NULL otherwise.

Arguments

f

A scalar field, given as function(x, y) returning a numeric value.

r

A parametric curve, given as function(t) that returns a numeric vector of length two, interpreted as c(x, y).

a, b

Numeric scalars giving the parameter limits, with b > a.

plot

Logical; if TRUE, produces a visualization using plotly.

n_curve

Number of sample points along the curve for plotting.

n_curtain_v

Number of subdivisions in the vertical direction for the curtain.

n_surf_x, n_surf_y

Grid resolution for sampling the surface z = f(x, y).

colorscale

Color scale for the surface. It may be a plotly scale name, a single color, or a vector of colors defining a gradient.

surface_opacity

Numeric opacity for the surface, between 0 and 1.

show_surface_grid

Logical; if TRUE, overlays grid lines on the surface.

surface_grid_color

Character string giving the color of surface grid lines.

surface_grid_width

Numeric width of surface grid lines.

curtain

Numeric value between 0 and 1 giving the opacity of the vertical curtain. A value of zero disables the curtain.

curtain_colorscale

Color scale for the curtain, using the same formats accepted by colorscale.

curve_color

Color for the curve drawn at height zero.

curve_width

Width of the curve drawn at height zero.

lifted_color

Color for the lifted curve whose height is f(x(t), y(t)).

lifted_width

Width of the lifted curve.

h

Step size used for centered finite differences when computing the derivative of r(t). If NULL, a default is chosen automatically.

method

Integration method; may be "adaptive" (using stats::integrate) or "simpson" (using a composite Simpson rule).

n_simpson

Number of subintervals for the Simpson method. This value is automatically adjusted to be even.

scene

List configuring the 3D scene in plotly.

bg

List with background color settings for the figure, with entries such as paper and plot.

Details

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.

Examples

Run this code
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