R中的数值三重积分

问题描述 投票:2回答:2

是否可以在不使用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

enter image description here

我试过了

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)
r integral numerical-integration
2个回答
4
投票

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

Triple Integral

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

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。

引用前一篇文章的扩展

这是一个将过程从前一个帖子扩展到三元组的示例。我将计算的示例很容易分析,因此我们可以检查我们是否得到了正确的答案。我会用:

Triple Integral

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

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

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


0
投票

对于这样一个整体,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
© www.soinside.com 2019 - 2024. All rights reserved.