6
$\begingroup$

There is a variety of algorithms for performing numerical integration (See wiki). What method does NIntegrate use by default?

I looked at the documentation page and I saw that the function NIntegrate can use a ton of different algorithms, but I couldn't find which one is used by default. Where can I find this info?

$\endgroup$
4

1 Answer 1

12
$\begingroup$

the answer is in the links -- just to demonstrate or validate what it says:

 First@Last@
    Reap@NIntegrate[Sin[x], {x, 0, 1}, EvaluationMonitor :> Sow[x], 
          MaxRecursion -> 0, Method -> "GaussKronrodRule"]
 xi=First@Last@
    Reap@NIntegrate[Sin[x], {x, 0, 1}, EvaluationMonitor :> Sow[x], 
          MaxRecursion -> 0]

{0.00795732, 0.0469101, 0.122917, 0.230765, 0.360185, 0.5, 0.639815, 0.769235, 0.877083, 0.95309, 0.992043}

{0.00795732, 0.0469101, 0.122917, 0.230765, 0.360185, 0.5, 0.639815, 0.769235, 0.877083, 0.95309, 0.992043}

the default uses the same evaluation points as the "GaussKronrodRule" method.

You can then pull out the weights like this:

 w=(f[x_?NumericQ] := Boole[x == #];
    Quiet@NIntegrate[f[x], {x, 0, 1}, MaxRecursion -> 0]) & /@ xi

{0.021291, 0.0576167, 0.0934004, 0.12052, 0.136425, 0.141494, 0.136425, 0.12052, 0.0934004, 0.0576167, 0.021291}

and show they are of course the same whether you specify "GaussKronrodRule" or nothing.

for completeness, just to show the weight extraction is correct:

 {w.Sin[xi] , NIntegrate[Sin[x], {x, 0, 1}]}
 SameQ @@ %

{0.459698, 0.459698}

True

Finally note, even with MaxRecursions->0 the automatic method can change, for example if it detects an oscilatory function:

 First@Last@Reap@NIntegrate[Sin[4 x], {x, 0, 1}, EvaluationMonitor :> Sow[x], 
      MaxRecursion -> 0]

{1., 0.996057, 0.984292, 0.964888, 0.938153, 0.904508, 0.864484, 0.818712, 0.767913, 0.71289, 0.654508, 0.593691, 0.531395, 0.468605, 0.406309, 0.345492, 0.28711, 0.232087, 0.181288, 0.135516, 0.0954915, 0.0618467, 0.0351118, 0.0157084, 0.00394265, 0.}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.