Professional Documents
Culture Documents
Practical
Practical
Practical
2.0
1.5
f (x)
1.0
2
0.5
The area of the region simulated by Monte Carlo area algorithm is : 1.93522
f (x)
6
g(x)
4 -1
10
2
-3 -2 -1 1 2
The area of the region simulated by Monte Carlo area algorithm is : 20.35
Practical 5(b)
Monte Carlo Volume Simulation : For a single
4 5.nb
function
(1)Find the volume of the sphere x2 + y2 + z2 ≤ 1 in the first octant.
Clear[n, counter, a, b, f, c, d, maxlimit, Volume, x, y, z, x0, y0, z0];
n=
Input["Enter the total number of random points to be generated in the simulation : "];
counter = 0;
a = 1;
b = 1;
f[x_, y_] = Sqrt1 - x2 - y2 ;
c = Minimize[{f[x, y], x ≥ 0 && x ≤ a && y ≥ 0 && y ≤ a}, {x, y}];
d = Maximize[{f[x, y], x ≥ 0 && x ≤ a && y ≥ 0 && y ≤ a}, {x, y}];
maxlimit = d[[1]] + 1;
Fori = 1, i ≤ n, i ++, x0 = RandomReal[{0, a}];
y0 = RandomReal[{0, b}];
z0 = RandomReal[{0, maxlimit}];
Ifz02 < f[x0, y0]2 , counter ++;;
Volume = maxlimit b a counter n;
Plot3D[{f[x, y], maxlimit}, {x, 0, a},
{y, 0, b}, Filling → Bottom, PlotLegends → "Expressions"]
Print["the number of points in the sphere is :", counter];
Print["The volume of the sphere in first octant
simulated by Monte Carlo area algorithm is : ", N[Volume]];
f (x, y)
maxlimit
The volume of the sphere in first octant simulated by Monte Carlo area algorithm is : 0.54
5.nb 5
f (x, y)
g(x, y)
minlimit
maxlimit
The volume of the region simulated by Monte Carlo area algorithm is : 42.4943