rerror-handlingnumerical-methodsnumerical-integration

Best way to handle numerical integration error iteratively in R?


Consider the following numeric integral in R:

integrand = function(u){log(u) * u ^ (2 * (H - 1)) * (exp(a * u) - exp(- a * u))}
integrate(f = integrand, lower = 0, upper = 100,
              rel.tol = .Machine$double.eps ^ 0.01, subdivisions = 1e6)

Where H = 0.1, a = 0.1, for example. This is divergent due to lower = 0. I would like to progressively increase the lower bound from 0 to 1e-13` and progressively by one decimal at a time until convergence. Which code would you suggest to use? I am not familiar with error handling.


Solution

  • Thanks for providing the values for H and a, I adapted the example now. I recommend you use the calculus package. Here is a sample snippet of how to use it, including the values you provide. I now also include the upper bound that you provided. The default method seems to have issues, therefore I suggest you try different ones. You will have to install cubature for that purpose.

    Here is the reference: https://cran.r-project.org/web/packages/calculus/index.html

    library(calculus)
    
    integrand <- function(u, H = 0.1, a = 0.1) {
      log(u) * u^(2 * (H - 1)) * (exp(a * u) - exp(-a * u))
    }
    
    # Different methods that converge, examples
    integral(integrand, bounds = list(u = c(0, 100)),
             method = "hcubature")$value
    integral(integrand, bounds = list(u = c(0, 100)),
             method = "suave")$value
    integral(integrand, bounds = list(u = c(0, 100)),
             method = "vegas")$value