Sunteți pe pagina 1din 5

Assignment 6

Name

: Nadya Amalia

Student ID

: 20213042

Subject

: Computation of Physical Systems (FI5005)

Lecturer

: Dr.rer.nat. Linus Ampang Pasasa

NUMERICAL INTEGRATION USING MONTE CARLO METHOD


1. Evaluate:
( = )4 1

for ( = , ). Use = 0, = 1 and = 1000.

2. Evaluate:

( = )6

for ( = 0, 2) and ( = 0, 1). Use = 2, = 6, and = 10000.


Solution
Consider 1-dimensional definite integral:

= )(

This can be approximated by pulling N random number , = 1, 2, 3, , , from a distribution


which is constant in the interval :

()

The result becomes exact when .

Fig, 1. The integral equals the area under the curve ()


1

In most cases, the exact value of the definite integral can not be found because an
antiderivative of the integrand can not be found. There are several methods to approximate the
value of a definite integral. One of these methods is Monte Carlo Integration.
The idea of Monte Carlo integration is to evaluate the integral F using random sampling.
Imagine a rectangle of height h, width , and area = ( ) such that the function ()

is within the boundaries of the rectangle (see Fig. 2). Compute N pairs of random numbers
with . The fraction of point that satisfy the condition () is an estimate of

the ratio of the integral of ( )to the area of the rectangle. Hence, the estimate in the hit or
miss method is given by:

Where is the number of splashes or points below the curve, and N is the total number of
points.

Fig. 2. The function ( )is in the domain determined by the ractangle of height h and width ( )

Computer is carrying out some well defined procedure, one that can be predicted or

repeated. What comes out of a computer program is called a stream of pseudorandom numbers.
1. ( = )41 , for ( = , ). = 0, = 1 and = 1000.
MATLAB Source Code

clc, clear all;


% Author: Nadya Amalia (20213042)
% Department of Physics
% Bandung Institute of Technology, Bandung
% Numerical Integration using Monte Carlo Method
% f(x) = 4*sqrt(1 - x^2)
N = 1000;
% amount of pseudo-random numbers
% Range of integration
a = 0;
b = 1;

area = 1;
% Generate N pseudo-random numbers in the interval [a,b]:
x = rand ( N, 1 );
% Uniformly randomly sample points x
sx = a + ( b - a ) .* x;
% Calculate the maximum value of the function f(x) in the range of
% integration
fx
= 4 .*sqrt(1 - sx .^2);
% function of x
plot(x,fx,'ro');
xlabel('x'); ylabel('y'); title('fx = 4*sqrt(1 - x^2)');
fmax = max(fx);
% maximum value of fx
% Restrict the value of x
i = find ( sx <= 4.*sqrt(1 - sx.^2));
p = numel(i);
% amount of the accepted x
q = N - p;
% amount of the rejected x
prob = [p q];
% Approximately evaluate the integral
fprintf('Amount of the accepted value of x : \t%d', p);
I = p * fmax * b / N

Result

Fig. 3. Plotting of ( )as a function of

Amount of the accepted value of x :


I =

970

3.8800
>>

2. ( = )6 , for ( = 0, 2) and ( = 0, 1). = 2, = 6, and = 10000.


3

MATLAB Source Code


clc, clear all;
% Author: Nadya Amalia (20213042)
% Department of Physics
% Bandung Institute of Technology, Bandung
% Numerical Integration using Monte Carlo Method
% f(x) = 6 - x ^(2 *y)
N = 10000;
% amount of pseudo-random numbers
% Range of integration
xo
= 0; xn = 2;
yo
= 0; yn = 1;
zmin = 2; zmax = 6;
area = ( xn - xo )*( yn - yo );

% the are of rectangle

% Generate N pseudo-random numbers in the interval [xo, xn] and [yo,


yn]:
x = rand ( N, 1 );
y = rand ( N, 1 );
% Uniformly randomly sample points (x,y)
sx = xo + ( xn - xo ) .* x;
sy = yo + ( yn - yo ) .* y;
subplot 121
scatter(sx,sy,'o');
xlabel('x'); ylabel('y');
title('Uniformly randomly sample points (x,y)')
% Restrict the value of x and y by considering the value of z^2 = x^2 +
y^2
i = find ( zmin^2 <= ( sx .^2 ) + ( sy .^2 ) <= zmax^2 );
p = length(i);
% amount of the accepted x and y
q = N - p;
% amount of the rejected x and y
prob = [p q];
fx = 6 - sx(i) .^ (2 .*sy(i));
subplot 122
plot3(sx,sy,fx,'r*');
grid on
xlabel('x'); ylabel('y');
title('f(x) = 6 - x ^(2 *y)');
% Approximately evaluate the integral
fprintf('Amount of the accepted value of x and y : \t%d', p);
I = area * sum( abs (fx) ) / p

Result

Fig. 4. Left: scattering of random and . Plotting of ( )as a function of and

Amount of the accepted value of x and y :


I =
9.8841
>>

10000

S-ar putea să vă placă și