One can constrain the precision of NIntegrate
by setting PrecisionGoal
:
NIntegrate[1/Sqrt[x + y], {x, 0, 1}, {y, 0, 1}] // FullForm
(*==> 1.1045695042415091`*)
NIntegrate[1/Sqrt[x + y], {x, 0, 1}, {y, 0, 1}, PrecisionGoal -> 2] // FullForm
(*==> 1.1052223748431014`*)
Since I have a lot of NIntegrate functions in my work,
I want to set globally the PrecisionGoal
to save the labor.
Here is what I did:
$Pre = Function[{expr},
expr /. NIntegrate -> (NIntegrate[##, PrecisionGoal -> 2] &),
HoldAll];
However, Trace
shows that the above setting does not work as expected:
NIntegrate[1/Sqrt[x + y], {x, 0, 1}, {y, 0, 1}] // Trace
{(NIntegrate[##1,PrecisionGoal->2]&)[1/Sqrt[x+y],{x,0,1},{y,0,1}],{{{Sqrt[x+y],Sqrt[x+y]},1/Sqrt[x+y],1/Sqrt[x+y]},1/Sqrt[x+y],1/Sqrt[x+y]},{{x,y}=.,{x=.,y=.},{x=.,Null},{y=.,Null},{Null,Null}},{x=.,Null},{x=.,Null},{y=.,Null},{{x,y}=.,{x=.,y=.},{x=.,Null},{y=.,Null},{Null,Null}},{Message[General::munfl,0.5 4.0326079252773842957465158527343513127923262375362274882806697019010^-330],Null},{Message[General::munfl,0.5 4.0326079252773842957465158527343513127923262375362274882806697019010^-330],Null},{Message[General::munfl,0.5 4.0326079252773842957465158527343513127923262375362274882806697019010^-330],{Message[General::stop,General::munfl],{General::stop,Further output of
1
will be suppressed during this calculation.},Null},Null},{Message[General::munfl,0.03125 2.5203799532983651848415724079589695704952038984601421801754185636910^-331],Null},{Message[General::munfl,0.03125 2.52037995329836518484157240795896957049520389846014218017541856369*10^-331],Null},..., 1.10457}
One can see that the numerical integration is still carried out with high precision, even though the first line in trace didn't ask for this. This claim can be verified by
$Pre=.
(NIntegrate[##1, PrecisionGoal -> 2] &)[1/Sqrt[x + y], {x, 0, 1}, {y, 0, 1}]
(*==> 1.10522*)
Apart from this, there are many General::munfl
warnings.
My question is how to understand the above behaviors?