How can I use Monte Carlo integration for non-rectangular regions?

  • Thread starter Thread starter steven187
  • Start date Start date
  • Tags Tags
    Monte carlo
AI Thread Summary
To use Monte Carlo integration for non-rectangular regions in Mathematica, the code must be adjusted to check if randomly generated points fall within the specified domain. The suggested approach involves maintaining two totals: one for the sum of function values within the valid region and another for the count of points that lie inside this region. The final integral estimate can be calculated as the ratio of the sum of valid function values to the total number of samples, multiplied by the area of the rectangular bounding box. The provided code example demonstrates this method, yielding results close to the expected integral value. Further optimization and testing with different functions and areas are recommended for accuracy and efficiency.
steven187
Messages
176
Reaction score
0
Dear friends

I have been trying to write a code in Mathematica for solving a double integral with a non rectangular region through monte carlo integration, I have written a code that will solve for rectangular regions but i have no idea how to change it to work for non rectangular regions

for example let the function be fun[x_,y_] := 2x
what do i need to do to make this code to solve it for the non rectangular region {(x,y):y<=x<=4y,0<=y<=2}

fun[x_,y_] := 2x
a =
b =
c =
d =
Monte2D[n_]:=(b-a)*(d-c)/n*Sum[fun[Random[Real,{a,b}],Random[Real,{c,d}]],{n}]

nsamples=1000; nrepetitions=100;
mc=Table[Monte2D[nsamples],{nrepetitions}];

Print["For n=",nsamples," with ",nrepetitions," repetitions, \
mean=",Mean[mc]," st.dev.=",StandardDeviation[mc]


Please help
 
Mathematics news on Phys.org
You calculate the integral in the rectangular region, as you have done, but for the smaller domain, you need to check to see whether each (x,y) point is inside the smaller domain.

The way I would do it would be to keep two running totals: the sum in the rectangular region(=sum), and the number of (x,y) points generated that are inside the smaller domain(=npass).

The answer will then be: sum*npass/nsamples

I'm not sure if this can be done without revamping your code. But I'm not a expert on the intracacies of Mathematica.
 
Thanks guys. It was very interesting and I learned a lot. This is what I came up with. I checked it against the integral of the indicated function over the specified domain. The integral is 40. The routine below returns 40.062.



Code:
fun[x_,y_]:=2 x;
MonteTest[fun_,nsamples_,nreps_,a_,b_,c_,d_]:=Module[{sum,tlist,xval,yval},
      tlist=Table[0,{nreps}];
      For[j=1,j<=nreps,j++,
        npass=0;
        sum=0;
        For[i=1,i<=nsamples,i++,
          xval=Random[Real,{a,b}];
          yval=Random[Real,{c,d}];
          If[yval<=xval && yval>=xval/4,
            sum+=fun[xval,yval];
            ];
          tlist[[j]]=sum*(b-a)*(d-c)/nsamples;
          ];
        ];
      Return[Mean[tlist]];
      ];
MonteTest[fun,1000,100,0,8,0,2]

Edit: Oh God. Should I not be doing this for Steve? You know Steve, I'm not sure this it right. Needs to be checked with other functions, other areas, perhaps optimized. It ran kind of slow. Don't use this for homework Ok?

Edit2: Ok, the tlist needs to go outside of the first For loop. Nevermind Steve.
 
Last edited:
Thread 'Video on imaginary numbers and some queries'
Hi, I was watching the following video. I found some points confusing. Could you please help me to understand the gaps? Thanks, in advance! Question 1: Around 4:22, the video says the following. So for those mathematicians, negative numbers didn't exist. You could subtract, that is find the difference between two positive quantities, but you couldn't have a negative answer or negative coefficients. Mathematicians were so averse to negative numbers that there was no single quadratic...
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...
Thread 'Unit Circle Double Angle Derivations'
Here I made a terrible mistake of assuming this to be an equilateral triangle and set 2sinx=1 => x=pi/6. Although this did derive the double angle formulas it also led into a terrible mess trying to find all the combinations of sides. I must have been tired and just assumed 6x=180 and 2sinx=1. By that time, I was so mindset that I nearly scolded a person for even saying 90-x. I wonder if this is a case of biased observation that seeks to dis credit me like Jesus of Nazareth since in reality...
Back
Top