# Integral in [-1,1] of 2x-1.
# Value is -2 and n=1 is enough for exact result
# Define the function
f <- function(x) {ff <- 2*x-1; return(ff)}
# 1-point quadrature
ltmp <- Gquad(f,-1,1,n=1)
# The only zero is x1=0
print(ltmp$xt)
# The only weight is w1=2
print(ltmp$wt)
# Quadrature gives exact integral
print(ltmp$itg)
# 2-point quadrature
ltmp <- Gquad(f,-1,1,n=2)
print(ltmp) # Same result but more zeros and weights
# Default, n=5, is accurate enough
ltmp <- Gquad(exp,-1,1)
print(ltmp$itg)
# Different extremes of integration
ltmp <- Gquad(exp,1,4)
print(ltmp$itg)
Run the code above in your browser using DataLab