Monte Carlo integration uses random sampling unlike classical techniques like the trapezoidal or Simpson's rule in evaluating the integration numerically.

restart; ff := proc (rho, phi) return exp(rho*cos(phi))*rho end proc; aa := 0; bb := 1; cc := 0; dd := 2*Pi; alfa := 5; nrun := 15000; sum1 := 0; sum2 := 0; X := Statistics:-RandomVariable(Uniform(0, 1)); SX := Statistics:-Sample(X); for ii to nrun do u1 := SX(1)[1]; u2 := SX(1)[1]; xx1 := aa+(bb-aa)*u1; xx2 := cc+(dd-cc)*u2; xx3 := (bb-aa)*(1-u1); xx4 := (dd-cc)*(1-u2); sum1 := sum1+evalf(ff(xx1, xx2)); sum2 := sum2+evalf(ff(xx1, xx2))+evalf(ff(xx1, xx4))+evalf(ff(xx3, xx2))+evalf(ff(xx3, xx4)) end do; area1 := (bb-aa)*(dd-cc)*sum1/nrun; area2 := (bb-aa)*(dd-cc)*sum2/(4*nrun); area2

HFloat(3.5442090450428574)

(1)

evalf(Int(exp(rho*cos(phi))*rho, rho = 0 .. 1, phi = 0 .. 2*Pi))

3.550999378

(2)

NULL


 

Download MONTE_CARLO_INTEGRATION1.mw

 

 


Please Wait...