https://github.com/cran/pracma
Raw File
Tip revision: 708a2ad382a163d1eef5af0665e3ae2aad200ced authored by HwB on 21 March 2013, 00:00:00 UTC
version 1.4.5
Tip revision: 708a2ad
quad2d.R
##
##  q u a d 2 d . R  and  q u a d v .R
##


quad2d <- function(f, xa, xb, ya, yb, n = 32, ...) {
    stopifnot(is.numeric(xa), length(xa) == 1, is.numeric(ya), length(ya) == 1,
              is.numeric(xb), length(xb) == 1, is.numeric(yb), length(yb) == 1)

    fun <- match.fun(f)
    f <- function(x, y) fun(x, y, ...)

    # Get Gauss-Legendre nodes and weights in x- and y-direction.
    cx <- gaussLegendre(n, xa, xb)
    x  <- cx$x
    wx <- cx$w
    cy <- gaussLegendre(n, ya, yb)
    y  <- cy$x
    wy <- cy$w

    # Compute function f at all nodes in x- and y-direction
    mgrid <- meshgrid(x, y)
    Z <- f(mgrid$X, mgrid$Y)

    Q <- wx %*% Z %*% as.matrix(wy)
    return(Q[,])
}
back to top