Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Background Monte Carlo method The showMontePi function computes an approximate value for pi, and graphically displays the Monte Carlo simulation. In this project you will

Background Monte Carlo method

The showMontePi function computes an approximate value for pi, and graphically displays the Monte Carlo simulation. In this project you will implement, and then revise, the showMontePi function described in chapter 2.6 of the Miller and Ranum text. Then you will add code to showMontePi to compare the value for generated by showMontePi to the value of math.pi.

Requirements

(1)Look at showMontePi,which implements the same algorithm to approximate as in montePi, and also displays a graphical visualization of the algorithm, per the description in ch. 2.6.5 of your text. Starter code for showMontePi is given on p. 78 of the text; a slightly revised (and improved avoids use of circle and pi as variable names, and uses from turtle import * and the anonymous turtle) version is available at the CIS 210 website. Read and test the code thoroughly to make sure you understand it. Then add the docstring and revise it to call isInCircle. Test your changes.

(2) Write a new function, drawBoard, to draw the dartboard (copy the code from the beginning of showMontePi). drawBoard has no parameters and returns None. When drawboard is working, revise showMontePi to call drawBoard.

(3) After the graphics capability has been implemented, write a new function, reportPi, which will be called from showMontePi. reportPi will compute the difference between the approximate value for generated by the Monte Carlo simulation and the math.pi value in the math library, then report (print) the percentage error (rounded to 2 decimal places), assuming that the value returned by the math library function is correct. reportPi will have two parameters, numDarts, the number of darts in the simulation, and approxPi, the approximate value of pi generated in showMontePi. reportPi returns None. Add code to showMontePi to call reportPi.

Note that showMontePi should still return the approximate value of . That is, showMontePi will return a value and draw the graphics animation of the algorithm and print a short report.

(4) Finally, write a main function that calls showMontePi. Note that due to the randomness in the algorithm, approximations may vary slightly. The last line in your .py file should be a call to function main: main().

>>> showMontePi(1000)

With 1000 iterations:

my approximate value for pi is: 3.104

math lib pi value is: 3.141592653589793

This is a 1.2 percent error.

3.104

STARTER CODE: from turtle import * import math import random def showMontePi(numDarts): ''' DOCSTRING written according to 210 style guidelines goes here ''' # set up canvas and turtle # to animate the algorithm; # draw x, y axes # new drawBoard function will replace wn = Screen() wn.setworldcoordinates(-2, -2, 2, 2) speed(0); hideturtle() penup() goto(-1, 0) pendown() goto(1, 0) penup() goto(0, 1) pendown() goto(0, -1) penup() goto(0, -1) # pen should stay up for drawing darts inCircleCt = 0 # throw the darts and check whether # they landed on the dart board and # keep count of those that do for i in range(numDarts): x = random.random() y = random.random() # Revise code to call new isInCircle function. # See Project 3-2 and 3-3 specifications. d = math.sqrt(x**2 + y**2) # show the dart on the board if d < 1: inCircleCt += 1 color('blue') else: color('red') goto(x, y) dot() # calculate approximate pi approxPi = inCircleCt/numDarts * 4 # ADD CODE HERE to compare approxPi to math.pi # call reportPi function to do this. #wn.exitonclick() return approxPi #add main function, too - definition and call

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Luc Perkins, Eric Redmond, Jim Wilson

2nd Edition

1680502530, 978-1680502534

More Books

Students also viewed these Databases questions