Skip to contents

Computes the definite integral of f over [lower, upper]. The default method, "gauss_kronrod", is the ported Numerics adaptive Gauss-Kronrod rule (10-point Gauss, 21-point Kronrod), which subdivides the interval until the two nested estimates agree to the requested tolerance. Nine other ported methods (P2 "math extras") are available: three more adaptive rules ("simpsons", "trapezoidal", "adaptive_simpsons", "gauss_lobatto") and five fixed rules with no adaptive refinement ("gauss_legendre", "gauss_legendre20", "simpsons_fixed", "trapezoidal_fixed", "midpoint").

Usage

quadrature(
  f,
  lower,
  upper,
  method = c("gauss_kronrod", "simpsons", "trapezoidal", "adaptive_simpsons",
    "gauss_lobatto", "gauss_legendre", "gauss_legendre20", "simpsons_fixed",
    "trapezoidal_fixed", "midpoint"),
  absolute_tolerance = NULL,
  relative_tolerance = NULL,
  max_function_evaluations = NULL,
  steps = NULL,
  min_depth = NULL,
  max_depth = NULL
)

Arguments

f

a function taking one number and returning one number.

lower, upper

the limits of integration. upper must be above lower; neither may be infinite (every one of these rules integrates a finite interval).

method

one of "gauss_kronrod" (the default), "simpsons", "trapezoidal", "adaptive_simpsons", "gauss_lobatto", "gauss_legendre", "gauss_legendre20", "simpsons_fixed", "trapezoidal_fixed", or "midpoint".

absolute_tolerance, relative_tolerance

the convergence tolerances on the difference between the two nested estimates the adaptive methods compare ("gauss_kronrod", "simpsons", "trapezoidal", "adaptive_simpsons", "gauss_lobatto"). Each must lie between 1e-15 and 1. NULL, the default, leaves the ported integrator's own defaults (1e-8) in force. Only apply to the adaptive methods; supplying either for one of the five fixed-rule methods raises an error.

max_function_evaluations

the cap on evaluations of f, for the adaptive methods. Reaching it stops the subdivision and reports it in the status rather than raising an error. NULL, the default, leaves the ported integrator's own default in force. Supplying it for one of the five fixed-rule methods raises an error.

steps

the number of integration steps for method "simpsons_fixed", "trapezoidal_fixed", or "midpoint" alone. NULL, the default, leaves the ported static's own default (2) in force.

min_depth, max_depth

the recursion-depth bounds for method = "adaptive_simpsons" alone. NULL, the default, leaves the ported class's own defaults (0 and 100) in force.

Value

the integral, a single number, carrying three attributes: status, one of "Success", "MaximumFunctionEvaluationsReached", "MaximumIterationsReached", "Failure" or "None" for the five adaptive methods (always "Success" for the five fixed-rule methods, which have no such status of their own); function_evaluations, the number of times f was called (NA for the five fixed-rule methods, whose ported statics report no count); and standard_error, the rule's own error estimate where the driven class has one (zero for "simpsons", "trapezoidal", and "gauss_lobatto", which have none; NA for the five fixed-rule methods).

Details

The name is quadrature() rather than integrate() so it does not mask stats::integrate().

Examples

quadrature(function(x) x^2, lower = 0, upper = 3)
#> [1] 9
#> attr(,"status")
#> [1] "Success"
#> attr(,"function_evaluations")
#> [1] 21
#> attr(,"standard_error")
#> [1] 1.776357e-15
q <- quadrature(sin, lower = 0, upper = pi)
attr(q, "status")
#> [1] "Success"
attr(q, "standard_error")
#> [1] 2.220446e-16
quadrature(function(x) x^3, lower = 0, upper = 1, method = "gauss_lobatto")
#> [1] 0.25
#> attr(,"status")
#> [1] "Success"
#> attr(,"function_evaluations")
#> [1] 18
#> attr(,"standard_error")
#> [1] 0
quadrature(function(x) x^3, lower = 0, upper = 1, method = "midpoint", steps = 1000)
#> [1] 0.2499999
#> attr(,"status")
#> [1] "Success"
#> attr(,"function_evaluations")
#> [1] NA
#> attr(,"standard_error")
#> [1] NA