This project demonstrates the Monte Carlo simulation method to approximate the area under the curve for any continuous functions as well the estimation of value Pi using random walks. The computer simulation involves randomly throwing "stones" within a defined boundary and counting how many fall under the curve to estimate the area.
The Monte Carlo method is a statistical technique that uses random sampling to obtain numerical results. In this simulation 1, we use the Monte Carlo method to approximate the area under the curve of the function ( f(x) = x**3 ) between (x = 0 and x = 3).
Before running the code, ensure you have the following installed:
Python 3.x
(You can download python here if you dont have it yet)matplotlib
library for plottingnumpy
library for numerical operations
You can install the required libraries using pip:
pip install matplotlib numpy
- Clone the repository and Go to the Project directory:
git clone https://github.com/topollonaketsana/Monte-Carlo-Simulation.git
cd Monte-Carlo-Simulation
- Run the Python script:
python monte_carlo_simulation.py
-
Description of the Python Script: The script monte_carlo_simulation.py performs the following steps:
- Defines the bounds of the rectangular boundary [x_min, x_max]*[y_min, y_max].
- Randomly generates points within the boundary [x, y].
- Checks whether each point lies below the curve y = x**3
- Calculates the estimated area under the curve based on the ratio of points below (Incount) the curve to the total number of points(N)
The code is already included in the python src
file, you can just copy it directly and paste it into your machine for interpretation. Code can be edited for better understanding, eg, on line (8) I included the rate for incount (number of stones thrown inside) this can be edited depending on how you want to interpret the results and how random stones are thrown inside the boundary.
Note that N (total number of stones thrown) can be adjusted depending on how many stones you want to throw.
//////////////////////