R 中的数值三重积分

Numerical Triple Integration in R

是否可以在不使用 cubature 包的情况下在 R 中进行三重积分?

based on the answer in this post

InnerFunc = function(x) { x + 0.805 }
InnerIntegral = function(y) { sapply(y, 
    function(z) { integrate(InnerFunc, 15, z)$value }) }
integrate(InnerIntegral , 15, 50)
16826.4 with absolute error < 1.9e-10

例如,编码 this triple integral:

我试过了

InnerMostFunc = function(v) { v + y^2 }
InnerMostIntegral = function(w) { sapply(w, 
   function(x) { integrate(InnerMostFunc, 1, 2)$value }) }
InnerIntegral = function(y) { sapply(y, 
   function(z){integrate(InnerMostIntegral, 1, 2)$value }) }
integrate(InnerIntegral, 0, 1)

再往下是评论中要求的引用 previous post 的扩展。但是这个顶部将展示如何计算更新后 post 中给出的积分。

这比下面的积分类型更难写,因为函数必须完全嵌套。您不能像第二个示例那样将它们分开,因此单个表达式相当长且难以阅读。下面是计算请求积分的代码。

integrate(Vectorize(function(x) { 
    integrate(Vectorize(function(y) { 
        integrate(function(z) { x^2 + y*z }, 1, 2)$value }), 1,2)$value }), 0,1)
2.583333 with absolute error < 2.9e-14

注意它计算出正确答案 31/12。引用的积分来源错误地给出了 31/4 的答案。

引用的前一个扩展 post

这里是将前面post的过程扩展到三重积分的例子。我将计算的示例很容易进行分析,因此我们可以检查我们是否得到了正确的答案。我将使用:

为了更容易理解,我将代码分成几个步骤。

InnerFunc = function(x) { x + 1 }
InnerIntegral = Vectorize(function(y) { integrate(InnerFunc, 0, y)$value})
Integral2     = Vectorize(function(z) { integrate(InnerIntegral , 0, z)$value})
(Tripleintegral = integrate(Integral2 , 0, 4))
21.33333 with absolute error < 2.4e-13

这扩展了前面的示例,但涵盖问题的新陈述中的积分类型。

对于这样的积分,cubature 套餐是最佳选择。

> library(cubature)
> hcubature(function(v) v[1]^2+v[2]*v[3], c(0,1,1), c(1,2,2))
$integral
[1] 2.583333