Question
Use Python Code to add a block of code to count the number of dots within the red circle on the graph that is produced.
Use Python Code to add a block of code to count the number of dots within the red circle on the graph that is produced.
import sys
import numpy as np # an alias for the namespace
import random
import matplotlib.pyplot as plt
#setting MC parameters
NumP=10000 # generate how many random point in side the square
n=100
#MC simulation block
pi_all=[]
#
for j in range(n):
cordx=[];cordy=[] # initialize the two empty coordinate lists
inside=0
for i in range(0,NumP): #generate random coordinates
cordx.append(random.random())
cordy.append(random.random())
r2 = cordx[i]*cordx[i]+cordy[i]*cordy[i]
if r2 <= 1:
inside=inside+1
# write control to count inside
pit= 4.0*inside/NumP
#End of sampling
pi_all.append(pit)
#print out result
print("pi: ","{:10.6f}".format(pit))
plt.hist(pi_all) # print hist of the sampling
# plot the random pair: cordx,cordy graphically for the last MC simulation
Symbol=10
LineW = 3
Lsp=100
f = plt.figure()
f.set_figwidth(8)
f.set_figheight(8)
plt.xlim(0,1)
plt.ylim(0,1)
plt.xlabel('X Cordinate')
plt.ylabel('Y Cordinate')
plt.scatter(cordx, cordy, s=Symbol,marker='o')
#plot circle
x = np.linspace(0,1,Lsp)
y = np.sqrt(1. - x*x)
plt.plot(x, y,'-r',color='red', linestyle='-', lw=LineW)
plt.show()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started