python: Instructions You are expected to write your own program to approximate t
ID: 3599674 • Letter: P
Question
python:
Instructions You are expected to write your own program to approximate the value of Pi (), using the method proposed by Archimedes (circa 250 BCE) Archimedes started by imagining a circle with circumference equal to (ie, a circle with diameter 1, radius %). He then imagined successive polygons within the circle, calculating the sum of the polygon's edges as an approximation to the circumference (and thus ). As the number of sides (n) in the polygon increases, the sum of the polygon's sides becomes a better and better estimate for . estimated= n sin 1 180 estimatednsin For the second part of this lab, write a loop that tries successive approximations for , starting with n-3 and continuing until the estimate is close enough to . The value math.pi can be used as an adequate approximation for . Use an epsilon of 0.00000000001. When you find the result, print the estimate (with a label), and also print the polygon size (n). How many iterations does it take? Can you think of any ways to improve this algorithm?Explanation / Answer
import math
# return the estimted vale of pi for n sided polygon
def getPI(n):
return n * math.sin(180 / n)
# save the original value of pi in pie
pie = math.pi
n = 3
# infinite loop
while True:
print('n :', n)
# get the estimted vale of pi for n sided polygon
pi_estimate = getPI(n)
error = abs(pi_estimate - pie)
# if the error between them is less than 0.00000000001
if(error <= 0.00000000001):
print('Estimated value of PI : ', pi_estimate)
print('Polygon is of side : ', n)
print('Number of Iterations : ', (n-3))
break
n += 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.