Question

In: Computer Science

Python Problem 4. Estimate pi version 2: write a program that will quickly estimate pi to...

Python

Problem 4. Estimate pi version 2: write a program that will quickly estimate pi to a precision of 1e-4 using a monte carlo approach. Your program should employ a loop where each iteration produces a new x,y pair. One will then determine whether the x,y pair lies inside or outside the circle. (see below) Since all the points will land within the square shown above but only a fraction will land within the circle, we can estimate pi by relating the area of the quarter circle shown above to the area of the square. As we randomly choose points, we can determine if they fall within the circle or not and form a ratio as shown below: AreaQtrcircle / AreaSqaure = πr 2 / 4r 2 = π / 4 = Points in Circle / All Points, π ≅ 4* Points in Circle / All Points.

Your loop should continue until the absolute value of the difference between the current estimate and the previous estimate is less than 1e-4. The program should output the estimate of pi and the number of iterations (points) needed to reach the desired level of precision. Run your program several times to see if the number of points changes. Note any observations in the comments.

Solutions

Expert Solution

import random

prevEstimate = None
currentEstimate = None

iterations = 0
pointsInCircle = 0
pointsInSquare = 0

while True:
        (x, y) = (random.random(), random.random())
        iterations += 1

        if x*x + y*y <= 1:
                pointsInCircle += 1
        pointsInSquare += 1     

        # min 100 iterations should be done.
        currentEstimate = 4.0 * pointsInCircle / pointsInSquare
        if prevEstimate is None or iterations <= 100:
                prevEstimate = currentEstimate
        elif abs(prevEstimate - currentEstimate) < 10**-4:
                break
        else:
                prevEstimate = currentEstimate

print('Value:', currentEstimate)
print('Iterations', iterations)
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

on python 3.6: Write a program that will quickly estimate pi to a precision of 1e-4...
on python 3.6: Write a program that will quickly estimate pi to a precision of 1e-4 using Archimedes approach of averaging polygon perimeters. Your program should employ a loop where the number of sides of the polygon are increased each iteration until the absolute value of the difference between the current estimate and the previous estimate is less than 1e-4. l. The program should output the estimate of pi and the number of iterations needed to reach the desired level...
Python Problem Problem 1: In this problem you are asked to write a Python program to...
Python Problem Problem 1: In this problem you are asked to write a Python program to find the greatest and smallest elements in the list. The user gives the size of the list and its elements (positive and negative integers) as the input. Sample Output: Enter size of the list: 7 Enter element: 2 Enter element: 3 Enter element: 4 Enter element: 6 Enter element: 8 Enter element: 10 Enter element: 12 Greatest element in the list is: 12 Smallest...
Write a Python Version 3 program that will ask you the loan amount and the interest...
Write a Python Version 3 program that will ask you the loan amount and the interest rate. Then the program will tell you - The monthly payment, How many months it will take you to pay off the loan, The total amount of interest paid over the life of the loan. Program should also indicate the final payment as in many cases the final payment at the end of the loan would not be exactly the normal monthly payment and...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and...
PYTHON Program Problem Statement: Write a Python program that processes information related to a rectangle and prints/displays the computed values. The program will behave as in the following example. Note that in the two lines, Enter length and Enter width, the program does not display 10.0 or 8.0. They are values typed in by the user and read in by the program. The first two lines are text displayed by the program informing the user what the program does. This...
Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
Program in Python Problem Statement Write a program with the following functions:  wordCount. This function...
Program in Python Problem Statement Write a program with the following functions:  wordCount. This function should accept a string as a parameter and return the number of words contained in the string.  mostFrequentWord. This function accepts a string as a parameter and returns the word that occurs the most frequently in the string.  replaceWord. This function accepts three strings as parameters, let’s call them string1, string2, and string3. It searches string1 for all occurrences of string2. When...
Write a Python program that has a list of 5 numbers [2, 3, 4, 5, 6)....
Write a Python program that has a list of 5 numbers [2, 3, 4, 5, 6). Print the first 3 elements from the list using slice expression. a. Extend this program in a manner that the elements in the list are changed to (6, 9, 12, 15, 18) that means each element is times 3 of the previous value. b. Extend your program to display the min and max value in the list.
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
Another less efficient way to estimate pi is the Gregory-Leibniz series: pi = 4/1 -4/3 +...
Another less efficient way to estimate pi is the Gregory-Leibniz series: pi = 4/1 -4/3 + 4/5 -4/7 + 4/9... . Use a while loop to approximate pi using this technique (5 points). Continue the calculations until the absolute value of the difference between the value of pi stored in MATLAB and the approximation is less than 0.001. Report the final approximation and the number of iterations required (5 points). Hint: to alternate the sign on each term use (-1)...
Another less efficient way to estimate pi is the Gregory-Leibniz series: pi = 4/1 -4/3 +...
Another less efficient way to estimate pi is the Gregory-Leibniz series: pi = 4/1 -4/3 + 4/5 -4/7 + 4/9... . Use a while loop to approximate pi using this technique (5 points). Continue the calculations until the absolute value of the difference between the value of pi stored in MATLAB and the approximation is less than 0.001. Report the final approximation and the number of iterations required (5 points). Hint: to alternate the sign on each term use (-1)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT