The problem is with numerical error in the procedure used by Integrate
. Sufficiently high arbitrary precision, or exact input, is needed to ensure an accurate result from Integrate
. On the other hand, MachinePrecision
is sufficient for NIntegrate
. There is nothing particularly numerically challenging about the integrand, so the NIntegrate
result is not surprising.
Finally, applying N
to the exact result to any precision yields an accurate result to the requested precision, as promised in the documentation. A high precision is not required.
Insufficient input precision
Block[{n = 20000, s = 0.01, μ = 10^-3, ν = 10^-3},
Integrate[E^(4 n x s) (1 - x)^(-1 + 4 n μ) x^(-1 + 4 n ν), {x, 0, 1}]
]
(* -1.786676093655969*10^352 *)
Block[{n = 20000, s = 0.01`20, μ = 10^-3, ν = 10^-3},
Integrate[E^(4 n x s) (1 - x)^(-1 + 4 n μ) x^(-1 + 4 n ν), {x, 0, 1}]
]
(* 0.*10^350 *)
Given that the answer is about 10^228
, this result suggests that the input needs more than 350 - 228 == 122
digits of precision to get at least one digit of accuracy in the result.
Sufficient input precision
122 more digits would be 142; 145 yields 3 digits.
Block[{n = 20000, s = 0.01`145, μ = 10^-3, ν = 10^-3},
Integrate[E^(4 n x s) (1 - x)^(-1 + 4 n μ) x^(-1 + 4 n ν), {x, 0, 1}]
]
(* 5.20*10^228 *)
Exact input yields an exact answer; applying N
gives an accurate approximation.
Block[{n = 20000, s = 1/100, μ = 10^-3, ν = 10^-3},
N[Integrate[E^(4 n x s) (1 - x)^(-1 + 4 n μ) x^(-1 + 4 n ν), {x, 0, 1}], 6]
]
(* 5.20048*10^228 *)
NIntegrate
gives an accurate answer quickly. It should be the preferred method for an approximate machine real result.
Block[{n = 20000, s = 0.01, μ = 10^-3, ν = 10^-3},
NIntegrate[E^(4 n x s) (1 - x)^(-1 + 4 n μ) x^(-1 + 4 n ν), {x, 0, 1}]
]
(* 5.20048*10^228 *)
Summary
NIntegrate
should also be the first method used to check a result from Integrate
for accuracy. Stability of the result under increasing precision can be used as a further check. These are fairly common checks one can find in answers and comments to integration problems on the site.
s = 0.01`20
and thens = 0.01`200
. $\endgroup$s = 0.01
20` I get0.*10^350
(in red) for the first formula and5.2004811325650457130*10^228
for the second. I am usingVersion 10.0.1.0 in Mac OSX
. I think I had a mistake first. Sorry about that. I corrected my post. Do we still get different results? $\endgroup$s = 0.01
200` I get in both cases some long number times 10 to the power228
. Seems much better. Was it indeed a round-off error? And the solution is to make to always write the good number of decimal in inputs so to tell Mathematica what degree of precision it should use? $\endgroup$0.*10^350
means that the precision loss was so great that M can only estimate that the result is closer to zero than10^350
-- a significant loss of precision. Precision tracking does not occur withMachinePrecision
numbers such ass = 0.01
. Using arbitrary-precision numbers such as0.01`20
etc, which turns on precision tracking, is a way to check calculations that seem off. I usually increase precision until the result stabilizes. $\endgroup$