Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating error: maximum number of subdivisions reached

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?

like image 284
Eugene Kolesnikov Avatar asked Jun 01 '14 16:06

Eugene Kolesnikov


1 Answers

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))

enter image description here

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))

enter image description here

like image 129
Matthew Lundberg Avatar answered Nov 17 '22 16:11

Matthew Lundberg