Learn R Programming

NMOF (version 0.20-0)

xwGauss: Integration of Gauss-type

Description

Compute nodes and weights for Gauss integration.

Usage

xwGauss(n, method = "legendre")
changeInterval(nodes, weights, oldmin, oldmax, newmin, newmax)

Arguments

n
number of nodes
method
character. default is "legendre"; also possible are "laguerre" and "hermite"
nodes
the nodes (a numeric vector)
weights
the weights (a numeric vector)
oldmin
the minimum of the interval (typically as tabulated)
oldmax
the maximum of the interval (typically as tabulated)
newmin
the desired minimum of the interval
newmax
the desired maximum of the interval

Value

  • a list with two elements
  • weightsa numeric vector
  • nodesa numeric vector

Details

xwGauss computes nodes and weights for integration for the interval -1 to 1. It uses the method of Golub and Welsch (1969). changeInterval is a utility that transforms nodes and weights to an arbitrary interval.

References

Gilli, M., Maringer, D. and Schumann, E. (2011) Numerical Methods and Optimization in Finance. Elsevier. http://www.elsevierdirect.com/product.jsp?isbn=9780123756626 Golub, G.H. and Welsch, J.H. (1969). Calculation of Gauss Quadrature Rules. Mathematics of Computation, 23(106), pp. 221--230+s1--s10.

See Also

callHestoncf

Examples

Run this code
## examples from Gilli/Maringer/Schumann (2011), ch. 15

## a test function
f1 <- function(x) exp(-x)
m <- 5; a <- 0; b <- 5
h <- (b - a)/m

## rectangular rule -- left
w <- h; k <- 0:(m-1); x <- a + k * h
sum(w * f1(x))

## rectangular rule -- right
w <- h; k <- 1:m ; x <- a + k * h
sum(w * f1(x))

## midpoint rule
w <- h; k <- 0:(m-1); x <- a + (k + 0.5)*h
sum(w * f1(x))

## trapezoidal rule
w <- h
k <- 1:(m-1)
x <- c(a, a + k*h, b)
aux <- w * f1(x)
sum(aux) - (aux[1] + aux[length(aux)])/2

## R's integrate (from package stats)
integrate(f1, lower = a,upper = b)

## Gauss--Legendre
temp <- xwGauss(m)
temp <- changeInterval(temp$nodes, temp$weights,
                       oldmin = -1, oldmax = 1, newmin =  a, newmax = b)
x <- temp$nodes; w <- temp$weights
sum(w * f1(x))

Run the code above in your browser using DataLab