3
$\begingroup$

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.

$\endgroup$
6
  • 2
    $\begingroup$ Round-off error (from the asymptotes of Csc[t]). Following the advice in the error message, try it wth WorkingPrecision -> 16. $\endgroup$
    – Michael E2
    Nov 27 '21 at 19:25
  • 2
    $\begingroup$ Or try NIntegrate[2 Csc[t] Sin[3 t] // TrigExpand, {t, 0, \[Pi]}] $\endgroup$
    – Michael E2
    Nov 27 '21 at 19:34
  • 2
    $\begingroup$ In some sense, NIntegrate selects a bad rule (Clenshaw-Curtis osciilatory). Try NIntegrate[2 Csc[t] Sin[3 t], {t, 0, \[Pi]}, Method -> "GaussKronrodRule"] $\endgroup$
    – Michael E2
    Nov 27 '21 at 19:43
  • $\begingroup$ Or use Integrate rather than NIntegrate $\endgroup$
    – Bob Hanlon
    Nov 27 '21 at 20:39
  • $\begingroup$ 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, π}] $\endgroup$ Dec 9 '21 at 20:31
6
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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). $\endgroup$
    – Michael E2
    Nov 28 '21 at 16:03
  • 1
    $\begingroup$ 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. $\endgroup$ Dec 9 '21 at 20:24
  • $\begingroup$ @J.M. Thanks for the references. $\endgroup$
    – Michael E2
    Dec 10 '21 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.