In [1]:

```
from __future__ import division
import math
# Python Code Ex08.1 Determination of shortest wavelength and
# frequency of X-rays from accelerating potential Page-250 (2010)
#Variable declaration
V = 50e+03; # Accelerating potential, volt
c = 3e+08; # Speed of light in free space
#Calculation
Lambda_min = 1.24e-06/V; # Minimum wavelength, metre
F_max = c/Lambda_min; # Maximum frequency, Hz
#Result
print"\nThe shortest wavelength present in X-rays ="
print round(Lambda_min/(1*10**-10),2),"angstrom"
print"\nThe maximum frequency present in X-rays ="
print round((F_max*10**-19),1)*10**19," Hz"
```

In [2]:

```
from __future__ import division
import math
# Python Code Ex8.2 Calculation of impinging electrons on the target and
# characteristics of X-rays Page-253 (2010)
#Variable declaration
I = 2.5e-03; # Current through X-ray tube, ampere
V = 6e+03; # Potential across the X-ray tube, volt
e = 1.6e-19; # Charge on an electron, coulomb
m = 9.1e-031; # mass of an electron, kg
t = 1; # Transit time, second
#Calculation
Q = I*t; # Total charge flowing per second through the x-ray tube, coulomb
n = Q/e; # Number of electrons striking the target per second
# We have eV = 1/2*m*v**2 (stopping potential = maximum Kinetic energy)
# Solving for v
v = (2*e*V/m)**0.5; # speed of electrons striking the target, m/s
Lambda_min = 1.24e-06/V; # Minimum wavelength of X-rays produced, metre
#Result
print"\nThe number of electrons striking the target ="
print round((n*10**-16),2)*10**16
print"\nThe velocity of electrons striking the target ="
print round((v*10**-7),2)*10**7,"m/s"
print"\nThe shortest wavelength present in X-rays ="
print round((Lambda_min*10**10),2)*10**-10,"m"
```

In [3]:

```
from __future__ import division
import math
#Python Ex8.3 Calculation of wavelength of characteristic X-rays Page-253(2010)
#Variable declaration
h = 6.626e-034; # Planck's constant, Js
c = 3e+08; # Speed of light in free space, m/s
e = 1.602e-019; # Charge on an electron, coulomb
E_K = -78; # Energy of K shell for platinum, keV
E_L = -12; # Energy of L shell for platinum, keV
E_M = -3 ; # Energy of M shell for platinum, keV
#Calculation
E_K_alpha = E_L - E_K; # Energy of K_alpha line, keV
E_K_beta = E_M - E_K; # Energy of K_beta line, keV
# We have E = h*f, where f = c/Lambda this implies E = h*c/lambda
# Solving for Lambda
# Lambda = h*c/E
lambda_K_alpha = h*c/(E_K_alpha*e*1e+03);# Wavelength of K_alpha line, metre
lambda_K_beta = h*c/(E_K_beta*e*1e+03); # Wavelength of K_beta line, metre
#Result
print"\nThe wavelength of K_alpha line ="
print round(lambda_K_alpha/(1*10**-10),2),"angstrom"
print"\nThe wavelength of K_beta line ="
print round(lambda_K_beta/(1*10**-10),2),"angstrom"
```

In [4]:

```
from __future__ import division
import math
# PythonCode Ex8.4 Calculation of atomic number of
#an unknown element Page-255 (2010)
#Variable declaration
lambda_Pt = 1.321e-010; # Wavelength of L_alpha line of Pt, m
Z_Pt = 78; # Atomic number of platinum
b = 7.4; # Constant
lambda_x = 4.174e-010; # Wavelength of unknown element, m
#Calculation
# We have f = [a*(Z-b)]**2 (Moseley's law)
# As f_Pt = c/lambda_Pt = [a*(Z_Pt-b)]**2
# Similarly f_x = c/lambda_x = [a*(Z_x-b)]**2
# Dividing f_Pt by f_x and solving for x
Z_x = b + (lambda_Pt/lambda_x)**0.5*(Z_Pt-b);# Atomic number of unknown element
#Result
print"\nThe atomic number of unknown element = ", round(Z_x,2)
```

In [5]:

```
from __future__ import division
import math
# Python Code Ex8.5 Calculation of wavelength of copper
#using Moseley's law Page-256 (2010)
#Variable declaration
c = 3.0e+08; # Speed of light, m/s
lambda_W = 210e-010; # Wavelength of K_alpha line of W, m
Z_W = 74; # Atomic number of tungsten
Z_Cu = 29; # Atomic number of copper
b = 1; # Constant for K-series
#Calculation
# f_W = c/lambda_W = (a*73)**2, The frequency K_alpha line for tungsten, Hz
# f_Cu = c/lambda_Cu = (a*28)**2, The frequency K_alpha line for copper, Hz
# Dividing f_W by f_Cu and solving for lambda_Cu
lambda_Cu = ((Z_W-b)/(Z_Cu-b))**2*lambda_W;# Wavelength of K_alpha line of Cu,m
#Result
print"\nThe wavelength of K_alpha line of copper ="
print round(lambda_Cu/(1*10**-10),2)," angstrom", ;
```

In [6]:

```
from __future__ import division
import math
# Python Code Ex8.6 Calculation of atomic number from wavelength using
# Moseley's law Page-256 (2010)
#Variable declaration
c = 3.0e+08; # Speed of light, m/s
h = 6.626e-034; # Planck's constant, Js
# Absolute electrical permittivity of free space,
#coulomb square per newton per metre square
epsilon_0 = 8.85e-012;
m = 9.1e-031; # Mass of an electron, kg
e = 1.6e-019; # Charge on an electron, C
lamb = 0.7185e-010; # Wavelength of K_alpha line of unknown element
b = 1; # Mosley's constant for K-series
n_1 = 1; n_2 = 2; # Lower and upper energy levels
#Calculation
# We know that f = c/lambda =
# m*e**4*(Z-b)**2/(8*epsilon_0**2*h**3)*(1/n_2**2-1/n_1**2)
# This implies that lambda
#= (8*epsilon_0**2*c*h**3/(m*e**4*(Z-b)**2*(1/n_2**2-1/n_1**2))
# Solving for Z
# Atomic number unknown element
Z = (8*epsilon_0**2*c*h**3/(m*e**4*lamb*(1/n_1**2-1/n_2**2)))**0.5+b;
#Result
print"\nThe atomic number unknown element =", round( Z )
```

In [7]:

```
from __future__ import division
import math
# Python Code Ex8.7 Calculation of wavelengths of tin and barium
# using Moseley's law Page-257 (2010)
#Variable declaration
Z_Fe = 26; # Atomic number of iron
Z_Pt = 78; # Atomic number of platinum
Z_Sn = 50; # Atomic number of tin
Z_Ba = 56; # Atomic number of barium
b = 1; # Mosley's constant for K-series
lambda_Fe = 1.93e-010; # Wavelength of K_alpha line of Fe
lambda_Pt = 0.19e-010; # Wavelength of K_alpha line of Pt
#Calculation
# From Moseley's Law,
# f = a*(Z-1)**2. This implies lambda = C*1/(Z-1)**2
# so that lambda_Fe = C*1/(Z_Fe-1)**2 and lambda_Sn = C*1/(Z_Sn-1)**2
# Dividing lambda_Sn by lambda_Fe and solving for lambda_Sn
# Wavelength of K_alpha line for tin, m
lambda_Sn = (Z_Fe-1)**2/(Z_Sn-1)**2*lambda_Fe;
# Wavelength of K_alpha line for barium, m
lambda_Ba = (Z_Pt-1)**2/(Z_Ba-1)**2*lambda_Pt;
#Result
print"\nThe wavelengths of tin and barium ="
print round(lambda_Sn/10**-10,2),"angstrom and "
print round(lambda_Ba/10**-10,2)," angstrom respectively"
```

In [8]:

```
from __future__ import division
import math
# Python Code Ex8.8 Percentage transmitted energy of X-rays: Page 259 (2010)
#Variable declaration
mu = 139; # Attenuation co-efficient of aluminium, per metre
x = 0.005; # Thickness of aluminium sheet, m
#Calculations
# If X% is the intensity of the X-ray transmitted through the aluminium sheet,
# X% = I/I_0
# or X/100 = math.exp(-absorb_coeff*x)
# Solving for X
X = 100*math.exp(-mu*x); # Transmitted percentage of X-rays
#Result
print"\nThe intensity of the X-ray transmitted through the aluminium sheet ="
print round(X,2),"percent"
```

In [9]:

```
from __future__ import division
import math
# Python code Ex8.9 : Determination of thickness of lead piece
#by using two equal intensity X-ray wavelengths : Page 259 (2010)
#Variable declaration
lambda_1 = 0.064e-010; # First wavelength of X-ray, metre
lambda_2 = 0.098e-010; # Second wavelength of X-ray, metre
I1_ratio_I2 = 3; # Ratio of attenuated beam intensity
# Mass absorption coefficient for first wavelength, metre square per kg
mu_m1 = 0.164;
# Mass absorption coefficient for second wavelength, metre square per kg
mu_m2 = 0.35;
d = 0.164; # Density of the lead, kg per metre cube
#Calculation
# absorption co-efficient of the lead for first wavelength, per metre
mu1 = mu_m1*d;
# absorption co-efficient of the lead for second wavelength, per metre
mu2 = mu_m2*d;
# Declare 'x' as the thickness variable
# Now I = math.exp(-ac*x) thus
# I1_ratio_I2 = math.exp(-ac_1*x)/math.exp(-ac_2*x)
# or 3 = math.exp(2109.24)*x this implies
# 2104.24*x = math.log(3) and assume
x=math.log(3)/2104.24;
#Result
print"\nThe thickness of lead piece =",round((x*10**4),2)*10**-4,"m"
```

In [10]:

```
from __future__ import division
import math
# Python code Ex8.10: Determining angle of reflection
#by using wavelength of X-ray Page 261 (2010)
#Variable declaration
lamb = 0.440e-010; # Wavelength of X-rays, m
d = 2.814e-010; # Interplanar spacing of rocksalt crystal, m
#Calculation and Result
# 2*d*sin(theta)= n*lambda **Bragg's law, n is the order of diffraction
# Solving for theta, we have
# For converting angle into degrees and minutes
de=[]
m=[]
de.append(0)
m.append(0)
for n in range(1,6): # For diffraction order from 1 to 5
# Bragg's angle
theta = math.asin(n*lamb/(2*d))
de.append( int(math.degrees(theta)));
m.append((math.degrees(theta)-int(math.degrees(theta)))*60)
print"\nTheta",n," = ",de[n]," degree(s),",int(m[n]),"minute(s)"
```

In [11]:

```
from __future__ import division
import math
# Python code Ex8.11: Determining the wavelength of
# diffracted X-rays Page 262 (2010)
#Variable declaration
d = 2.814e-010; # Interplanar spacing of rocksalt crystal, m
theta = 9; # Bragg's angle, degree
#Calculations and Results
# 2*d*sin(theta) = n*lambda **Bragg's law, n is the order of diffraction
# Solving for lambda, we have
print"\nThe first four wavelengths of diffracted beam are:"
for n in range(1,5): # For diffraction order from 1 to 5
# Wavelength of X-rays, m
lamb = 2*d*math.sin(math.radians(theta))/n
if (lamb >= 0.2e-010 or lamb <= 1.0e-010):
print"\nLambda",n," = ",round(lamb/(1*10**-10),2)," angstrom"
```

In [12]:

```
from __future__ import division
import math
# Python code Ex8.12: Reciprocal lattice parameters from
# 2-D direct lattice parameters Page 277 (2010)
#Variable declaration
a = 3e-010; # First lattice parameter of direct lattice
b = 5e-010; # Second lattice parameter of direct lattice
theta = 60; # Angle between two lattice vectors of the direct lattice
#Calculation
# if a_prime and b_prime are the lattice vectors for the reciprocal lattice,
# a_prime*a = 2*%math.pi and a_prime*b = 0
# Similarly, b_prime*b = 2*%math.pi and b_prime*a = 0
# Solving for a_prime and b_prime, we have
# Lattice vector for reciprocal lattice, per metre
a_prime = 2*math.pi/(a*math.cos(math.radians(90-theta)));
# Lattice vector for reciprocal lattice, per metre
b_prime = 2*math.pi/(b*math.cos(math.radians(90-theta)));
#Result
print"\nThe reciprocal lattice vectors are:\n a_prime ="
print round(a_prime*1e-010,2),"per angstrom and b_prime ="
print round(b_prime*1e-010,2)," per angstrom"
```

In [13]:

```
from __future__ import division
import math
# Python code Ex8.13: Bragg angle and the indices of
# diffraction of Powder Lines Page 285 (2010)
#Variable declaration
n = 1; # Cosider first order diffraction
a = 6e-010; # First lattice parameter of direct lattice, m
# Wavelength used in diffraction of X-rays by Powder Method, m
lamb = 1.54e-010;
# Declare a function for converting angle into degrees and minutes
#Calculations and Results
# Calculate the hkl and hence interpalnar spacing
# 'd' for three lowest powder lines
print"\nThe Bragg angles and the indices of diffraction "
print" for the three lowest powder lines are:"
for h in range (0,3):
p=(h%2)
for k in range (0,3):
q=(k%2)
for l in range (0,2):
r=(l%2)
if (((p==1 and q==1 and r==1))or((p==0 and q==0 and r==0))):
if (h <> 0):
N = h**2+k**2+l**2;
d=(a/(N)**0.5);
# Interplanar spacing, metre
theta = math.asin(n*lamb/(2*d));
de=( int(math.degrees(theta)));
m=((math.degrees(theta)-int(math.degrees(theta)))*60)
# Call conversion function
print "\nd[",h, k, l,"] ="
print round((d*10**10),2)*10**-10,"and theta"
print"[", h, k, l,"] =",de," deg",round(m,2)," min"
```

In [14]:

```
from __future__ import division
import math
# Python code Ex8.14: Minimum distance from the centre of the Laue pattern
#of an fcc crystal Page 289 (2010)
#Variable declaration
n = 1; # Consider the first order diffraction
a = 4.5e-010; # Lattice parameter for fcc lattice, m
V = 50e+03; # Potential difference across the X-ray tube, volt
D = 5; # Crystal to film distance, cm
h = 1 ; # Incides for the planes of maximum spacing
k = 1
l = 1
#Calculation
lambda_min = 1.24e-06/V; # The cut-off wavelength of X-rays, m
d_111 = a/(h**1+k**2+l**2)**0.5;
theta_111 = math.asin(n*lambda_min/(2*d_111));
# As tan(2*theta_111) = x/D, solving for x
x = D*math.tan((2*theta_111));#Minimum distance from the centre of Laue pattern
#Result
print"\nThe minimum distance from the centre of the Laue pattern "
print"at which reflections can occur from the planes of maximum spacing ="
print round(x,2),"cm"
```

In [15]:

```
from __future__ import division
import math
# Python code Ex8.15: Calculating unit cell height along the axis
#of a rotation photograph Page 291 (2010)
#Variable declaration
n = 1; # Consider the first order diffraction of X-rays
# An array of heights of first six layers above(below) the zero layer, cm
S = [0.29,0.59,0.91,1.25,1.65,2.12];
R = 3; # Radius of the camera, cm
lamb = 1.54e-08; # Wavelength of the X-rays, cm
#Calculation
# For an a-axis rotation photograph, the unit cell parameter is given by
# a = n*lambda/S(n)*(R**2 + S(n)**2)**(1/2)
# Calculate 'a' for six different values of n from 1 to 6
a=[]
for f in range(0,6):
a.append((n*lamb/S[f])*(R**2 + S[f]**2)**(1/2));
#Result
print"\nThe unit cell height of the crystal ="
print round(a[0]/(1*10**(-8)),2),"angstrom",
```

In [16]:

```
from __future__ import division
import math
# Python code Ex8.16: Diffraction of thermal neutrons from planes
# of Ni crystal Page 294 (2010)
#Variable declaration
k = 1.38e-023; # Boltzmann constant, J/mol/K
h = 6.626e-034; # Planck's constant, Js
theta = 28.5; # Bragg's angle, degree
a = 3.52e-010; # Lattice parameter of fcc structure of nickel, m
m_n = 1.67e-027; # Rest mass of neutron, kg
#Calcultion
# For fcc lattice, the interplanar spacing is given by
d = a/(3)**0.5; # Interplanar spacing of Ni, m
# Bragg's equation for first order diffraction (n = 1) is
lamb = 2*d*math.sin(math.radians(theta)); # Bragg's law, m
# From kinetic interpretaion of temperature, we have
# (1/2)*m*v**2 = (3/2)*k*T -- (a)
# Further from de-Broglie relation
# lambda = h/(m*v) -- (b)
# From (a) and (b), solving for T, we have
T = h**2/(3*m_n*k*lamb**2); # Effective temperature of the neutrons, K
#Result
print"\nThe effective temperature of neutrons =",round(T,2),"K"
```

In [17]:

```
from __future__ import division
import math
#Python code Ex8.17:Diffraction of electrons from fcc crystal planes Page 295
#Variable declaration
# Declare a function for converting angle into degrees and minutes
h = 6.626e-034; # Planck's constant, Js
m = 9.1e-031; # Rest mass of electron, kg
e = 1.602e-019; # charge on an electron, coulomb
a = 3.5e-010; # Lattice parameter of fcc crystal, m
V = 80; # Accelerating potential for electrons, volt
#Calculations
lamb = h/(2*m*e*V)**0.5; # de-Broglie wavelength of electrons, m
d_111 = a/(3)**0.5; # Interplanar spacing for (111) planes of fcc crystal, m
# Bragg's equation for first order diffraction (n = 1) is
# lambda = 2*d_111*math.sin(math.degrees(theta_111)); // Bragg's law, m
theta_111 = math.asin(lamb/(2*d_111)); # Bragg's angle, degree
de=( int(math.degrees(theta_111)));
# Call conversion function
m=((math.degrees(theta_111)-int(math.degrees(theta_111)))*60)
#Result
print"\nThe Bragg angle for electron diffraction =",de,"deg",round(m,2),"min"
```