Anda di halaman 1dari 6

Lab Session 9

Performing Monte Carlo Simulation in Matlab


Course Number and Name:
CSC 353 Simulation and Modeling
Semester and Year:
Spring 2015
Name of Student:
Shafaq Siddiqui

Name of Lab Instructor:


Muhammad Irshad Nazeer

Date of Report Submitted:


16/4/2015

Grade:

ABSTRACT
In this lab we will perform Monte Carlo simulation and will find the value of Pi and area under
the curve.

Objective of lab:
Objective of lab is to perform Monte Carlo Simulation

Objective significance:
Monte Carlo simulation (also known as the Monte Carlo Method) lets you see all the possible
outcomes of your decisions and assess the impact of risk, allowing for better decision making
under uncertainty

Major results of work achieved:


.Value of Pi and area under the curve of square function

Type of work to achieve the objective?


User defined functions (.m) files in Matlab

Conclusions from results:


By using Monte Carlo method we will find the value of Pi and are under the curve using random
values and analyze the results,

Objectives

To understand the Monte Carlo Method its purpose and use.


To write user defined functions to find the value of Pi by applying concepts of Monte carl
Method
To write user defined functions to find the area under the curve by applying concepts of
Monte carl Method
To aggregate and analyze the results of simulation

Experiment and Methodologies


Matlab
MATLAB (matrix laboratory) is a fourth-generation high-level programming language and
interactive environment for numerical computation, visualization and programming.
It allows matrix manipulations; plotting of functions and data; implementation of algorithms;
creation of user interfaces; interfacing with programs written in other languages, including C, C+
+, Java, and FORTRAN; analyze data; develop algorithms; and create models and applications.
(Lab report 6 covers the complete basics of Matlab)
Monte Carlo Simulation
A problem solving technique used to approximate the probability of certain outcomes by running
multiple trial runs, called simulations, using random variables.
Monte Carlo simulation is named after the city in Monaco, where the primary attractions are
casinos that have games of chance. Gambling games, like roulette, dice, and slot machines,
exhibit random behavior[1].
Monte Carlo methods vary, but tend to follow a particular pattern:
1.
2.
3.
4.

Define a domain of possible inputs.


Generate inputs randomly from a probability distribution over the domain.
Perform a deterministic computation on the inputs.
Aggregate the results.

Task 01: Approximating value of Pi using Monte Carlo Simulation

Consider a circle inscribed in a unit square. Given that the circle and the square have a ratio of
areas that is /4, the value of can be approximated using a Monte Carlo method:[2]
1.
2.
3.
4.

Draw a square on the ground, then inscribe a circle within it.


Uniformly scatter some objects of uniform size (grains of rice or sand) over the square.
Count the number of objects inside the circle and the total number of objects.
The ratio of the two counts is an estimate of the ratio of the two areas, which is /4.
Multiply the result by 4 to estimate .

In this procedure the domain of inputs is the square that circumscribes our circle. We generate
random inputs by scattering grains over the square then perform a computation on each input (test
whether it falls within the circle). Finally, we aggregate the results to obtain our final result, the
approximation of .
When writing the program by following Monte Carlo method having:
1. Domain: Set of real numbers .
2. Probability distribution: Uniform probability distribution
3. Perform a deterministic computation on the inputs: by using equation of square x2+y2=r2
If x2+y2<r2 count the random numbers they are inside the circle else do not count.
4. Aggregate the results: divide the all counts from total and multiply the results from 4.

There are two important points to consider here:


Firstly, if the grains are not uniformly distributed,
then our approximation will be poor. Secondly, there
should be a large number of inputs. The
approximation is generally poor if only a few grains
are randomly dropped into the whole square. On
average, the approximation improves as more grains
are dropped[2].

Task 02: Calculating area under the curve


Following the same method as described in task 01 we will find the area under the curve of a
square function
When writing the program by following Monte Carlo method having:
1. Domain: x = a---b and y= 0--M .
2. Probability distribution: Uniform probability distribution
3. Perform a deterministic computation on the inputs: by using square function f(x)
That if f(x)<x2 count else do not count
4. Aggregate the results: finding area under the curve with the ratio of the area of rectangle.

Functions
1. Area under the curve
function area = AreaUnderTheCurve(a,b,m,n)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
count = 0;
for i=1:n
x(i)=a+(rand()*(b-a));
y(i)=m*rand();
if(y(i)<=x(i)^2)
count= count+1;
else
continue;
end
end
area = (m(b-a))*(count/n);
end
2. Approximating value of PI
function piVal = FindPi(n)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
count = 0;
for i=1:n
x(i)=rand();
y(i)=rand();
if(((x(i)*x(i)) + (y(i)*y(i))) < 1)
count= count+1;
else
continue;

end
end
piVal = (count/n)*4;
end

Conclusion
Monte Carlo simulation (also known as the Monte Carlo Method) lets you see all the possible
outcomes of your decisions and assess the impact of risk, allowing for better decision making
under uncertainty and by using Monte Carlo Simulation we can estimate any given measure
which is difficult to calculate through a defined mathematical model. By using Monte Carlo
method we can find the values under high uncertainty by doing hit and trial.
References
[1] http://www.investopedia.com/terms/m/montecarlosimulation.asp#ixzz3XU3Wt3LE
[2] http://en.wikipedia.org/wiki/Monte_Carlo_method