xwGauss {NMOF} | R Documentation |
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 |
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 |
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.
Value
a list with two elements
weights |
a numeric vector |
nodes |
a numeric vector |
Author(s)
Enrico Schumann
References
Gilli, M., Maringer, D. and Schumann, E. (2019) Numerical Methods and Optimization in Finance. 2nd edition. Elsevier. doi:10.1016/C2017-0-01621-X
Golub, G.H. and Welsch, J.H. (1969). Calculation of Gauss Quadrature Rules. Mathematics of Computation, 23(106), pp. 221–230+s1–s10.
Schumann, E. (2023) Financial Optimisation with R (NMOF Manual). http://enricoschumann.net/NMOF.htm#NMOFmanual
See Also
Examples
## examples from Gilli/Maringer/Schumann (2019), ch. 17
## 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))