Statmech - Lab 1 (Exercise 1) : Name: Pratul Manna

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

StatMech_Lab 1 (Exercise 1)

Name : Pratul Manna


March 9, 2021

Roll No : 107
Semeseter : 6

Class : B.Sc. Physics 3rd year

Question 1 :

Python Code :
1. from pylab import *
2. rows=5; cols=3 #taking rows and columns of matrix
3. p=1 #inital element value
4. X=[]
5. for i in range(cols):
6. m=[]
7. for j in range(rows):
8. m.append(p) #creating sublists
9. p+=1 #increasing element value

1
10. X.append(m) #appending to main list
11. k=vstack((X,)).T
12. print "The array is\n",k
13. a=k[1,:];b=k[3,:] #taking 2nd and 4th row
14. print "New array is\n ", vstack((a,b))

Ipython window :

Question 2 :

Python Code :
1. from numpy import *
2. numbers=random.normal(size=5)
3. print numbers

Ipython window :

2
Question 3 :

Python Code :
from numpy import *
from random import randint
X=array([]) #defining null array
for i in range(10):

p=randint(1,10)
q=randint(1,10)
m=complex(p,q) #taking the complex no. p+iq
X=append(X,m)

print "The array taken :\n", X


X=sort_complex(X) #sorting the complex number
print "Sorting the complex array\n", X

Ipython window :

3
Question 4 :

Python Code :

from pylab import * #PARTICLE IN 1D BOX


L=1 #Length of the box
x=linspace(0,L,500) #position
n=range(1,4) #quantum number
for i in n:

subplot(2,2,i)
Y=sqrt(2/L)*sin(i*pi*x/L) #wave function
xlabel(’X--->’)
ylabel(’Wavefunction(Y)--->’)
title(’Wave func. for n=’+str(i))
plot(x,Y,’b-’)
suptitle(’Wavefunction for different eigenstates in 1D potential
well’)
show()

4
Output :

Question 5 :

Python Code :

from numpy import *


from scipy import *
from time import *
A=matrix(random.rand(100,100))

5
B=matrix(random.rand(100,1))
t1=time()
x1=linalg.solve(A,B)
t2=time()
ta=t2-t1
print ("solving using linalg sub-package\n\n")
print x1,("\n\n")
t3=time()
A_inv= linalg.inv(A) #calculating inverse of A and using # x=A_inv.B
x2=dot(A_inv,B)
t4=time()
tb=t4-t3
print("solving using inverse calculation\n\n")
print x2
if(ta>tb):

print ’solve takes more time’

if(ta<tb):

print ’inv takes more time’

Ipython window :

6
Question 6 :

Python Code :

from numpy import *


from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
def sine(x,a,b):

return a*sin(b*x)

x=linspace(-5,+5,50)
random.seed(0)
y=2.9*sin(1.5*x)+random.normal(size=len(x))
plt.scatter(x,y,s=20,color=’#02A4F4’,label=’datapoints’)
plt.legend(loc=’best’)
pars,cov=curve_fit(f=sine,xdata=x, ydata=y,p0=[x[0],y[0]])
print’best fit parameters : ’,(pars)
plt.xlabel("X--->")
plt.ylabel("Y--->")

7
plt.plot(x,sine(x,*pars),linewidth=2,color=’black’,label="fitted curve")
plt.legend(loc=’best’)
plt.show()

Ipython window :

8
————————————————————————————————-

You might also like