Sei sulla pagina 1di 11

Simulation of Maxwell Distribution

In [68]:
import random
import math
Array p consists of the momentum of all the 50 particles at any point
In [277]:
p=[]
We initialise all the momentum components to have some random value between -1 and 1. Since the

assigned value is random we ensure that the conditions : i pxi = 0 and i pyi = 0
In [278]:
for i in range(50):
p.append((random.uniform(-1, 1),random.uniform(-1, 1)))
The function exchange gives us the resultant momentum when two particles collide at a random
angle . The function takes to 2 momentum as an input and gives the final momentum of the 2
particles as an output.
In [111]:
def exchange(p1,p2):
th = random.uniform(0,
p3=(0.5*(p1[0]-p2[0])*
0.5*(p1[1]-p2[1])*
0.5*(p2[0]-p1[0])*
0.5*(p1[1]-p2[1])*
p4=(0.5*(p2[0]-p1[0])*
0.5*(p2[1]-p1[1])*
0.5*(p1[0]-p2[0])*
0.5*(p2[1]-p1[1])*
return (p3,p4)

2*math.pi)
math.cos(th)+
math.sin(th)+
math.sin(th)+
math.cos(th)+
math.cos(th)+
math.sin(th)+
math.sin(th)+
math.cos(th)+

0.5*(p1[0]+p2[0]),
0.5*(p1[1]+p2[1]))
0.5*(p1[0]+p2[0]),
0.5*(p1[1]+p2[1]))

We now take 2 particles at random and make them collide. We replace the old momentum values
with the new ones. We do this around 100000 times to make sure that we get the appropriate
porbability distribution at the end.We store the momentum value of the first particle at each time in the
variable pdist and the speed in momdist.
In [308]:
pdist=[]
momdist=[]
for iterate in range(1000000):

for iterate in range(1000000):


i=random.randint(0,49)
j=random.randint(0,49)
(p[i],p[j])=exchange(p[i],p[j])
pdist.append(p[36][0])
momdist.append(math.sqrt(p[36][0]**2+p[36][1]**2))
In [101]:
%matplotlib inline
from IPython.core.pylabtools import figsize
from matplotlib import pyplot as plt
plt.style.use('bmh')
import numpy as np
We plot the variable pdist in the form of a histogram and show that it follows the normal curve having
mean=0 and standard deviation of around 0.5 (Which is the mean and standard deviation of the initial
momentum of the particles)
In [314]:
figsize(12.5, 5)
def f(t):
return 305000*np.exp(-1.5*t**2)
plt.hist(pdist, bins=10, alpha=1,
color="#467821")
t = np.arange(-2, 2 , 0.02)
plt.plot(t, f(t) , 'k');

The probability distribution of speed is given by the Maxwell Distribution. We can see that the
v2

[ ]
2

probability p(v) v exp 2

In [316]:
plt.hist(momdist, bins=30, alpha=1,color="#467821");
def f(t):
return 270000*t*np.exp(-1.5*t**2)
t = np.arange(0, 2.5 , 0.02)

t = np.arange(0, 2.5 , 0.02)


plt.plot(t, f(t) , 'k');

Particles 2 and 10 have plotted as well to show that the same probability distribution holds for any of
the other 50 particles.
In [387]:
p=[]
for i in range(50):
p.append((random.uniform(-1, 1),random.uniform(-1, 1)))
pdist=[]
momdist=[]
for iterate in range(100000):
i=random.randint(0,49)
j=random.randint(0,49)
(p[i],p[j])=exchange(p[i],p[j])
pdist.append(p[2][0])
momdist.append(math.sqrt(p[2][0]**2+p[2][1]**2))
def f(t):
return 26000*np.exp(-2*t**2)
plt.hist(pdist, bins=10, alpha=1,
color="#348ABD")
t = np.arange(-2, 2 , 0.02)
plt.plot(t, f(t) , 'k');

In [392]:
plt.hist(momdist, bins=30, alpha=1,color="#348ABD");
def f(t):
return 23000*t*np.exp(-1.5*t**2)
t = np.arange(0, 2.5 , 0.02)
plt.plot(t, f(t) , 'k');

In [386]:
p=[]
for i in range(50):
p.append((random.uniform(-1, 1),random.uniform(-1, 1)))
pdist=[]
momdist=[]
for iterate in range(100000):
i=random.randint(0,49)
j=random.randint(0,49)
(p[i],p[j])=exchange(p[i],p[j])
pdist.append(p[10][0])
momdist.append(math.sqrt(p[10][0]**2+p[10][1]**2))
def f(t):
return 27000*np.exp(-2*t**2)
plt.hist(pdist, bins=10, alpha=1,
color="#A60628")
t = np.arange(-2, 2 , 0.02)
plt.plot(t, f(t) , 'k');

In [393]:
plt.hist(momdist, bins=30, alpha=1,color="#A60628");
def f(t):
return 23000*t*np.exp(-1.5*t**2)
t = np.arange(0, 2.5 , 0.02)
plt.plot(t, f(t) , 'k');

We see that even for a 5 particle system the maxwell distribution works pretty well.
In [395]:
p=[]
for i in range(5):
p.append((random.uniform(-1, 1),random.uniform(-1, 1)))
pdist=[]
momdist=[]
for iterate in range(100000):
i=random.randint(0,4)
j=random.randint(0,4)
(p[i],p[j])=exchange(p[i],p[j])
pdist.append(p[1][0])
momdist.append(math.sqrt(p[1][0]**2+p[1][1]**2))
def f(t):
return 19000*np.exp(-1.5*t**2)
plt.hist(pdist, bins=10, alpha=1,
color="#7A68A6")
t = np.arange(-2, 2 , 0.02)
plt.plot(t, f(t) , 'k');

In [402]:
plt.hist(momdist, bins=30, alpha=1,color="#7A68A6");
def f(t):
return 40000*t*np.exp(-1.3*t**2)
t = np.arange(0, 2.5 , 0.02)
plt.plot(t, f(t) , 'k');

Next we look at the probability distribution in case of inelastic collisions. Function exchange_inelastic
returns the final momentum of 2 particles when the coefficient of restitution is e.
In [196]:
def exchange_inelastic(p1,p2,e):
th = random.uniform(0, 2*math.pi)
p3=(0.5*e*(p1[0]-p2[0])* math.cos(th)+
0.5*e*(p1[1]-p2[1])* math.sin(th)+
0.5*e*(p2[0]-p1[0])* math.sin(th)+
0.5*e*(p1[1]-p2[1])* math.cos(th)+
p4=(0.5*e*(p2[0]-p1[0])* math.cos(th)+
0.5*e*(p2[1]-p1[1])* math.sin(th)+
0.5*e*(p1[0]-p2[0])* math.sin(th)+
0.5*e*(p2[1]-p1[1])* math.cos(th)+
return (p3,p4)

0.5*(p1[0]+p2[0]),
0.5*(p1[1]+p2[1]))
0.5*(p1[0]+p2[0]),
0.5*(p1[1]+p2[1]))

We carry out the same simulation as before except now we consider inelastic collisions. Several plots
are made for coefficient of restitution e = 1, 0.999, , 0.0991. We see that as e decreases the standard
deviation reduces more and more and the probability distribution curve starts to peak even more at
the mean.
In [446]:
figsize(12.5, 40)

figsize(12.5, 40)
p=[]
for i in range(50):
p.append((random.uniform(-1, 1),random.uniform(-1, 1)))
pdist=[]
f, axarr = plt.subplots(10, sharey=True)
axarr[0].set_title('Probability Distribution \n e=1')
for e in range(10):
pdist=[]
for iterate in range(10000):
i=random.randint(0,49)
j=random.randint(0,49)
(p[i],p[j])=exchange_inelastic(p[i],p[j],1-e*0.001)
pdist.append(p[1][0])
if e!=0:
axarr[e].set_title('e=%.3f'%(1-e*0.001))
axarr[e].hist(pdist, bins=10, alpha=1, color="#348ABD");

The speed on the other hand follows the maxwell distribution. When we decrease the coefficient of
restitution the probability curve becomes sharper and the mean speed decreases. (somewhat
exponentially)
In [ ]:
import numpy as np
In [468]:
figsize(12.5, 40)
p=[]
momdist=[]
for i in range(50):
p.append((random.uniform(-1, 1),random.uniform(-1, 1)))
f, axarr = plt.subplots(10,sharey=True)
axarr[0].set_title('Probability Distribution \n e=1')
for e in range(10):
momdist=[]
p=[]
for i in range(50):
p.append((random.uniform(-1, 1),random.uniform(-1, 1)))
for iterate in range(100000):
i=random.randint(0,49)
j=random.randint(0,49)
(p[i],p[j])=exchange_inelastic(p[i],p[j],1-e*0.001)
momdist.append(math.sqrt(p[1][0]**2+p[1][1]**2))
if e!=0:
axarr[e].set_title('e=%.3f with mean %f'%((1-0.001*e)
,np.mean(momdist)))
axarr[e].hist(momdist, bins=60, alpha=1, color="#348ABD");

Potrebbero piacerti anche