Question

In: Computer Science

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 of precision.

the code for the finding pi using the Archimedes approach is:

numside_ins=float(input(enter the number of sides of the inscribed polygon))

numside_cis=float(input(enter the number of sides of the circumscribed polygon))

diameter= 1
length_ins= 2*(diameter/2)*sin(pi/numside_ins)
peri_ins= numside_ins*length_ins
length_cir= 2*(diameter/2)*tan(pi/numside_cir)
peri_cir = numside_cir * length_cir
ave_peri=((peri_cir + peri_ins) / 2)
found_pi= ave_peri/diameter
print(found_pi)

Solutions

Expert Solution

from math import sin,pi,tan
numside_ins=2
numside_cir=2
diameter= 1
length_ins= 2*(diameter/2)*sin(pi/numside_ins)
peri_ins= numside_ins*length_ins
length_cir= 2*(diameter/2)*tan(pi/numside_cir)
peri_cir = numside_cir * length_cir
ave_peri=((peri_cir + peri_ins) / 2)
currentEstimate = ave_peri/diameter
iterations=1
while True:
numside_ins+=1
numside_cir+=1
diameter= 1
length_ins= 2*(diameter/2)*sin(pi/numside_ins)
peri_ins= numside_ins*length_ins
length_cir= 2*(diameter/2)*tan(pi/numside_cir)
peri_cir = numside_cir * length_cir
ave_peri=((peri_cir + peri_ins) / 2)
previousEstimate = currentEstimate
currentEstimate = ave_peri/diameter
iterations+=1
if abs(currentEstimate-previousEstimate)<1e-4:
break
  
print('Estimate of pi:',currentEstimate)
print('Number of iterations:',iterations)


Related Solutions

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...
In Python, write a program that computes machine epsilon (macheps) in: Single precision (32 bits) and...
In Python, write a program that computes machine epsilon (macheps) in: Single precision (32 bits) and Double precision (64 bits)
write value of PI(3.14159) in IEEE-754 single-precision format
write value of PI(3.14159) in IEEE-754 single-precision format
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)...
Write a Python code that when the temperature of the CPU in the raspberry pi exceeds...
Write a Python code that when the temperature of the CPU in the raspberry pi exceeds 40 degrees Celsius you receive an email.
[4 marks] Write a Python program for a “Deliver-Eat!” app that asks the user for their...
[4 marks] Write a Python program for a “Deliver-Eat!” app that asks the user for their tip amount. If the user tips less than $5 they should receive a 1-star rating. If the user tips $5 or more they should receive a 5-star rating. Your program should match the output below. User input is in red. >>> How much would you like to tip? $4.50 >>> Here is your rating! * >>> How much would you like to tip? $7.20...
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT