Question

In: Computer Science

Part  2: Approximating the Value of Pi Mathematical constant pi it most often to find the circumference...

Part  2: Approximating the Value of Pi

Mathematical constant pi it most often to find the circumference or the area of a circle. For simplicity, the value that is commonly used for pi is 3.14. However, pi is actually an irrational number, meaning that that it has an infinite, nonrepeating number of decimal digits. The value is

3.1415926535897932384626433832795028841971693993751058209749445923078164062…

In this lab, we will approximate the value of pi using a technique known as Monte Carlo Simulation. This means that we will use random numbers to simulate a “game of chance”. The result of this game will be an approximation for pi.

Setup

The game that we will use for this simulation is “darts”. We will “randomly” throw a number of darts at a specially configured dartboard. The set up for our board is shown below. In the figure, you can see that we have a round dartboard mounted on a square piece of wood. The dartboard has a radius of one unit. The piece of wood is exactly two units square so that the round board fits perfectly inside the square.

But how will this help us to approximate pi? Consider the area of the circular dartboard. It has a radius of one so its area is pi. The area of the square piece of wood is 4 (2 x 2). The ratio of the area of the circle to the area of the square is pi/4. If we throw a whole bunch of darts and let them randomly land on the square piece of wood, some will also land on the dartboard. The number of darts that land on the dartboard, divided by the number that we throw total, will be in the ratio described above (pi/4). Multiply by 4 and we have pi.

Throwing Darts

Now that we have our dartboard setup, we can throw darts. We will assume that we are good enough at throwing darts that we always hit the wood. However, sometimes the darts will hit the dartboard and sometimes they will miss.

In order to simulate throwing the darts, we can generate two random numbers between zero and one. The first will be the “x coordinate” of the dart and the second will be the “y coordinate”. However, we have a problem. The coordinates for the dartboard go from -1 to 1.

How can we turn a random number between 0 to 1 into a random number between -1 and 1? We know that random.random() will return a number between 0 and 1. We may obtain a number between -1 and 1by calculating random.random() *2 -1.

Activity 4: The program has been started for you. You need to fill in the part that will “throw the dart”. Once you know the x,y coordinate, have the turtle move to that location and make a dot. by using the dot() function of turtle. Note that the tail is already up so it will not leave a line.

import turtle

import math

import random

wn = turtle.Screen()

wn.setworldcoordinates(-1,-1,1,1)

fred = turtle.Turtle()

fred.up()

numdarts = 50

for i in range(numdarts):

randx = random.random()

randy = random.random()

x =

y =

wn.exitonclick()

Counting Darts

We already know the total number of darts being thrown. The variable numdarts keeps this for us. What we need to figure out is how many darts land in the circle? Since the circle is centered at (0,0) and it has a radius of 1, the question is really simply a matter of checking to see whether the dart has landed within 1 unit of the center. Luckily, there is a turtle method called distance that will return the distance from the turtle to any other position. It needs the x,y for the other position.

For example, fred.distance(0,0) would return the distance from fred’s current position to position (0,0), the center of the circle.

Now we simply need to use this method in a conditional to ask whether fred is within 1 unit from the center. If so, color the dart red, otherwise, color it blue. Also, if we find that it is in the circle, count it. Create an accumulator variable, call it insideCount, initialize it to zero, and then increment it when necessary. Remember that the increment is a form of the accumulator pattern using reassignment.

The Value of Pi

After the loop has completed and visualization has been drawn, we still need to actually compute pi and print it. Use the relationship insideCount/numdarts *4, why?

Run your program with larger values of numdarts to see if the approximation gets better. If you want to speed things up for large values of numdarts, like 1000, set the tracer to be 100 using wn.tracer(100).

NB: The language is Python.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

import turtle
import math

import random

wn = turtle.Screen()
wn.tracer(100)

wn.setworldcoordinates(-1,-1,1,1)

fred = turtle.Turtle()
fred.up()
fred.speed(0)
fred.goto(0,0)



numdarts = 1000
insideCount=0 #number of dots inside

for i in range(numdarts):
    randx = random.random()
    randy = random.random()
    #adjusting randx and randy to be within -1,1 range
    x =randx*2 -1
    y =randy*2-1
    #moving turtle to above coordinates
    fred.goto(x, y)
    #finding distance from center coordinate
    dist_from_center=fred.distance(0,0)
    #if this distance is less than or equal to 1, it means that the dot is
    #inside circle,else not
    if dist_from_center<=1:
        #updating insideCount
        insideCount+=1
        #using red color
        fred.pencolor('red')
    else:
        #using blue color
        fred.pencolor('blue')
    #drawing the dot
    fred.dot()

#finding the value of pi by dividing insideCount by numDarts and multiplying by 4
pi=(insideCount/numdarts) * 4
#displaying this value to the console.
print('Estimated value for PI is',pi)
wn.exitonclick()

#output when numdarts=1000

#output when numdarts=10000


Related Solutions

a) Give the mathematical equilibrium constant expression Kc and Kp (not the value!) for the reaction...
a) Give the mathematical equilibrium constant expression Kc and Kp (not the value!) for the reaction 2N2O5(g) ↔ 4NO2 (g) + O2(g) b) At 250oC the concentrations of N2O5, NO2 and O2 are 0.100 mol/L, 0.0283 mol/L and 0.0105 mol/L. Calculate the values for Kc and Kp
1. Create a symbolic constant called PI in two different manners. For both, the value that...
1. Create a symbolic constant called PI in two different manners. For both, the value that is associated with the constant should be 3.14159 2. x = 0; y = 5; Is the following condition true or false? if ( x > 0 and x < 10 or y = = 4) 3. What statement is used to bring in libraries at the top of your C program?
In statistics, the mode of a set of values is the value that occurs most often...
In statistics, the mode of a set of values is the value that occurs most often or with the greatest frequency. Write a function that accepts as arguments the following: An array of integers An integer that indicates the number of elements in the array The function should determine the mode of the array. That is, it should determine which value in the array occurs most often. The mode is the value the function should return. If the array has...
   \(\int_{0}^{\pi}e^xsinx dx\) with n=6 subdivisions 1) Find the exact value of the integral 2) Approximate...
   \(\int_{0}^{\pi}e^xsinx dx\) with n=6 subdivisions 1) Find the exact value of the integral 2) Approximate the integral using the trapezoidal rule, midpoint rule and simpson rule. 3) Find the errors using each of these rules 4) For each of these rules, show how large n should be, to be within .0001 of the actual answer.
   \(\int_{0}^{\pi}e^xsinx dx\) with n=6 subdivisions 1) Find the exact value of the integral 2) Approximate...
   \(\int_{0}^{\pi}e^xsinx dx\) with n=6 subdivisions 1) Find the exact value of the integral 2) Approximate the integral using the trapezoidal rule, midpoint rule and simpson rule. 3) Find the errors using each of these rules 4) For each of these rules, show how large n should be, to be within .0001 of the actual answer.
Submit a c++ file: 2. Write a program that defines the named constant PI, const double...
Submit a c++ file: 2. Write a program that defines the named constant PI, const double PI = 3.14159;, which stores the value of p. The program should use PI and the functions listed in Table 6-1 to accomplish the following: a. Output the value of Pi Output the value of Pi. b. Prompt the user to input the value of a double variable r, which stores the radius of a sphere. The program then outputs the following: i.   The...
Find the real Fourier series of the piece-wise continuous periodic function f(x)=1+x+x^2 -pi<x<pi
Find the real Fourier series of the piece-wise continuous periodic function f(x)=1+x+x^2 -pi<x<pi
Common stock valuelong dash—Constant growth   Use the​ constant-growth model​ (Gordon model) to find the value of...
Common stock valuelong dash—Constant growth   Use the​ constant-growth model​ (Gordon model) to find the value of each firm shown in the following​ table:  ​(Click on the icon located on the​ top-right corner of the data table below in order to copy its contents into a​ spreadsheet.) Firm Dividend expected next year Dividend growth rate Required return A ​$1.201.20 8.08.0​% 13.013.0​% B 4.004.00 5.05.0 15.015.0 C 0.650.65 10.010.0 14.014.0 D 6.006.00 8.08.0 9.09.0 E 2.252.25 8.08.0 20.020.0
2. Find the value of ? in the following cases: a. ?(−? ≤ ? ≤ ?)...
2. Find the value of ? in the following cases: a. ?(−? ≤ ? ≤ ?) = 0.6840 b. ?(? ≤ ? ≤ 1.40) = .8240 c. ?(−1.20 ≤ ? ≤ ?) = .5860
Part A. Find the horizontal and vertical asymptotes of ?(?)= (6x^2) / (7(x^2 + 8)) Part...
Part A. Find the horizontal and vertical asymptotes of ?(?)= (6x^2) / (7(x^2 + 8)) Part B.  Find the horizontal and vertical asymptotes of ?(?)= (x^2 - 3) / (x^2 + 2x - 8) Part C. Find the horizontal and vertical asymptotes of ?(?)= (x^2 - 49) / (3x^2 - 75) Part D. Find the horizontal and vertical asymptotes of ?(?)= (x^3 - 6) / (x^2 + 14x + 49)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT