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

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...
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,...
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 in Python and as 2 seperate programs Write a program that allows the user to...
Write in Python and as 2 seperate programs Write a program that allows the user to enter the total rainfall for each of 12 months into a LIST. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts. Data: January 7.9 inches February 10.1 inches March 3.4 inches April 6.7 inches May 8.9 inches June 9.4 inches July 5.9 inches August 4.1 inches September...
Write a modified version of the program below. In this version, you will dynamically allocate memory...
Write a modified version of the program below. In this version, you will dynamically allocate memory for the new C-string and old C-string, using the new keyword. Your program should ask the user for the size of the C-string to be entered, and ask the user for the C-string, then use new to create the two pointers (C-strings).   Then, call Reverse, as before. Don’t forget to use delete at the end of your program to free the memory! #include <iostream>...
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)...
Write a complete and syntactically correct Python program to solve the following problem: You are the...
Write a complete and syntactically correct Python program to solve the following problem: You are the payroll manager for SoftwarePirates Inc. You have been charged with writing a package that calculates the monthly paycheck for the salespeople. Salespeople at SoftwarePirates get paid a base salary of $2000 per month. Beyond the base salary, each salesperson earns commission on the following scale: Sales Commission Rate Bonus <$10000 0% 0 $10000 – $100,000 2% 0 $100,001 - $500,000 15% $1000 $500,001 -...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT