Relevant code and results for yesterday's post on 'Estimating the value of \pi'



[]














In [58]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline


In [77]:
a = 100
alist = np.arange(0,a+1,1)
anotherlist = np.zeros(iterations)
count = 0.
iterations = 100
for i in xrange(iterations):
    tempx = np.random.choice(alist)
    tempy = np.random.choice(alist)
    if tempx**2 + tempy**2 < (a**2)+1:
        count += 1.
    anotherlist[i] = count/(i+1)*4.


In [78]:
print anotherlist[-1]
plt.plot(anotherlist)




3.0


Out[78]:


[<matplotlib.lines.Line2D at 0xa8b9f58c>]








In [85]:
a = 100
alist = np.arange(0,a+1,1)
andanotherlist = []
for iterations in [10+1,10**2+1,10**3+1,10**4+1,10**5+1,10**6+1]:
    #anotherlist = np.zeros(iterations)
    count = 0.
    #iterations = 100
    for i in xrange(iterations):
        tempx = np.random.choice(alist)
        tempy = np.random.choice(alist)
        if tempx**2 + tempy**2 < (a**2)+1:
            count += 1.
        #anotherlist[i] = count/(i+1)*4.
    andanotherlist.append(count/iterations*4.)


In [86]:
plt.plot(andanotherlist,'o')




Out[86]:


[<matplotlib.lines.Line2D at 0xa8a019ec>]








In [87]:
a = 1000
alist = np.arange(0,a+1,1)
andanotherlist = []
for iterations in [10+1,10**2+1,10**3+1,10**4+1,10**5+1,10**6+1]:
    #anotherlist = np.zeros(iterations)
    count = 0.
    #iterations = 100
    for i in xrange(iterations):
        tempx = np.random.choice(alist)
        tempy = np.random.choice(alist)
        if tempx**2 + tempy**2 < (a**2)+1:
            count += 1.
        #anotherlist[i] = count/(i+1)*4.
    andanotherlist.append(count/iterations*4.)


In [88]:
plt.plot(andanotherlist,'o')




Out[88]:


[<matplotlib.lines.Line2D at 0xa89cffec>]








In []:





Popular posts from this blog

Animation using GNUPlot

Pandas download statistics, PyPI and Google BigQuery - Daily downloads and downloads by latest version

Adaptive step size Runge-Kutta method