Is there a way to extract the error that Mathematica estimates when calculating a numerical integral using NIntegrate
?
Internally Mathematica must keep track of this error, because it is used to determine if the PrecisionGoal
has been met.
The reason I want to extract this information is that some integration strategies (e.g. "Trapezoidal"
) can significantly overshoot the PrecisionGoal
target. In cases where this happens it would be very useful to include this in any estimated error bars on the result.
edit
For clarification lets consider example.
I have a complicated integrand f[x]
(that is expensive to compute). I know a couple of things about this integrand:
1) It is 2π periodic. 2) It is C-infinite smooth.
These two facts mean that f[x]
has a Fourier series whose coefficients decay exponentially.
This in turn means that a trapezoidal integration strategy also converges exponentially. Hence I integrate with:
NIntegrate[
f[x],
{x,0,2Pi},
Method-> {"Trapezoidal", "SymbolicProcessing"->0},
PrecisionGoal -> n
]
Where n
is my desired precision. This works well.
Now I want to estimate the error bound of the integration. More I want to do this without any further evaluations of f[x]
.
Because (by assumption) of exponential convergence of the integrand, the "Trapezoidal"
strategy will double the precision of the result at each integration step (once it is in the tail). Consequently, the (estimated) precision of the final answer is somewhere between n
and 2n
. By guessing an error bound equal to the precision goal we my be massively overestimating the actual error. (Which is not good if the result is to be used in a later data analysis step.)
This integral is part of a loop of a much longer code. A typical run contains up to 10^5 of these integrals. Hence, fiddling with settings to coax an error report for a single integral is not really an option. Somewhere in its internals Mathematica is calculating this error estimate, hence it must be possible to extract it. If only we knew the name of the internal variable that is used for the error estimate for the trapezoidal strategy.
MaxPoints
and then catching the value of the messageNIntegrate::maxp
. $\endgroup$tutorial/NIntegrateIntegrationRules
(search for error weights). See also the rule data functions?NIntegrate
*RuleData`, which return the error weights, among other things. I used such functions in my answer to 61238 to calculate the error estimate. $\endgroup$