Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. __author__ = 'elianasullivan'
  2. import random
  3. import math
  4. import numpy as np
  5. import pylab
  6. from scipy.optimize import curve_fit
  7.  
  8.  
  9. def error_graph(min_size,max_size,step):
  10. circle_count = 0
  11. square_count = 0
  12.  
  13. sampsize_list = []
  14. error_list = []
  15. estimates_list = []
  16.  
  17. for samp_size in range(min_size,max_size,step):
  18. for dart in range(samp_size):
  19. x_coord = random.random()
  20. y_coord = random.random()
  21. if ((x_coord)**2)+((y_coord)**2) <= 1:
  22. circle_count += 1
  23. square_count += 1
  24. else:
  25. square_count += 1
  26.  
  27. pi_estimate = 4*(float(circle_count)/square_count)
  28. error = abs((math.pi)-(pi_estimate))
  29.  
  30. estimates_list.append(pi_estimate)
  31. sampsize_list.append(samp_size)
  32. error_list.append(error)
  33.  
  34. #makes a function to define what kind of best fit line I am looking for. I want something exponential.
  35. def line(sampsize_list,x):
  36. return sampsize_list**x
  37.  
  38. #finds the best fit value to fit the function n^x when n = sample size
  39. popt, pcov = curve_fit(line,sampsize_list,error_list)
  40. popt
  41. print "The exponent of your best fit line is:",popt
  42.  
  43. #graps the fluctuations in error bars (in blue) and the best fit line generated above (in purple)
  44. x = np.linspace(min_size,max_size,step)
  45. y = x**popt
  46.  
  47. # compose plot
  48. pylab.plot(sampsize_list,error_list, color="blue")
  49. pylab.plot(x,y, color= "purple")
  50. pylab.xlabel("Number of Darts"), pylab.ylabel("Error"), pylab.title("Error Values with Varying Number of Darts")
  51. #pylab.ylim(0,0.1) #uncomment this to see a zoomed in view
  52.  
  53. pylab.show() # show the plot
  54.  
  55. print error_graph(1000,100000,50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement