Integration in R

I want to integrate exp (-x) from 0 to 100000 using the integration function in R. However, I find that the answer has a value of 2.061453e-45, which is almost 0 (zero). The true answer will be 1-exp (-100000), which is almost equal to 1. How to perform this integration using the integration function in R to get closer to the correct solution?

Below is the R code used

ab<-function(x) { return(exp(-x)) }
integrate(ab,0,100000)$value

Output signal

 2.061453e-45
+4
source share
2 answers

This is a limitation of the numerical integration method used in R at large intervals. Function documentation indicates that:integrate

, . - , , .

exp(-x) x=100000 ( 3.56 × 10^-43430), , , ,

integrate( ab , 0 , Inf)

,

1
+3

@Aziz.

integrate -

subdivisions: , .

- 100. I.e. 100 0 100000. , - x = 0. , - 0 10, 10 20 20 100000 - x = 0.

> integrate(ab,0,10)$value 
[1] 0.9999546
> integrate(ab,10,20)$value
[1] 4.539787e-05
> integrate(ab,20,100000)$value
[1] 4.341375e-54
> 
> integrate(ab,0,10)$value + integrate(ab,10,20)$value + integrate(ab,20,100000)$value
[1] 1
+1

Source: https://habr.com/ru/post/1694213/


All Articles