5
$\begingroup$

I am using NIntegrate with Method -> "MonteCarlo" to evaluate a high dimensional integral. Is there a way I can obtain a list containing Mathematica's successive estimates for the integral? I would like to see a plot showing the convergence.

$\endgroup$
9
$\begingroup$

Using the undocumented IntegrationMonitor:

{val, {vals}} = 
 Reap@NIntegrate[x y^2, {x, 0, 1}, {y, 0, 1}, PrecisionGoal -> 2,
   Method -> "MonteCarlo",
   IntegrationMonitor :> ((Sow[Total@Through[#["Integral"]]]) &)]
(*
  {0.165268, {{0.186623, 0.172189, 0.168129, 0.166339, 0.165429, 
     0.16988, 0.173145, 0.171675, 0.173355, 0.177199, 0.175892, 
     0.176102, 0.176696, 0.175119, 0.173314, 0.172074, 0.172378, 
     0.173556, 0.172886, 0.173371, 0.171839, 0.172351, 0.171481, 
     ...,
     0.165933, 0.165739, 0.165553, 0.165592, 0.165477, 0.165581, 
     0.165412, 0.165332, 0.165484, 0.165209, 0.165268}}}
*)

ListPlot@vals

Mathematica graphics

$\endgroup$
1
$\begingroup$

Yes... try using EvaluationMonitor

ListPlot[Reap[
   NIntegrate[1/Sqrt[x y z], {x, 0,1}, {y, 0,1}, {z, 0,1},
    EvaluationMonitor :> Sow[x]]][[2, 1]], PlotRange -> All]
$\endgroup$
2
  • 1
    $\begingroup$ This command plots the $x$-coordinates of the sampling points. $\endgroup$ – Anton Antonov Feb 26 '16 at 21:10
  • $\begingroup$ Is there a way to sow the actual integrand rather than just the x-coordinates (without computing the integrand a second time for each x-coordinate, since that would be inefficient as my integrand is computationally expensive)? $\endgroup$ – syhpphys Feb 27 '16 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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