Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __author__ = 'elianasullivan'
- import random
- import math
- import numpy as np
- import pylab
- from scipy.optimize import curve_fit
- def error_graph(min_size,max_size,step):
- circle_count = 0
- square_count = 0
- sampsize_list = []
- error_list = []
- estimates_list = []
- for samp_size in range(min_size,max_size,step):
- for dart in range(samp_size):
- x_coord = random.random()
- y_coord = random.random()
- if ((x_coord)**2)+((y_coord)**2) <= 1:
- circle_count += 1
- square_count += 1
- else:
- square_count += 1
- pi_estimate = 4*(float(circle_count)/square_count)
- error = abs((math.pi)-(pi_estimate))
- estimates_list.append(pi_estimate)
- sampsize_list.append(samp_size)
- error_list.append(error)
- #makes a function to define what kind of best fit line I am looking for. I want something exponential.
- def line(sampsize_list,x):
- return sampsize_list**x
- #finds the best fit value to fit the function n^x when n = sample size
- popt, pcov = curve_fit(line,sampsize_list,error_list)
- popt
- print "The exponent of your best fit line is:",popt
- #graps the fluctuations in error bars (in blue) and the best fit line generated above (in purple)
- x = np.linspace(min_size,max_size,step)
- y = x**popt
- # compose plot
- pylab.plot(sampsize_list,error_list, color="blue")
- pylab.plot(x,y, color= "purple")
- pylab.xlabel("Number of Darts"), pylab.ylabel("Error"), pylab.title("Error Values with Varying Number of Darts")
- #pylab.ylim(0,0.1) #uncomment this to see a zoomed in view
- pylab.show() # show the plot
- print error_graph(1000,100000,50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement