如何解决数值积分中的“非数值参数”错误?

3
我想在R中计算以下积分:

enter image description here

我尝试使用Vectorizeintegrate函数,但是出现错误 Error in (log(z)) * (InIntegl2) : non-numeric argument to binary operator
  fxyz= function(x,y,z) { (x*y*z)+z+x+2*y}
  InIntegl1 = Vectorize(function(x) { integrate(fxyz, 0,5)$value})
  InIntegl2 = Vectorize(function(y) { integrate( InIntegl1, 0,3)$value})
  InIntegl3 = Vectorize(function(z) { integrate((log(z))*(InIntegl2), 2,6)$value})
  Integral = integrate(InIntegl3 , 2, 6)$value
3个回答

4
第一个积分必须按照y和z进行参数化,而第二个积分则按照z进行。然后我们可以进行最后的积分。
int1 <- Vectorize(function(y, z) integrate(fxyz, 0, 5, y = y, z = z)$value)
int2 <- Vectorize(function(z) integrate(int1, 0, 3, z = z)$value)
integrate(function(z) log(z) * int2(z), 2, 6)$value
## [1] 2071.71

3

R中的三重数值积分为灵感

integrate(Vectorize(function(z) { 
    log(z)*integrate(Vectorize(function(y) { 
        integrate(function(x) { x*y*z +x + 2*y + z}, 0, 5)$value }), 0,3)$value }), 2,6)

3

使用cubature软件包,只需要一个函数调用即可解决三重积分问题。

library(cubature)

f <- function(X){
  x <- X[1]
  y <- X[2]
  z <- X[3]
  log(z)*(x*y*z + x+ 2*y + z)
}
loLim <- c(0, 0, 2)
hiLim <- c(5, 3, 6)
tol <- .Machine$double.eps^0.5

hcubature(f, loLim, hiLim, tol = tol)
#$integral
#[1] 2071.71
#
#$error
#[1] 2.059926e-05
#
#$functionEvaluations
#[1] 165
#
#$returnCode
#[1] 0

如果只需要积分的值,

hcubature(f, loLim, hiLim, tol = tol)$integral
#[1] 2071.71

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接