[R] Problem with integrate(function(x) x^3 / sin(x), -pi/2, pi/2)

Andrew Simmons @kw@|mmo @end|ng |rom gm@||@com
Sun Jan 8 04:32:25 CET 2023


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 using 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 using 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]]



More information about the R-help mailing list