r - 集成错误 : maximum number of subdivisions reached

标签 r plot numerical-integration

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

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 给了我正常的结果。
你能提出一些建议吗?

最佳答案

在超过 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))

enter image description here

增加耐受性:
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))

enter image description here

关于r - 集成错误 : maximum number of subdivisions reached,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23982230/

相关文章:

python - 使用 matplotlib 堆积条形图

python - scipy.integrate.fixed_quad 可以计算具有函数边界的积分吗?

R:提供 5 秒钟要求暂停。如果不需要暂停,则继续该过程

r - 将具有不等长元素的列表转换为 R 中的两列数据框(首选 dplyr)

python - Pandas 数据帧的视觉探索

android - 如何在 Android Graph 中绘制实时传感器值?

Python:计算泰勒级数的误差

c++ - 自适应正交 (C++)

r - R中的二维彩色图

r - 将(df 的)每一列的唯一值存储在列表中