NIntegrate doesn't evaluate
How can I force NIntegrate to calculate?
Below are listed "forcing" answers. They should apply in a wide range of situations with minimal understanding of the integrands.
Diagnosing
First let us look at the messages given by NIntegrate
:
NIntegrate[(t^4 x^3)/Sqrt[-t + x], {x, 0, 1}, {t, 0, x}]
(* During evaluation of In[18]:= NIntegrate::zeroregion: Integration region {{0.5,1},{1.,0.999999999999999999999999999999975153439150570957241015732418974750}} cannot be further subdivided at the specified working precision. NIntegrate assumes zero integral there and on any further indivisible regions. *)
(* During evaluation of In[18]:= NIntegrate::inumri: The integrand (t^4 x^3)/Sqrt[-t+x] has evaluated to Overflow, Indeterminate, or Infinity for all sampling points in the region with boundaries {{0.5,1},{0.999999999999999999999999999999975153439150570957241015732418974750,0.999999999999999999990527764909997233148869688962838439242343509680}}. *)
(* NIntegrate[(t^4 x^3)/Sqrt[-t + x], {x, 0, 1}, {t, 0, x}] *)
The messages NIntegrate::zeroregion
and NIntegrate::inumri
are issued because of the application of the default singularity handler "IMT".
Approaches
1. Using Exclusions
is one alternative.
2. Another alternative is to prevent the singularity handler application (and increase MaxRecursion
.)
NIntegrate[(t^4 x^3)/Sqrt[-t + x], {x, 0, 1}, {t, 0, x},
Method -> {"GlobalAdaptive", "SingularityHandler" -> None,
MaxRecursion -> 120}]
(* 0.0956116 *)
3. A third alternative is to use the tuning parameters for "IMT", if you think "IMT" is beneficial. (Described in NIntegrate
's advanced documentation.)
NIntegrate[(t^4 x^3)/Sqrt[-t + x], {x, 0, 1}, {t, 0, x},
Method -> {"GlobalAdaptive",
"SingularityHandler" -> {"IMT", "TuningParameters" -> 2}}]
(* During evaluation of In[16]:= NIntegrate::zeroregion: Integration region {{0.75,1},{1.,0.999999999999999999999999999925933445985376189112492843112221898520}} cannot be further subdivided at the specified working precision. NIntegrate assumes zero integral there and on any further indivisible regions. *)
(* 0.0956116 *)
4. Use more sampling points per integration region.
5. Related to 4, use higher MinRecursion
:
NIntegrate[(t^4 x^3)/Sqrt[-t + x], {x, 0, 1}, {t, 0, x},
MinRecursion -> 4]
(* 0.0956116 *)
6. Use higher precision.
7. Switch the order of integration.
I think the problem is that the error estimation at the singularity drives the recursive subdivision too far. In addition to the other methods presented, here are some more.
Use a different rule (with a different error estimator):
NIntegrate[(t^4 x^3)/Sqrt[-t + x], {x, 0, 1}, {t, 0, x},
Method -> "GaussKronrodRule"]
(* 0.0956116 *)
Switch the order of integration:
NIntegrate[(t^4 x^3)/Sqrt[-t + x], {t, 0, 1}, {x, t, 1}]
(* 0.0956116 *)
Use a higher working precision:
NIntegrate[(t^4 x^3)/Sqrt[-t + x], {x, 0, 1}, {t, 0, x},
WorkingPrecision -> 16]
(* 0.09561157754126271 *)
Addendum
I feel NIntegrate
should handle the OP's integral without user intervention. The singularity should be easy to identify automatically
and easy to handle computationally.
I think the problem is that for some unknown reason, the singularity is mishandled and that it could possibly be a bug. Here are three "fixes" for which there is absolutely no mathematical or computational grounding that I can imagine:
NIntegrate[(t^4 x^3)/Sqrt[-t + x], {x, 0, 1}, {t, 0, x},
Exclusions -> x == 100] (* x == 100 is way outside the interation region *)
(* 0.0956116 *)
NIntegrate[(t^4 x^3)/Sqrt[-t + x], {x, 0, 1}, {t, 0, x},
Exclusions -> t == 100] (* ditto *)
(* 0.0956116 *)
(* Specify an ordinary point as a singularity in the `x` interval *)
NIntegrate[(t^4 x^3)/Sqrt[-t + x], {x, 0, 1/2, 1}, {t, 0, x}]
(* 0.0956116 *)
NIntegrate
seems to apply "UnitCubeRescaling"
, which is similar to the following substitution, which I left earlier in a comment:
NIntegrate[
((t^4 x^3)/Sqrt[-t + x] /. t -> t x) * Abs@ Det@ D[{x, t x}, {{x, t}}],
{x, 0, 1}, {t, 0, 1}]
(* 0.0956116 *)
One can partially see into the workings of NIntegrate
using IntegrationMonitor
:
ireg = NIntegrate[(t^4 x^3)/Sqrt[-t + x],
{x, 0, 1}, {t, 0, x},
IntegrationMonitor :> (Return[#, NIntegrate] &)]
If we compare the integrands from my substitution and from the transformation done by NIntegrate
, we will see that they are equivalent, although symbolically they are different expressions:
First[ireg]["NumericalFunction"]["FunctionExpression"]
((t^4 x^3)/Sqrt[-t + x] /. t -> t x) Abs@Det@D[{x, t x}, {{x, t}}]
(*
(t^4 x^8)/Sqrt[x - t x]
(t^4 x^7 Abs[x])/Sqrt[x - t x]
*)
The only difference is that I wrapped the Jacobian determinant in Abs[]
. Since 0 <= x <= 1
, there's no significant difference between x
and Abs[x]
. Or is there?:
NIntegrate[(t^4 x^8)/Sqrt[x - t x], {x, 0, 1}, {t, 0, 1}]
NIntegrate::zeroregion: Integration region...cannot be further subdivided ....
NIntegrate::inumri: The integrand (t^4 x^8)/Sqrt[x-t x] has evaluated to Overflow....
(* NIntegrate[(t^4 x^8)/Sqrt[x - t x], {x, 0, 1}, {t, 0, 1}] *)
NIntegrate[(t^4 x^7 Abs[x])/Sqrt[x - t x], {x, 0, 1}, {t, 0, 1}]
(* 0.0956116 *)
In the NIntegrate
rescaling, we get the same errors as the OP. In the manual one, with Abs[x]
, it works without a hitch. It would seem that Abs[x]
triggers a different handling of the singularity.
Again, I would repeat that I can see no justification for why the OP's code shouldn't just simply work.
One should indicate the singular line (as it is described in the documentation) to calculate numerically the improper integral under consideration:
NIntegrate[(t^4 x^3)/Sqrt[-t + x], {x, 0, 1}, {t, 0, x},Exclusions -> {t == x}]
0.0956116