I am trying to plot a Fourier integral, but I get error while integrating
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))
Error:
maximum number of subdivisions reached
I found out that "cos(l * x)" causes this error but Wolfram gives me normal result. Can you suggest something?
The algorithm is not converging before 100 subdivisions are exceeded. You can increase the number of allowed subdivisions, or increase the tolerance:
More allowed subdivisions:
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))
Increased tolerance:
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))