Hi am trying to compute the following integral in Mathematica:
NIntegrate[(Sign[Cos[q]] Sqrt[Abs[Cos[q]]])/(q+100),{q,0,Infinity}]
But when I run that command I get the following error:
NIntegrate::ncvb: NIntegrate failed to converge to prescribed
accuracy after 9 recursive bisections in q near {q} = {6.3887*10^56}.
NIntegrate obtained -317.031 and 171.60663912222586` for the integral
and error estimates.
Is there a way to solve this issue? Thanks
Answer
Here's a manual implementation of the extrapolating oscillatory strategy. NIntegrate
balks at trying it because the cosine is buried inside other functions.
ClearAll[psum];
psum[i_?NumberQ, opts___] :=
NIntegrate[(Sign[Cos[x]] Sqrt[Abs[Cos[x]]])/(x + 100),
{x, (i + 1/2) π, (i + 3/2) π}, opts];
res = NSum[psum[i], {i, 0, ∞},
Method -> {"AlternatingSigns", Method -> "WynnEpsilon"},
"VerifyConvergence" -> False] +
NIntegrate[(Sign[Cos[x]] Sqrt[Abs[Cos[x]]])/(x + 100), {x, 0, (1/2) π}]
(*
0.00010967(-)
*)
This agrees with Chip Hurst's result to almost 5 digits. But the method is reasonably quick, so we can examine the convergence as the WorkingPrecision
(and therefore the PrecisionGoal
) is raised:
ClearAll[res];
mem : res[wp_: MachinePrecision] := mem =
NSum[psum[i, WorkingPrecision -> wp], {i, 0, ∞},
Method -> {"AlternatingSigns", Method -> "WynnEpsilon"},
"VerifyConvergence" -> False, WorkingPrecision -> wp] +
NIntegrate[(Sign[Cos[x]] Sqrt[Abs[Cos[x]]])/(x + 100),
{x, 0, (1/2) π}, WorkingPrecision -> wp];
Table[res[wp], {wp, 16, 40, 8}]
(*
{0.0001096696058468,
0.000109676059727856341260,
0.00010967605972782927874728238851,
0.0001096760597278292787470847687426733221}
*)
% // Differences
(*
{6.4538811*10^-9, -2.7062513*10^-17, -1.9761977*10^-25}
*)
At each step we extend the number of digits that agree, and we can see that the result seems reliable.
Alternatively, one can multiply and divide by cosine, which enables NIntegrate
to use the "ExtrapolatingOscillatory"
strategy. It's a bit slower because it introduces (integrable) singularities in the non-oscillatory part of the integrand, but again we get similar results.
ClearAll[res2];
mem : res2[wp_: MachinePrecision] := mem =
NIntegrate[Cos[q] (1/Sqrt[Abs[Cos[q]]])/(q + 100), {q, 0, Infinity},
Method -> "ExtrapolatingOscillatory",
MaxRecursion -> Ceiling[10 + 1.5 wp], (* increase in MaxRecursion for singularities *)
WorkingPrecision -> wp]
res2[]
(*
0.000109673
*)
Table[res2[wp], {wp, 16, 40, 8}]
(*
{0.0001096731867831,
0.000109676059727866251185,
0.00010967605972782927874731895632,
0.0001096760597278292787470847687424806474}
*)
Comments
Post a Comment