Calculating Definite Integrals in R
📂RCalculating Definite Integrals in R
Overview
In R, you can use the integrate()
function to calculate definite integrals. For example,
Code
To calculate ∫03(x2+4x+1)dx and ∫0∞e−xdx, you can use the following. Notably, by including inf
in the integration interval, it is possible to perform improper integrals as well.
f<-function(x) {x^2 + 4*x + 1}
g<-function(x) {exp(-x)}
integrate(f,0,3)
integrate(g,0,Inf)

Upon actual calculation,
∫03(x2+4x+1)dx=[31x3+2x2+x]x=03=9+18+3=30
and
∫0∞e−xdx=[−e−x]x=0∞=0−(−1)=1
can be confirmed.
See Also