I NEED HELP TO MAKE THIS CODE RUN AND GIVE ME A PI VALUE, WITHOUT CREATING A DIFFERENT CODE. COULD I PLEASE HAVE THIS ONE CHECKED. #Python code for determining Pi using Monte Carlo #INPUTS #Defining constants x_coord=0.0 #x coord of stone y_coord=0.0 #y coord of stone maxit=100 #upper limit to loop i.e. number of stones throw rad=1.0 #radius of circle square=2.0 #area of square #Defining variables valuePi=0.0 #variable to hold the value of Pi incount=0 #variable to count splashes M=5 #number of times for each N MPi=0 #holder for sum of of Pi values for that N averagePi=0 #average Pi value for the M times i=0.0 #inner loop variable j=0 #outer loop variable #Defining the graphical displays gdisplay() #define display for stones inside=gdots(color=color.red) outside=gdots(color=color.blue) gdisplay() #define display for graph plotpi=gdots(color=color.black) #PROCESSING #loop over M times for j in range(1,M+1): #reset variables i=0 incount=0 valuePi=0 #delete graph contents for quarter circle outside.delete() inside.delete() #Throw N stones for i in range(0,maxit): #do this maxit tmes rate(10) #slow down visualisation #Throw a stone x_coord=random() y_coord=random() #Check if stone splashed if sqrt(x_coord**2 + y_coord**2)<=rad: incount= incount+1 #update splashes inside.plot(pos=(x_coord,y_coord)) #plot stone in blue else: #if stone is outside just plot outside.plot(pos=(x_coord,y_coord)) #Determine current value of Pi valuePi= ((incount / maxit)*square)/rad #Plot current value of Pi plotpi.plot(pos=(i, valuePi)) MPi=MPi+valuePi #put M values of Pi into a container #This is outside the loops averagePi=(MPi/M) #determine average Pi value. #OUTPUT print("Pi estimate=",averagePi) aberror = abs(pi-valuePi) print("absolute error =",aberror)
I NEED HELP TO MAKE THIS CODE RUN AND GIVE ME A PI VALUE, WITHOUT CREATING A DIFFERENT CODE. COULD I PLEASE HAVE THIS ONE CHECKED.
#Python code for determining Pi using Monte Carlo
#INPUTS
#Defining constants
x_coord=0.0 #x coord of stone
y_coord=0.0 #y coord of stone
maxit=100 #upper limit to loop i.e. number of stones throw
rad=1.0 #radius of circle
square=2.0 #area of square
#Defining variables
valuePi=0.0 #variable to hold the value of Pi
incount=0 #variable to count splashes
M=5 #number of times for each N
MPi=0 #holder for sum of of Pi values for that N
averagePi=0 #average Pi value for the M times
i=0.0 #inner loop variable
j=0 #outer loop variable
#Defining the graphical displays
gdisplay() #define display for stones
inside=gdots(color=color.red)
outside=gdots(color=color.blue)
gdisplay() #define display for graph
plotpi=gdots(color=color.black)
#PROCESSING
#loop over M times
for j in range(1,M+1):
#reset variables
i=0
incount=0
valuePi=0
#delete graph contents for quarter circle
outside.delete()
inside.delete()
#Throw N stones
for i in range(0,maxit): #do this maxit tmes
rate(10) #slow down visualisation
#Throw a stone
x_coord=random()
y_coord=random()
#Check if stone splashed
if sqrt(x_coord**2 + y_coord**2)<=rad:
incount= incount+1 #update splashes
inside.plot(pos=(x_coord,y_coord)) #plot stone in blue
else: #if stone is outside just plot
outside.plot(pos=(x_coord,y_coord))
#Determine current value of Pi
valuePi= ((incount / maxit)*square)/rad
#Plot current value of Pi
plotpi.plot(pos=(i, valuePi))
MPi=MPi+valuePi #put M values of Pi into a container
#This is outside the loops
averagePi=(MPi/M) #determine average Pi value.
#OUTPUT
print("Pi estimate=",averagePi)
aberror = abs(pi-valuePi)
print("absolute error =",aberror)
Step by step
Solved in 3 steps with 2 images