Learn R Programming

vectorialcalculus (version 1.0.5)

line_integral_vector2d: 2D line integral of a vector field with visualization

Description

This function computes a numerical approximation of the line integral of a planar vector field along a parametric curve r(t) on the interval from a to b. The derivative of the curve is approximated by finite differences and the integral is evaluated either by an adaptive numerical integrator or by a composite Simpson rule.

Usage

line_integral_vector2d(
  field,
  r,
  a,
  b,
  plot = TRUE,
  n_curve = 600,
  grid_n = 15,
  padding = 0.15,
  h = NULL,
  method = c("adaptive", "simpson"),
  n_simpson = 1000,
  arrow_scale = 0.08,
  normalize_bias = 1,
  field_color = "rgba(0,0,0,0.55)",
  field_width = 1.8,
  traj_palette = "RdBu",
  traj_width = 5,
  show_markers = FALSE,
  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 components:

  • value: numeric value of the line integral.

  • samples: data frame with sampled points, velocities and power along the trajectory.

  • fig: plotly object when plot = TRUE, otherwise NULL.

Arguments

field

Vector field in the plane. A function function(x, y) that returns a numeric vector of length 2 c(Fx, Fy).

r

Parametric curve in the plane. A function function(t) that returns a numeric vector of length 2 c(x, y).

a

Numeric scalar. Left endpoint of the parameter interval.

b

Numeric scalar. Right endpoint of the parameter interval. Must satisfy b > a.

plot

Logical. If TRUE, an interactive plotly figure is created with the field and the curve.

n_curve

Integer. Number of parameter values used to sample the curve.

grid_n

Integer. Number of grid points per axis used to draw the vector field arrows.

padding

Numeric scalar. Relative margin added around the bounding box of the curve when building the field grid.

h

Numeric scalar or NULL. Step size used in the finite difference approximation of the derivative of the curve. If NULL, a small step is chosen automatically, based on the length of the interval b - a.

method

Character string. Integration method for the line integral. One of "adaptive" (uses stats::integrate) or "simpson" (composite Simpson rule).

n_simpson

Integer. Number of subintervals used when method = "simpson". If it is odd, it is increased by one internally.

arrow_scale

Numeric scalar. Controls the overall length of the field arrows as a fraction of the plot span.

normalize_bias

Numeric scalar. Saturation parameter used to avoid extremely long arrows for large field magnitudes.

field_color

Character string. Color used for the field arrows.

field_width

Numeric scalar. Line width for the field arrows.

traj_palette

Color scale used to represent the power along the trajectory. Passed to plotly as a colorscale name.

traj_width

Numeric scalar. Line width for the trajectory.

show_markers

Logical. If TRUE, markers are drawn along the trajectory in addition to the line.

scene

List with plotly scene options (axis titles, aspect mode, etc.) passed to plotly::layout().

bg

List with background colors for plotly, with components paper and plot.

Details

Optionally, the function can build an interactive plotly figure that shows a grid of arrows for the vector field and the curve colored by the local power field(r(t)) * r'(t).

Examples

Run this code
# Simple example:
# field(x, y) = (y, -x), r(t) = (cos t, sin t), t in [0, 2*pi]
line_integral_vector2d(
  field = function(x, y) c(y, -x),
  r = function(t) c(cos(t), sin(t)),
  a = 0, b = 2*pi, plot = FALSE
)

Run the code above in your browser using DataLab