5

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.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
syhpphys
  • 195
  • 4

2 Answers2

9

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

Michael E2
  • 235,386
  • 17
  • 334
  • 747
1

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]
David G. Stork
  • 41,180
  • 3
  • 34
  • 96
  • 1
    This command plots the $x$-coordinates of the sampling points. – Anton Antonov Feb 26 '16 at 21:10
  • 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)? – syhpphys Feb 27 '16 at 21:47