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
```

- Installing R on Linux: configure: error: libcurl >= 7.28.0 library and headers are required with support for https
- How to do ensembles with time series using AICc?
- planes3d expands and draws the area based on the sphere's radius
- How to extract tag code itself using R, rvest
- How to Display or Print Contents of Environment in R
- How to use Windows user credentials for proxy authentication in R/RStudio
- R reticulate specifying python executable to use
- Replace multiple Instances of a variable name in an R function and save the modified function
- Standardizing address formatting in R
- How to fix "failed to load cairo DLL" in R?
- Using grepl to filter columns names in specific range of columns
- changing the legends in ggplot2 to have groups of similar labels
- How to keep only unique rows but ignore a column?
- convert string date to R Date FAST for all dates
- Add subgroup text to plotly pie chart
- R Shiny : adjust height of DT datatable when fillContainer=TRUE,
- Why do R external pointers' "unusual copying semantics" mean they should not be used stand-alone?
- How to extract somo character after a string with a number of word which can change in R
- What does `se` stand for in geom_smooth(..., se = FALSE)?
- How to find number of rows greater than any values in R
- Align text and reduce space between text and parentheses in plotly hover info box
- Remove outer box of geom_bar plot with broken y-axis
- How to use lag/lead in mutate with an initial value?
- Is it possible to have a Shiny ConditionalPanel whose condition is a global variable?
- counting elements in one list in another list
- How to vectorize nested loops in R?
- Replace NA values with an incrementing sequence starting from the previous non-NA value
- How can I calculate the number of uniques in a row within a species matrix?
- How to perform operations on pairs of rows, based on a "distinguishing" column's values
- Mutate variable based on previous observations