Practical

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Practical 5(a)

Monte Carlo Simulation : For a single function


(1)Find the area beneath the curve y=Cos[x] over the interval
-π/2≤ x≤ π/2.
Clear[n, counter, a, b, f, c, d, maxlimit, Area, x];
n=
Input["Enter the total number of random points to be generated in the simulation : "];
counter = 0;
a = - π  2;
b = π  2;
f[x_] = Cos[x];
c = Minimize[{f[x], x ≥ a && x ≤ b}, x];
d = Maximize[{f[x], x ≥ a && x ≤ b}, x];
maxlimit = d[[1]] + 1;
For[i = 1, i ≤ n, i ++, x0 = RandomReal[{a, b}];
y0 = RandomReal[{0, maxlimit}];
If[y0 < f[x0], counter ++];];
Area = maxlimit b - a counter  n;
Plot[{f[x], maxlimit}, {x, a, b}, Filling → Bottom, PlotLegends → "Expressions"]
Print["the number of points under the curve is :", counter];
Print[
"The area of the region simulated by Monte Carlo area algorithm is : ", N[Area]];

2.0

1.5

f (x)
1.0
2

0.5

-1.5 -1.0 -0.5 0.5 1.0 1.5


2 5.nb

the number of points under the curve is :308

The area of the region simulated by Monte Carlo area algorithm is : 1.93522

Monte Carlo Simulation : For more than one


function
(2) Find the area trapped between the two curves y =
x2 and y = 6 - x.
5.nb 3

Clear[n, counter, a, b, f, c, d, maxlimit, Area, x];


n=
Input["Enter the total number of random points to be generated in the simulation : "];
counter = 0;
f[x_] = x2 ;
g[x_] = 6 - x;
solution = Solve[f[x] == g[x], x];
a = solution[[1, 1, 2]];
b = solution[[2, 1, 2]];
c1 = Minimize[{f[x], x ≥ a && x ≤ b}, x];
d1 = Maximize[{f[x], x ≥ a && x ≤ b}, x];
c2 = Minimize[{g[x], x ≥ a && x ≤ b}, x];
d2 = Maximize[{g[x], x ≥ a && x ≤ b}, x];
minlimit = Min[c1[[1]], c2[[1]]] - 1;
maxlimit = Max[d1[[1]], d2[[1]]] + 1;
For[i = 1, i ≤ n, i ++, x0 = RandomReal[{a, b}];
y0 = RandomReal[{minlimit, maxlimit}];
If[Min[f[x0], g[x0]] < y0 < Max[f[x0], g[x0]], counter ++];];
Area = maxlimit - minlimit b - a counter  n;
Plot[{f[x], g[x], minlimit, maxlimit},
{x, a, b}, Filling → Bottom, PlotLegends → "Expressions"]
Print["the number of points under the curve is :", counter];
Print["The area of the region simulated by Monte Carlo area algorithm is : ", N[Area]];
10

f (x)
6
g(x)
4 -1
10
2

-3 -2 -1 1 2

the number of points under the curve is :370

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_] = Sqrt1 - 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;
Fori = 1, i ≤ n, i ++, x0 = RandomReal[{0, a}];
y0 = RandomReal[{0, b}];
z0 = RandomReal[{0, maxlimit}];
Ifz02 < 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 number of points in the sphere is :270

The volume of the sphere in first octant simulated by Monte Carlo area algorithm is : 0.54
5.nb 5

Monte Carlo Volume Simulation : For more


than one function
(2) Calculate the volume trapped between the
two paraboloids z = 8 - x2 - y2 and z = x2 + 3 y2.
6 5.nb

Clear[n, counter, a, b, f, c, d, maxlimit, Area, x];


n=
Input["Enter the total number of random points to be generated in the simulation : "];
counter = 0;
f[x_, y_] = 8 - x2 - y2 ;
g[x_, y_] = x2 + 3 y2 ;
solution = Solve[f[x, y] == g[x, y], {x, y}];
f1 [x_] = solution[[1, 1, 2]];
f2 [x_] = solution[[2, 1, 2]];
solution1 = Solve[f2 [x] ⩵ 0, x];
a = solution1[[1, 1, 2]];
b = solution1[[2, 1, 2]];
c1y = Minimize[{f1 [x], x ≥ a && x ≤ b}, x];
d1y = Maximize[{f1 [x], x ≥ a && x ≤ b}, x];
c2y = Minimize[{f2 [x], x ≥ a && x ≤ b}, x];
d2y = Maximize[{f2 [x], x ≥ a && x ≤ b}, x];
minlimity = Min[c1y[[1]], c2y[[1]]];
maxlimity = Max[d1y[[1]], d2y[[1]]];
c1 = Minimize[{f[x, y], x ≥ a && x ≤ b && y ≤ maxlimity && y ≥ minlimity}, {x, y}];
d1 = Maximize[{f[x, y], x ≥ a && x ≤ b && y ≤ maxlimity && y ≥ minlimity}, {x, y}];
c2 = Minimize[{g[x, y], x ≥ a && x ≤ b && y ≤ maxlimity && y ≥ minlimity}, {x, y}];
d2 = Maximize[{g[x, y], x ≥ a && x ≤ b && y ≤ maxlimity && y ≥ minlimity}, {x, y}];
minlimit = Min[c1[[1]], c2[[1]]] - 1;
maxlimit = Max[d1[[1]], d2[[1]]] + 1;
For[i = 1, i ≤ n, i ++, x0 = RandomReal[{a, b}];
y0 = RandomReal[{minlimity, maxlimity}];
z0 = RandomReal[{minlimit, maxlimit}];
If[Min[f[x0, y0], g[x0, y0]] < z0 < Max[f[x0, y0], g[x0, y0]], counter ++];];
Volume = maxlimit - minlimit maxlimity - minlimity b - a counter  n;
Plot3D[{f[x, y], g[x, y], minlimit, maxlimit}, {x, a, b},
{y, minlimity, maxlimity}, Filling → Bottom, PlotLegends → "Expressions"]
Print["the number of points under the curve is :", counter];
Print[
"The volume of the region simulated by Monte Carlo area algorithm is : ", N[Volume]];

f (x, y)
g(x, y)
minlimit
maxlimit

the number of points under the curve is :313

The volume of the region simulated by Monte Carlo area algorithm is : 42.4943

You might also like