【问题标题】:How to solve "non-numeric argument.." error in numerical integration?如何解决数值积分中的“非数值参数..”错误?
【发布时间】:2021-05-14 17:08:18
【问题描述】:

我想在 R 中计算以下积分。

我尝试使用 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

【问题讨论】:

    标签: r integral numerical-integration


    【解决方案1】:

    第一个积分必须由 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
    

    【讨论】:

      【解决方案2】:

      本着Numerical Triple Integration in 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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-11-01
          • 2012-10-19
          • 1970-01-01
          • 1970-01-01
          • 2021-02-18
          • 2018-08-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多