You are on page 1of 25

##regn. no.

: 144-1111-0302-21

##roll no. : 213144-21-0035

##autocorelation

import numpy as np

import matplotlib.pyplot as plt

def random_lcg(x,size):

a,c,m=2416,37441,1771875 #given

X=[]

for i in range(size):

x=(a*x+c)%m

r=x/m

X.append(r)

return X

N=int(input('Enter sample size:'))

seed=int(input('Enter seed value:'))

l=random_lcg(seed, N)

#Autocorelation

l_avg=np.mean(1)

c=lambda k:np.mean([(l[i]-l_avg)*(l[i+k]-l_avg) for i in range(N-k)])

r=lambda k: c(k)/c(0)

corr=[r(k) for k in range(1,1000)]

plt.subplot(121)

plt.title('Uniform random distribution using L.C.G')

plt.hist(1,bins=40, edgecolor='black')

plt.subplot(122)

plt.title('Autocorelation')

plt.plot(corr)

plt.savefig('autoc1.png')

plt.show()

'''

OUTPUT

Enter sample size:10000


Enter seed value:122

'''
##regn. no. : 144-1111-0302-21

##roll no. : 213144-21-0035

##Plot Dulong-Petit law, Einstien distribution function and Debye


distribution function for high temperature and low temperature

import numpy as np

import matplotlib.pyplot as plt

from scipy.integrate import quad

Theta_E=eval(input("Enter a value for the Einstein/Debye temperature: "))

Temp=np.linspace (0.0001, 1000,10000)

def integrate (T):

I=quad (lambda x: np.exp(x)*x**4/(np.exp(x)-1)**2,0, Theta_E/T)

return I[0]

Dulong_Petit=np.ones(10000)

Einstein=lambda Temp:
(Theta_E/Temp)**2*np.exp(Theta_E/Temp)/(np.exp(Theta_E/Temp)-1)**2

Debye=[3*(T/Theta_E)**3*integrate (T) for T in Temp]

print("OUTPUT")

plt.title("For Dulong and Petit's law, Einstien's theory and Debye


approximation")

plt.plot(Temp, Dulong_Petit, Temp, Einstein (Temp), Temp, Debye)

plt.xlim(0,50)

plt.ylim(0,1.15)

plt.xlabel("Temperature")

plt.ylabel("C_V")

plt.legend(["Dulong & Petit's Law", "Einstein's Theory", "Debye


Approximation"],loc= "best")

plt.savefig('dulong.png')

plt.grid()

plt.show()
import numpy as np

import matplotlib.pyplot as plt

a = int(input("Enter the the type of distribution you want to plot(-


1,1,0):"))

def distribution(E, T):

return 1 / (np.exp(E / (T * 8.6173e-5))+a)

def T_distribution(Temperatures):

energies = np.linspace(-1, 1, 100)

for T in Temperatures:

be_values = [distribution(E, T) for E in energies]

plt.plot(energies, be_values, label=f'Temperature: {T}K ')

plt.axvline(x = 0,color = 'y',linestyle = '--')

plt.axhline(y = 1,color = 'y',linestyle = '--' )

plt.ylim(0,1.5)

plt.xlabel('E')

plt.ylabel(' Distribution')

plt.legend()

plt.title('Distribution versus E at Different Temperatures')

plt.savefig("mbfdbe3.png")

plt.show()

Temperatures = [10, 300, 800, 2000,5000]

T_distribution(Temperatures)

plt.show()
##regn. no. : 144-1111- FERMI-DIRAC DISTRIBUTION 0302-21

MAXWELL-BOLTZMANN DISTRIBUTION

##roll no. : 213144-21-0035


BOSE-EINSTEIN DISTRIBUTION

##monte carlo

import numpy as np

def f(x):

return x/(1+x*x)

def MonteCarlo (a,b,N,f):

x=np.random.uniform(a,b,N)

return (b-a)*np.mean(f(x))

N=eval(input("Enter the number of points:"))


a, b=eval(input("Enter the range of integration:"))

I=MonteCarlo (a,b,N,f)

print("OUTPUT")

print("The result using MonteCarlo integration is: ",I)

'''

OUTPUT

Enter the number of points:10000

Enter the range of integration:0,1

OUTPUT

The result using MonteCarlo integration is: 0.3466336539132379

'''

##regn. no. : 144-1111-0302-21

##roll no. : 213144-21-0035

##Normal Random Number with mean mu and s.d sigma.

import numpy as np

import matplotlib.pyplot as plt

N=10000

mu=1

sigma=0.5

x=np.random.normal(mu,sigma,N)

plt.savefig('normal.png')

plt.hist(x)

plt.show()
##regn. no. : 144-1111-0302-21

##roll no. : 213144-21-0035

#Simulation of radioactive decay

import numpy as np

import matplotlib.pyplot as plt

lam=eval(input("Enter the value of decay constatnt:"))

NO=eval(input("Enter the initial number of nuclie:"))

tfinal=eval(input("Enter the final time:"))

number=nloop=NO

N,Time=[NO], [0]

for time in range(1,tfinal+1):

for nuclei in range(1,number+1):

decay=np.random.random()
if(decay<lam):

nloop=nloop-1

number=nloop

N.append(nloop)

Time.append(time)

T=np.array(Time)

NN=NO*np.exp(-lam*T)

plt.plot(Time, N, Time, NN)

plt.plot(Time, NO-NN)

plt.xlabel("time,t")

plt.ylabel("Number of nuclei left")

plt.savefig('nuclear.png')

plt.show()

'''

OUTPUT

Enter the value of decay constatnt:0.6

Enter the initial number of nuclie:10000

Enter the final time:10

'''
##regn. no. : 144-1111-0302-21

##roll no. : 213144-21-0035

#Uniform random number between a and b.

import numpy as np

import matplotlib.pyplot as plt

N=10000

a=1

b=100

x=np.random.uniform (a,b,N)

plt.savefig('random2.png')

plt.hist(x)

plt.show()
##regn. no. : 144-1111-0302-21

##roll no. : 213144-21-0035

#random number generation

import numpy as np

import matplotlib.pyplot as plt

N=10000

a=1

b=100

x=np.random.randint(a,b,N)

plt.savefig('hist1.png')

plt.hist(x)

plt.show()

##regn. no. : 144-1111-0302-21

##roll no. : 213144-21-0035


## random number under exponential distribution

import numpy as np

import matplotlib.pyplot as plt

m=int(input('Enter mean:'))

exp=np.random.exponential (m,size=10000)

f=lambda m: np.mean (np.random.exponential (m, size=100))

x=[f(m) for _ in range(10000)]

print('mean for exponential distribution:',np.mean(exp))

print('standard deviation for exponential distribution:',np.std(exp))

print('mean for normal distribution:',np.mean(x))

print('standard deviation for exponential distribution: ', np.std(x))

plt.subplot(121)

plt.title('Exponential distribution')

plt.hist (exp, bins=40, edgecolor='red')

plt.subplot(122)

plt.title('Normal distribution')

plt.hist(x,bins=40, edgecolor='red')

plt.savefig('rnd.png')

plt.show()

'''

OUTPUT

Enter mean:5

mean for exponential distribution: 4.942536127906533

standard deviation for exponential distribution: 4.9473594734481825

mean for normal distribution: 5.001094837507649

standard deviation for exponential distribution: 0.49206493401024504

'''
##regn. no. : 144-1111-0302-21

##roll no. : 213144-21-0035

##exponential random number with mean m

import numpy as np

import matplotlib.pyplot as plt

N=10000
m=10#mean

x=np.random.exponential (m,N)

plt.savefig('expnumber.png')

plt.hist(x)

plt.show()

##regn. no. : 144-1111-0302-21

##roll no. : 213144-21-0035

##Simulate unbiased random walk in 1D. Plot of r.m.s. value of end to end
distance as a function of time step. Fit the data and find the exponents.

import numpy as np

import matplotlib.pyplot as plt

number=100

time=1000

x=2*np.random.randint(0,2,size=(number, time))-1
pos=np.cumsum(x, axis=1)

pos_sq=pos**2

m_pos_sq=np.mean (pos_sq, axis=0)

rms=np.sqrt(m_pos_sq)

t=np.arange(1,1001,1)

sq_t=np.sqrt(t)

t=np.log(t)

sq_t=np.log(sq_t)

rms=np.log(rms)

print("OUTPUT: ")

plt.plot(t,rms, 'o')

plt.plot(t,sq_t)

plt.savefig('rndwalk.png')

plt.show()
##regn. no. : 144-1111-0302-21

##roll no. : 213144-21-0035

##single trial experiment

import numpy as np

import matplotlib.pyplot as plt

N=200

m=100

H=np.zeros(200)

T=np.zeros(200)

for j in range(N):

a=np.random.randint(0,2,100)

h=0

t=0

for i in range(m):

if(a[i]==0):

h=h+1

else:

t=t+1

H[j]=h

T[j]=t

print('Average heads:', np.mean(H))

plt.hist(H)

plt.savefig('single.png')

plt.show()

'''

OUTPUT

Average heads: 50.09

'''
##regn. no. : 144-1111-0302-21

##roll no. : 213144-21-0035

"""

Generate uniform random number between 0 and 1

using pre defined functions numpy module.

Generate normal random number from it using

C.L.T. Find mean and standard deviation of both the

distributions. Show both the distributions in the

same file using subplot

"""

import numpy as np

import matplotlib.pyplot as plt

u=np.random.uniform(0,1,size=10000)

f=lambda : np. mean (np.random. uniform (0,1,size=100))

x=[f() for _ in range(10000)]

print('mean for uniform distribution: ', np.mean(u))

print('standard deviation for uniform distribution:',np.std(u))

print('mean for normal distribution: ', np.mean(x))

print('standard deviation for normal distribution: ', np.std(x))

plt.subplot(121)

plt.title('Uniform distribution')

plt.hist(u,bins=40, edgecolor='red')

plt.subplot(122)

plt.title('Normal distribution')

plt.hist(x,bins=40, edgecolor='red')

plt.savefig('random3.png')

plt.show()

'''

OUTPUT

mean for uniform distribution: 0.4947355003384134

standard deviation for uniform distribution: 0.2901207301434447


mean for normal distribution: 0.4996291901568922

standard deviation for normal distribution: 0.028974770320392158

'''

You might also like