gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:54:21 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com