# Why can I not numerically integrate a smooth, integrable function?

Consider the function $$\csc(t)\sin(3t)$$ on the interval $$[0,\pi]$$. This is a perfectly well-behaved smooth function, as can be seen when plotting it:

Plot[2 Csc[t] Sin[3 t], {t, 0, π}]


so why cannot I not numerically integrate it by running the following?

NIntegrate[2 Csc[t] Sin[3 t], {t, 0, π}]


Instead I get the error message "NIntegrate::errprec: Catastrophic loss of precision in the global error estimate due to insufficient WorkingPrecision or divergent integral". FYI I expect the answer to be $$2\pi$$, as can be seen by running

Integrate[2 Csc[t] Sin[3 t], {t, 0, π}]


Thanks in advance for any help.

• Round-off error (from the asymptotes of Csc[t]). Following the advice in the error message, try it wth WorkingPrecision -> 16. Nov 27, 2021 at 19:25
• Or try NIntegrate[2 Csc[t] Sin[3 t] // TrigExpand, {t, 0, \[Pi]}] Nov 27, 2021 at 19:34
• In some sense, NIntegrate selects a bad rule (Clenshaw-Curtis osciilatory). Try NIntegrate[2 Csc[t] Sin[3 t], {t, 0, \[Pi]}, Method -> "GaussKronrodRule"] Nov 27, 2021 at 19:43
• Or use Integrate rather than NIntegrate Nov 27, 2021 at 20:39
• I am certain this is a toy example, but for the integral as presented by the OP, reformulating the integrand does the trick: NIntegrate[2 ChebyshevU[2, Cos[t]], {t, 0, π}] Dec 9, 2021 at 20:31

Maybe my last comment makes it worth posting my comments as an answer. The first one shows that the error message contains a hint at a fix, and one naturally would try it out first. The second one seems a common-sense thing to try. For the last one I had to take a look into what NIntegrate was doing.

Round-off error (from the asymptotes of Csc[t]). Following the advice in the error message, try it wth WorkingPrecision -> 16. – Michael E2 18 mins ago

Or try NIntegrate[2 Csc[t] Sin[3 t] // TrigExpand, {t, 0, \[Pi]}] – Michael E2 9 mins ago

In some sense, NIntegrate selects a bad rule (Clenshaw-Curtis oscillatory). Try NIntegrate[2 Csc[t] Sin[3 t], {t, 0, \[Pi]}, Method -> "GaussKronrodRule"] – Michael E2 52 secs ago

Method -> "LevinRule" also works.

• Does anyone have a reference for how Method -> "ClenshawCurtisOscillatoryRule" works? I assume the catastrophic loss of precision comes from some sort of extrapolation to the endpoints where the integrand is undefined (not from a catastrophic loss in Csc[t] per se, which is improbable). Nov 28, 2021 at 16:03
• If memory serves, that one is supposed to be an implementation of the method of Piessens and Branders, first noted here, and then extended here. Dec 9, 2021 at 20:24
• @J.M. Thanks for the references. Dec 10, 2021 at 15:23