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

  • Thread starter steven187
  • Start date
  • Tags
    Monte carlo
In summary, the conversation discusses the process of writing a code in Mathematica for solving a double integral with a non-rectangular region through Monte Carlo integration. The code provided solves for rectangular regions but not for non-rectangular regions. The suggested solution involves checking whether each (x,y) point is inside the smaller domain and keeping track of two running totals. The final output should be the product of the sum in the rectangular region and the number of points inside the smaller domain divided by the number of samples. The code provided needs to be optimized and tested with other functions and domains.
  • #1
steven187
176
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
  • #2
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.
 
  • #3
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:

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

What is Monte Carlo Integration?

Monte Carlo Integration is a numerical method used to calculate the value of an integral by generating random samples from the function being integrated. It is based on the Law of Large Numbers, which states that as the number of samples increases, the average of the samples will converge to the true value of the integral.

How does Monte Carlo Integration work?

Monte Carlo Integration works by randomly selecting points within the bounds of the integral and using these points to calculate the average value of the function. The more points that are used, the more accurate the approximation of the integral will be.

What are the benefits of using Monte Carlo Integration?

One of the main benefits of using Monte Carlo Integration is its versatility. It can be used to solve integrals that are difficult or impossible to solve analytically, and it can handle a wide range of integrands. Additionally, Monte Carlo Integration is relatively easy to implement and can provide accurate results with a relatively small number of samples.

Are there any limitations to Monte Carlo Integration?

While Monte Carlo Integration is a powerful tool, it does have some limitations. It can be computationally expensive, especially for high-dimensional integrals, and the convergence rate can be slow. Additionally, the accuracy of the results heavily depends on the quality of the random number generator used.

How do I know if Monte Carlo Integration is the right method to use for my integral?

Monte Carlo Integration is a good choice for integrals with a high number of dimensions, as other numerical integration methods can become increasingly difficult to implement in higher dimensions. It is also useful for integrals with complex or irregular boundaries. However, for simple, low-dimensional integrals, other methods such as the trapezoidal rule or Simpson's rule may be more efficient and accurate.

Similar threads

Replies
15
Views
8K
Replies
5
Views
2K
Replies
1
Views
4K
Replies
3
Views
2K
Replies
1
Views
1K
Replies
12
Views
2K
Replies
1
Views
3K
Back
Top