【问题标题】:Integrating error: maximum number of subdivisions reached积分误差:达到最大细分数
【发布时间】:2014-07-21 20:16:21
【问题描述】:

我正在尝试绘制傅里叶积分,但在积分时出错

X <- seq(-10, 10, by = 0.05)
f_fourier <- function(X) {
    Y <- sapply(X, function(x) {
        integrand <- function(l) {
            y <- (2 / pi) * cos(l * x) / (l^2 + 1)
        }
        integrate(integrand, lower = 0, upper = Inf)$value
    })
}
plot(X,f_fourier(X))

错误:

maximum number of subdivisions reached

我发现“cos(l * x)”会导致这个错误,但 Wolfram 给了我正常的结果。 你能提出一些建议吗?

【问题讨论】:

    标签: r plot numerical-integration


    【解决方案1】:

    算法在超过 100 个细分之前不会收敛。 您可以增加允许的细分数,或增加容差:

    更多允许的细分:

    f_fourier <- function(X) {
        Y <- sapply(X, function(x) {
            integrand <- function(l) {
                y <- (2 / pi) * cos(l * x) / (l^2 + 1)
            }
            integrate(integrand, lower = 0, upper = Inf, subdivisions=2000)$value
        })
    }
    
    plot(f_fourier(X))
    

    增加容差:

    f_fourier <- function(X) {
        Y <- sapply(X, function(x) {
            integrand <- function(l) {
                y <- (2 / pi) * cos(l * x) / (l^2 + 1)
            }
            integrate(integrand, lower = 0, upper = Inf, rel.tol=.Machine$double.eps^.05)$value
        })
    }
    
    plot(f_fourier(X))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      相关资源
      最近更新 更多