Computing Triple Integral in 'R'

Click For Summary
SUMMARY

The discussion focuses on computing the triple integral of a function of three variables, specifically using the R packages Cubature, Base, and SimplicialCubature. The integrand is defined as 1 within the domain where x < y < z and 0 otherwise. The results from the Cubature package yield an integral value of approximately 0.1664146, while the Base package provides a value of approximately 0.1666632, and the SimplicialCubature package gives 0.1666667. The participants clarify the limits of integration and the definition of the domain, emphasizing that the integration is effectively calculating the volume of the simplex.

PREREQUISITES
  • Understanding of triple integrals in calculus
  • Familiarity with R programming language
  • Knowledge of R packages: Cubature, Base, and SimplicialCubature
  • Concept of defining integration limits and domains
NEXT STEPS
  • Explore the R package Cubature for advanced numerical integration techniques
  • Learn about defining and working with integration domains in R
  • Study the mathematical principles behind triple integrals and their applications
  • Investigate the differences between various R integration packages, including their performance and accuracy
USEFUL FOR

Mathematicians, data scientists, and R programmers who are interested in numerical integration techniques and the application of triple integrals in computational problems.

WMDhamnekar
MHB
Messages
378
Reaction score
30
I would like to compute the triple integral of a function of three variables $f(x,y,z)$in R. I am using the package Cubature, Base, SimplicialCubature and the function adaptIntegrate(), Integrate and adaptIntegrateSimplex(). The integrand is equal to 1 only in certain domain(x<y<z, 0 otherwise).
Followings are the different ways to answer this question but i didn't understand how to compute the answer 0.166666 manually. If any member knows the answer, may reply. Using Cubature package,

library(cubature)
lower <- rep(0,3)
upper <- rep(1,3)

fxyz <- function(w) {
x <- w[1]
y <- w[2]
z <- w[3]
as.numeric(x <= y)*as.numeric(y <= z)
}
adaptIntegrate(f=fxyz,lowerLimit=lower,upperLimit= upper,doChecking=TRUE, maxEval=2000000,absError=10e-5,tol=1e-5)
\$integral
[1]0.1664146

\$error
[1]0.0001851699

\$functionEvaluations
[1]2000031

\$returnCode
[1]0

Using base package in 'R'
f.xyz <- function(x, y, z) ifelse(x < y & y < z, 1, 0)
f.yz <- Vectorize(function(y, z) integrate(f.xyz, 0, 1, y=y, z=z)\$value,
vectorize.args="y")

f.z <- Vectorize(function(z) integrate(f.yz, 0, 1, z=z)\$ value,
vectorize.args="z")
integrate(f.z, 0, 1)
>0.1666632 with absolute error < 9.7e-05

Using SimpicialCubature package in 'R'
library(SimplicialCubature)
f <- function(x) 1
S <- CanonicalSimplex(3)
> adaptIntegrateSimplex(function(x) 1, S)
\$integral
[1] 0.1666667

\$estAbsError
[1] 1.666667e-13

\$functionEvaluations
[1] 55

$returnCode
[1] 0

\$message
[1] "OK"
Note that integrating the constant function f(x)=1 over the simplex simply gives the volume of the simplex, which is 1/6. The integration is useless for this example

SimplexVolume(S)
[1] 0.1666667

Can any member explain me what is this actual question and how we can solve this question manually?
 
Physics news on Phys.org
I don't know "Cubature" but I will reply anyway!

First, your domain, x< y< z, is not well defined. The simple "x< y< z" is infinite and the integral of "1" on that domain is not finite. Is there not some other condition bounding the domain? In your program you have "lower<- rep(0,3)" and "upper<- rep(1,3)". Could that possibly mean that x lies between 0,3 and 1,3 (I would have said 0.3< z< 1.3)?
 
HallsofIvy said:
I don't know "Cubature" but I will reply anyway!

First, your domain, x< y< z, is not well defined. The simple "x< y< z" is infinite and the integral of "1" on that domain is not finite. Is there not some other condition bounding the domain? In your program you have "lower<- rep(0,3)" and "upper<- rep(1,3)". Could that possibly mean that x lies between 0,3 and 1,3 (I would have said 0.3< z< 1.3)?

Hello,
lower<-rep(0,3) means in adaptIntegrate function lower limit 0,repeats 3 times and upper<-(1,3) means upper limit 1, repeats 3 times.
e.g.$\displaystyle\int_0^1\displaystyle\int_0^1\displaystyle\int_0^1$
 
Dhamnekar Winod said:
Hello,
lower<-rep(0,3) means in adaptIntegrate function lower limit 0,repeats 3 times and upper<-(1,3) means upper limit 1, repeats 3 times.
e.g.$\displaystyle\int_0^1\displaystyle\int_0^1\displaystyle\int_0^1$
That's strange. What would happen if you entered different number of repetitions for the the lower limits and upper limits? Also \int_0^1\int_0^1\int_0^1 doesn't match your "x< y< z".
 
HallsofIvy said:
That's strange. What would happen if you entered different number of repetitions for the the lower limits and upper limits? Also \int_0^1\int_0^1\int_0^1 doesn't match your "x< y< z".

Hello,
Because of the following programming command while using 'Cubature' package in 'R', adaptIntegrate function gives answer 0.1664146.
fxyz <- function(w) {
x <- w[1]
y <- w[2]
z <- w[3]
as.numeric(x <= y)*as.numeric(y <= z)
}

This programming command seems to be difficult to understand.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K