






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The monte carlo method for finding integrals through direct sampling. It includes examples with matlab code and visualizations of the results. The method involves generating random numbers and using their cumulative distribution to approximate the integral.
Typology: Slides
1 / 10
This page cannot be seen from the preview
Don't miss anything!







docsity.com
b a
we have to find following integral:
Then let us first sample x using f(x) (say by direct method).
First we find its
normalization constant:
∞^ ∫
0
dx x f
x a
docsity.com
1 0 4
1 0
3
∫
x
dx x
∫
1 0
2
1
2
) (
1 0 2
1 0
1 0
=
=
=^
∫
∫^
A
x A
dx x A
dx x Af
Let us consider f(x) = x and g(x) = x
The exact answer is
Let us first find the normalization constant for f(x): Then comulative pdf is
∫
∫^
x
x
dx x
xdx
x
x
x
0 2
0
0
and the find the area as
. /) (
1 2
N x g
I^
i^
i
∑ ≈
So, we use this equationto generate x
docsity.com
Program for the
example
% Program to find integral (f(x)g(x) type) % by Monte Carlo method % as a function of N N = 1000 ; rand('state', 0) % initialize area(1)= 0.25; xn(1)= N; exact_area(1)= 0.25; er(1) =0.0;^ for i = 2:
i sum = 0; for k = 1: N
exi = rand; x(k) = sqrt(exi);
end^ for kk = 1: N
sum = sum + x(kk)*x(kk);
end^ area(i) = sum/(2.*N); xn(i)= N;^ N = N + 1000;^ exact_area(i) = 0.25;^ error(i) = (area(i) - exact_area(i)); end plot(xn, area, xn, exact_area)
docsity.com
Let us try to find the following integral:
π
π
=
=
∫^0
sin
xdx
x
I
1 0
)
(sin
dx x
x
I^2
0 2
0
0
/ 2 1 2 ) ( π
π
π
π
=
=
=^
∫
∫^
A
x A
dx x A
dx x Af
Let us consider f(x) = x and g(x) = sinx.
The exact answer is
Let us first find the normalization constant for f(x): Then comulative pdf is
ξ π
ξ
=
⇒
=
=
=^
∫
∫
x
x A
dx x A
Axdx
x
x
x
0 2
0
0
2
So, we use the above equation to generate x and the find the area as
. /)
(
(^22)
N
x g
I^
i^
i
∑
≈
π
docsity.com
The MATLAB program
for Example 2
% Program to find integral (f(x)g(x) type) % by Monte Carlo method % as a function of number of histories N = 1000 ; rand('state', 0) % initialize area(1)= pi; xn(1)= N; exact_area(1)= pi; er(1) =0.0;^ for i = 2:
i sum = 0; for k = 1: N
exi = rand; x(k) = pi*sqrt(exi);
end
for kk = 1: N
sum = sum + sin(x(kk)); end area(i) = pipisum/(2.*N); xn(i)= N; N = N + 1000; exact_area(i) = pi; error(i) = (area(i) - exact_area(i));
docsity.com
Error in Area
Number of Trails in a sample
x 1000
Error in Area from MC method Zero Error line
docsity.com