quad2d | R Documentation |
Two-dimensional Gaussian Quadrature.
quad2d(f, xa, xb, ya, yb, n = 32, ...)
f |
function of two variables; needs to be vectorized. |
xa, ya |
lower limits of integration; must be finite. |
xb, yb |
upper limits of integration; must be finite. |
n |
number of nodes used per direction. |
... |
additional arguments to be passed to |
Extends the Gaussian quadrature to two dimensions by computing two sets of nodes and weights (in x- and y-direction), evaluating the function on this grid and multiplying weights appropriately.
The function f
needs to be vectorized in both variables such that
f(X, Y)
returns a matrix when X
an Y
are matrices
(of the same size).
quad
is not suitable for functions with singularities.
A single numerical value, the computed integral.
The extension of Gaussian quadrature to two dimensions is obvious, but see also the example ‘integral2d.m’ at Nick Trefethens “10 digits 1 page”.
Quarteroni, A., R. Sacco, and F. Saleri (2007). Numerical Mathematics. Second Edition, Springer-Verlag, Berlin Heidelberg.
quad
, cubature::adaptIntegrate
## Example: f(x, y) = (y+1)*exp(x)*sin(16*y-4*(x+1)^2)
f <- function(x, y)
(y+1) * exp(x) * sin(16*y-4*(x+1)^2)
# this is even faster than cubature::adaptIntegral():
quad2d(f, -1, 1, -1, 1)
# 0.0179515583236958 # true value 0.01795155832370
## Volume of the sphere: use polar coordinates
f0 <- function(x, y) sqrt(1 - x^2 - y^2) # for x^2 + y^2 <= 1
fp <- function(x, y) y * f0(y*cos(x), y*sin(x))
quad2d(fp, 0, 2*pi, 0, 1, n = 101) # 2.09439597740074
2/3 * pi # 2.0943951023932
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.