MY CODE DOES NOT PRODUCE THE CORRECT PI VALUE, WHY?? #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)

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

MY CODE DOES NOT PRODUCE THE CORRECT PI VALUE, WHY??

#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)

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY