Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. __author__ = 'elianasullivan'
  2.  
  3. #THIS CODE GRAPHS HOW THE ESTIMATE OF PI CHANGES WITH ERROR BARS THAT ARE EQUAL TO THE ABS(MATH.PI-PI_ESTIMATE)
  4.  
  5. import random
  6. import math
  7. import matplotlib.pyplot as plt
  8.  
  9. def error_bar_estimator(min_size,max_size, step):
  10.  
  11. circle_count = 0
  12. square_count = 0
  13.  
  14. sampsize_list = []
  15. error_list = []
  16. estimates_list = []
  17.  
  18. for samp_size in range(min_size,max_size,step):
  19. for dart in range(samp_size):
  20. x_coord = random.random()
  21. y_coord = random.random()
  22. if ((x_coord)**2)+((y_coord)**2) <= 1:
  23. circle_count += 1
  24. square_count += 1
  25. else:
  26. square_count += 1
  27.  
  28. #calculate the error of each estimate
  29. pi_estimate = 4*(float(circle_count)/square_count)
  30. error = abs((math.pi)-(pi_estimate))
  31.  
  32. #create lists to prepare for graphing
  33. estimates_list.append(pi_estimate)
  34. sampsize_list.append(samp_size)
  35. error_list.append(error)
  36.  
  37. #creates graph
  38. plt.errorbar(sampsize_list,estimates_list, yerr=error_list, linewidth = 1.5)
  39. plt.xlabel("Number of Darts")
  40. plt.ylabel("Pi Estimate")
  41. plt.title("Pi Estimate with Varying Number of Darts")
  42. plt.plot((0, max_size), (math.pi, math.pi), linewidth=2.0)
  43. plt.show()
  44.  
  45. print error_bar_estimator(1000,10000,50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement