1) Separate into two parts Sum the integral from retensi
to a
plus the integral from a
to Inf for an a
that works. We can find such as a
by trying 10^i for i = 7, 8, ... The code stops at the first a
that works.
retensi <- 1136074
b <- 1/1.230219e-07
sx <- function(x) exp(-x/b)
for(a in 10^(7:12)) {
res <- integrate(sx, a, Inf, stop.on.error = FALSE)
if (res$message == "OK") break
}
a
## [1] 1e+09
res
## 5.21431e-51 with absolute error < 9.7e-51
so at a value of a = 10^9 the integral from a
to Inf
is essentially zero so we can just calculate the integral from retensi
to a = 10^9
res <- integrate(sx, retensi, a); res
## 7068377 with absolute error < 0.0044
2) Symbolic integration We can calculate the integral symbolically
library(Ryacas0)
x <- Sym("x")
b <- Sym("b")
Integrate(exp(-x/b), x)
## yacas_expression(-(exp(-x/b) * b))
and using that symbolic result
b <- 1/1.230219e-07
retensi <- 1136074
(-exp(-Inf/b) * b) - (-exp(-retensi/b) * b)
## [1] 7068377
3) Change of variables Another method is to use a change of variables replacing x with a function that goes to infinity when the input to that function goes to some finite value. Trying x = tan(y) we have dx = dy/cos(y)^2:
D(quote(tan(y)), "y")
## 1/cos(y)^2
The answer below checks with the above two.
b <- 1/1.230219e-07
retensi <- 1136074
sx <- function(x) exp(-x/b)
sy <- function(y) sx(tan(y)) / cos(y)^2
integrate(sy, atan(retensi), pi/2)
## 7068377 with absolute error < 15