Sum of One-Dimensional Functions

add(f, lower, upper, ..., abs.tol = .Machine$double.eps)

Arguments

f

an R function taking a numeric first argument and returning a numeric vector of the same length.

lower

the lower limit of sum. Can be infinite.

upper

the upper limit of sum. Can be infinite.

...

additional arguments to be passed to f.

abs.tol

absolute accuracy requested.

Value

This function returns the sum value.

Author

Freddy Hernandez, fhernanb@unal.edu.co

Examples

# Poisson expected value
add(f=function(x, lambda) x*dpois(x, lambda), lower=0, upper=Inf,
    lambda=7.5)
#> $value
#> [1] 7.5
#> 
#> $abs.error
#> [1] 0
#> 

# Binomial expected value
add(f=function(x, size, prob) x*dbinom(x, size, prob), lower=0, upper=20,
    size=20, prob=0.5)
#> $value
#> [1] 10
#> 
#> $abs.error
#> [1] 0
#> 

# Examples with infinite series
add(f=function(x) 0.5^x, lower=0, upper=100) # Ans=2
#> $value
#> [1] 2
#> 
#> $abs.error
#> [1] 0
#> 
add(f=function(x) (1/3)^(x-1), lower=1, upper=Inf) # Ans=1.5
#> $value
#> [1] 1.5
#> 
#> $abs.error
#> [1] 2.435019e-144
#> 
add(f=function(x) 4/(x^2+3*x+2), lower=0, upper=Inf, abs.tol=0.001) # Ans=4.0
#> $value
#> [1] 3.986799
#> 
#> $abs.error
#> [1] 4.371298e-05
#> 
add(f=function(x) 1/(x*(log(x)^2)), lower=2, upper=Inf, abs.tol=0.000001) # Ans=2.02
#> $value
#> [1] 2.002743
#> 
#> $abs.error
#> [1] 9.999232e-07
#> 
add(f=function(x) 3*0.7^(x-1), lower=1, upper=Inf) # Ans=10
#> $value
#> [1] 10
#> 
#> $abs.error
#> [1] 7.1061e-47
#> 
add(f=function(x, a, b) a*b^(x-1), lower=1, upper=Inf, a=3, b=0.7) # Ans=10
#> $value
#> [1] 10
#> 
#> $abs.error
#> [1] 7.1061e-47
#> 
add(f=function(x, a=3, b=0.7) a*b^(x-1), lower=1, upper=Inf) # Ans=10
#> $value
#> [1] 10
#> 
#> $abs.error
#> [1] 7.1061e-47
#>