Monte Carlo Integration

Unit Circle

num =40000;
Clear[inlist]; Clear[outlist];
inlist = {};
 outlist = {};
numin = 0;
For[m = 1, m ≤ num, m++,
{
x = 2(Random[]-.5);
y = 2 (Random[]-.5);
If[x^2 + y^2 ≤ 1,loc =1,loc = 2];
If[loc == 1,  inlist = AppendTo[inlist, {x,y}],   outlist= AppendTo[outlist, {x,y}]];
numin = numin + 2-loc;
percentage[m] = 1.0 numin / m;
}];
cut[dataset_, number_] := Module[{},
temp = {};
For[i = 1, i ≤ Floor[number], i++, temp = AppendTo[temp,dataset[[i]]]];
Return[temp];
];
cut[inlist,5]

percentage[7000]

Print["Plot of randomly chosen points inside and outside unit circle, whose area is ", 1.0 Pi];
Manipulate[ListPlot[{cut[inlist,numdo], cut[outlist,numdo]},PlotStyle -> {Red,Blue},AspectRatio -> 1, PlotLabel -> 4 percentage[Floor[numdo]]],{numdo,20,.2num}]

Ellipse x^2 + 4 y^2 = 1

num2 =40000;
Clear[inlist2]; Clear[outlist2];
inlist2 = {};
 outlist2 = {};
numin = 0;
For[m = 1, m ≤ num2, m++,
{
x = 2(Random[]-.5);
y = 2 (Random[]-.5);
If[x^2 + 4 y^2 ≤ 1,loc =1,loc = 2];
If[loc == 1,  inlist2 = AppendTo[inlist2, {x,y}],   outlist2= AppendTo[outlist2, {x,y}]];
numin = numin + 2-loc;
percentage2[m] = 1.0 numin / m;
}];
cut[dataset_, number_] := Module[{},
temp = {};
For[i = 1, i ≤ Floor[number], i++, temp = AppendTo[temp,dataset[[i]]]];
Return[temp];
];
cut[inlist2,5]

Print["Plot of randomly chosen points inside and outside ellipse x^2 + 4 y^2 = 1, whose area is ", 1.0 Pi/2];
Manipulate[ListPlot[{cut[inlist2,numdo], cut[outlist2,numdo]},PlotStyle -> {Red,Blue},AspectRatio -> 1, PlotLabel -> 4 percentage2[Floor[numdo]]],{numdo,20,.2num2}]

General Region

numn =40000;
randpoints = {};
For[m = 1, m ≤ numn, m++,
randpoints = AppendTo[randpoints, {2(Random[]-.5), 2 (Random[]-.5)}]];
randpoints[[1,2]]

Clear[inlistn]; Clear[outlistn];
inlistn= {};
outlistn= {};
numin = 0;
For[m = 1, m ≤ numn, m++,
{
x = randpoints[[m,1]];
y = randpoints[[m,2]];
If[16 x^2  - 1 ≥  4 y^2,loc =1,loc = 2];
If[loc == 1,  inlistn = AppendTo[inlistn, {x,y}],   outlistn= AppendTo[outlistn, {x,y}]];
numin = numin + 2-loc;
percentagen[m] = 1.0 numin / m;
}];
cut[dataset_, number_] := Module[{},
temp = {};
For[i = 1, i ≤ Floor[number], i++, temp = AppendTo[temp,dataset[[i]]]];
Return[temp];
];
cut[inlistn,5]

Print["Plot of randomly chosen points inside and outside."];
Manipulate[ListPlot[{cut[inlistn,numdo], cut[outlistn,numdo]},PlotStyle -> {Red,Blue},AspectRatio -> 1, PlotLabel -> 4 percentagen[Floor[numdo]]],{numdo,20,.2numn}]

Fibonacci Monte Carlo Integration

gm = (1 + Sqrt[5])/2;
xmove = Sqrt[1/(2 + gm)];
ymove = xmove gm;
Simplify[1 - xmove^2 - ymove^2]

num4=40000;
Clear[inlist4]; Clear[outlist4];
inlist4 = {};
 outlist4 = {};
numin = 0;
xcoord = 0;
For[m = 1, m ≤ num4, m++,
{
xcoord = xcoord + xmove;
x = Mod[ xcoord, 1];
y = Mod[gm xcoord,1];
inlist4 = AppendTo[inlist4, {x,y}];
}];
cut[dataset_, number_] := Module[{},
temp = {};
For[i = 1, i ≤ Floor[number], i++, temp = AppendTo[temp,dataset[[i]]]];
Return[temp];
];
cut[inlist2,5]

Print["Plot of randomly chosen points inside and outside."];
Manipulate[ListPlot[cut[inlist4,numdo],PlotStyle -> {Red},AspectRatio -> 1],{numdo,1,.2numn}]