-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-Monte_carlo_simulation_area_under_curve.py
62 lines (54 loc) · 1.39 KB
/
11-Monte_carlo_simulation_area_under_curve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
"""SM-1_Monte-Carlo_Assignment-3_Solution.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/19c21lj_qfiNrf_yJ7-4OJfNQoi_RcA_m
"""
import math
import random
import matplotlib.pyplot as plt
darts=[100,1000,5000,10000]
hits=0
PI_value_list=[]
circle_area=[]
rectangle_area=60
x_missed=[]
y_missed=[]
x_hit=[]
y_hit=[]
area_rectangle=16
for darts in darts:
print("Darts:",darts)
hits=0
for i in range(darts):
x=random.uniform(0, 8)
y=random.uniform(0, 4)
if x<=4:
y1=math.sqrt(4*x)
if y<=y1:
x_hit.append(x)
y_hit.append(y)
hits=hits+1
else:
x_missed.append(x)
y_missed.append(y)
area_perabola=(hits/darts)*area_rectangle
else:
y1=8-x
if y<=y1:
x_hit.append(x)
y_hit.append(y)
hits=hits+1
else:
x_missed.append(x)
y_missed.append(y)
area_triangle=(hits/darts)*area_rectangle
total_area=area_perabola+area_triangle
print("Area:",total_area)
######### Scatter Plot#########
plt.figure(figsize=(10, 6))
plt.scatter(x_missed,y_missed, color= "green", marker= ".", s=80,label = "Missed Points")
plt.scatter(x_hit,y_hit, color= "red", marker= ".", s=80,label = "Hits Points")
plt.title('Scatter Plot')
plt.legend(loc='upper right')
plt.show()