Learn R Programming

vectorialcalculus (version 1.0.5)

divergence_field: Numerical divergence of a vector field

Description

Computes the divergence of a vector field at a given point using central finite differences. The vector field field must take a numeric vector x and return a numeric vector of the same length.

Usage

divergence_field(field, x0, h = NULL, plot = FALSE)

Value

A numeric scalar: the divergence evaluated at x0.

Arguments

field

Function of the form field(x) returning a numeric vector of the same length as x.

x0

Numeric vector giving the evaluation point.

h

Step size for finite differences. Can be:

  • NULL: an automatic step is selected as 1e-4 * (1 + abs(x0));

  • A numeric scalar: same step for all components;

  • A numeric vector of the same length as x0.

plot

Logical; if TRUE and the dimension is 2 or 3, a basic visualization is drawn with plotly.

Details

Optionally, if the dimension is 2 or 3 and plot = TRUE, a simple visualization is produced using plotly.

Examples

Run this code
field <- function(x) c(x[1] + x[2], x[2] - x[1])
divergence_field(field, c(0.5, -0.2))

Run the code above in your browser using DataLab