Leonard Mada
2023-Jan-08 03:24 UTC
[R] Problem with integrate(function(x) x^3 / sin(x), -pi/2, pi/2)
Dear List-Members, I encounter a problem while trying to integrate the following function: integrate(function(x) x^3 / sin(x), -pi/2, pi/2) # Error in integrate(function(x) x^3/sin(x), -pi/2, pi/2) : #? non-finite function value # the value should be finite: curve(x^3 / sin(x), -pi/2, pi/2) integrate(function(x) x^3 / sin(x), -pi/2, 0) integrate(function(x) x^3 / sin(x), 0, pi/2) # but this does NOT work: integrate(function(x) x^3 / sin(x), -pi/2, pi/2, subdivisions=4096) integrate(function(x) x^3 / sin(x), -pi/2, pi/2, subdivisions=4097) # works: integrate(function(x) x^3 / sin(x), -pi/2, pi/2 + 1E-10) # Note: works directly with other packages pracma::integral(function(x) x^3 / sin(x), -pi/2, pi/2 ) # 3.385985 I hope that integrate() gets improved in base R as well. Sincerely, Leonard
Andrew Simmons
2023-Jan-08 03:32 UTC
[R] Problem with integrate(function(x) x^3 / sin(x), -pi/2, pi/2)
You're dividing 0 by 0, giving you NaN, perhaps you should try function(x) ifelse(x == 0, 0, x^3/sin(x)) On Sat, Jan 7, 2023, 22:24 Leonard Mada via R-help <r-help at r-project.org> wrote:> Dear List-Members, > > I encounter a problem while trying to integrate the following function: > > integrate(function(x) x^3 / sin(x), -pi/2, pi/2) > # Error in integrate(function(x) x^3/sin(x), -pi/2, pi/2) : > # non-finite function value > > # the value should be finite: > curve(x^3 / sin(x), -pi/2, pi/2) > integrate(function(x) x^3 / sin(x), -pi/2, 0) > integrate(function(x) x^3 / sin(x), 0, pi/2) > # but this does NOT work: > integrate(function(x) x^3 / sin(x), -pi/2, pi/2, subdivisions=4096) > integrate(function(x) x^3 / sin(x), -pi/2, pi/2, subdivisions=4097) > # works: > integrate(function(x) x^3 / sin(x), -pi/2, pi/2 + 1E-10) > > > # Note: works directly with other packages > > pracma::integral(function(x) x^3 / sin(x), -pi/2, pi/2 ) > # 3.385985 > > > I hope that integrate() gets improved in base R as well. > > > Sincerely, > > > Leonard > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Ivan Krylov
2023-Jan-08 07:27 UTC
[R] Problem with integrate(function(x) x^3 / sin(x), -pi/2, pi/2)
On Sun, 8 Jan 2023 05:24:05 +0200 Leonard Mada via R-help <r-help at r-project.org> wrote:> pracma::integral(function(x) x^3 / sin(x), -pi/2, pi/2 ) > # 3.385985Note that at least one implementation used by pracma::integral has the same problem: pracma::integral(function(x) x^3/sin(x), -pi/2, pi/2, no_intervals=7) # [1] NaN # Warning: # In .gkadpt(f, a, b, tol = tol) : Infinite or NA function value # encountered. You just have to be less lucky to have it evaluate the function at 0. By default, the subdivision strategy used by pracma::integral combined with the default number of intervals leaves the special point on the edge of the interval, where the function happens not to be evaluated. -- Best regards, Ivan