{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7: X-ray Diffraction and Defects in Crystals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 1, Page number 239"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cube edge of unit cell is 4.1 angstrom\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "n=1;     #order\n",
    "theta=32*math.pi/180;     #glancing angle(radian)\n",
    "lamda=1.54;               #wavelength(angstrom)\n",
    "h=2;\n",
    "k=2;\n",
    "l=0;\n",
    "\n",
    "#Calculation\n",
    "d=n*lamda/(2*math.sin(theta));       #lattice parameter(angstrom)\n",
    "a=d*math.sqrt(h**2+k**2+l**2);       #cube edge of unit cell(angstrom)\n",
    "\n",
    "#Result\n",
    "print \"cube edge of unit cell is\",round(a,1),\"angstrom\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 2, Page number 240"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "interplanar spacing at 1st angle is 2.582 angstrom\n",
      "interplanar spacing at 2nd angle is 1.824 angstrom\n",
      "interplanar spacing at 3rd angle is 1.289 angstrom\n",
      "answers given in the book are wrong\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "lamda=0.58;        #wavelength(angstrom)\n",
    "theta1=6.45*math.pi/180;     #glancing angle(radian)\n",
    "theta2=9.15*math.pi/180;     #glancing angle(radian)\n",
    "theta3=13*math.pi/180;     #glancing angle(radian)\n",
    "n=1;                #order\n",
    "\n",
    "#Calculation\n",
    "d1=lamda/(2*math.sin(theta1));     #interplanar spacing at 1st angle(angstrom)\n",
    "d2=lamda/(2*math.sin(theta2));     #interplanar spacing at 2nd angle(angstrom)\n",
    "d3=lamda/(2*math.sin(theta3));     #interplanar spacing at 3rd angle(angstrom)\n",
    "\n",
    "#Result\n",
    "print \"interplanar spacing at 1st angle is\",round(d1,3),\"angstrom\"\n",
    "print \"interplanar spacing at 2nd angle is\",round(d2,3),\"angstrom\"\n",
    "print \"interplanar spacing at 3rd angle is\",round(d3,3),\"angstrom\"\n",
    "print \"answers given in the book are wrong\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 3, Page number 240"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "order of diffraction is 1\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "d=1.181;         #lattice spacing(angstrom)\n",
    "theta=90*math.pi/180;     #glancing angle(radian)\n",
    "lamda=1.540;     #wavelength of X-rays(angstrom)\n",
    "\n",
    "#Calculation\n",
    "n=2*d*math.sin(theta)/lamda;    #order of diffraction                    \n",
    "\n",
    "#Result\n",
    "print \"order of diffraction is\",int(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 4, Page number 240"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lattice parameter is 3.514 angstrom\n",
      "answer given in the book varies due to rounding off errors\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "theta=9.5*math.pi/180;     #glancing angle(radian)\n",
    "lamda=0.58;     #wavelength of X-rays(angstrom)\n",
    "n=1;            #order\n",
    "h=2;\n",
    "k=0;\n",
    "l=0;\n",
    "\n",
    "#Calculation\n",
    "d=n*lamda/(2*math.sin(theta));                      \n",
    "a=d*math.sqrt(h**2+k**2+l**2);    #lattice parameter(angstrom)\n",
    "\n",
    "#Result\n",
    "print \"lattice parameter is\",round(a,3),\"angstrom\"\n",
    "print \"answer given in the book varies due to rounding off errors\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 5, Page number 241"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "glancing angle is 26 degrees 35 minutes\n",
      "answer for glancing angle in minutes given in the book is wrong\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "theta1=(8+(35/60))*math.pi/180;     #glancing angle(radian)\n",
    "lamda=0.842;        #wavelength of X-rays(angstrom)\n",
    "n1=1;               #order\n",
    "n2=3;               #order \n",
    "\n",
    "#Calculation\n",
    "x=n2*lamda*math.sin(theta1)/(n1*lamda);                     \n",
    "theta2=math.asin(x)*180/math.pi;          #glancing angle\n",
    "theta2d=int(theta2);                      #glancing angle(degrees)\n",
    "theta2m=(theta2-theta2d)*60;              #glancing angle(minutes)\n",
    "\n",
    "#Result\n",
    "print \"glancing angle is\",theta2d,\"degrees\",int(theta2m),\"minutes\"\n",
    "print \"answer for glancing angle in minutes given in the book is wrong\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 6, Page number 241"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "interplanar spacing is 2.22 angstrom\n",
      "answer for interplanar spacing given in the book is wrong\n",
      "value of h**2+k**2+l**2 is 2 . hence the miller indices could be (110) (011) or (101)\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "a=3.16;         #lattice parameter(angstrom)\n",
    "theta=20.3*math.pi/180;     #glancing angle(radian)\n",
    "lamda=1.54;        #wavelength of X-rays(angstrom)\n",
    "n=1;               #order\n",
    "\n",
    "#Calculation\n",
    "d=n*lamda/(2*math.sin(theta));            #interplanar spacing(angstrom)\n",
    "x=(a/d)**2;\n",
    "\n",
    "#Result\n",
    "print \"interplanar spacing is\",round(d,2),\"angstrom\"\n",
    "print \"answer for interplanar spacing given in the book is wrong\"\n",
    "print \"value of h**2+k**2+l**2 is\",int(x),\". hence the miller indices could be (110) (011) or (101)\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 7, Page number 242"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wavelength is 0.084 nm\n",
      "maximum order of diffraction is 6\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "d=0.282;           #lattice spacing(nm)\n",
    "theta=(8+(35/60))*math.pi/180;     #glancing angle(radian)\n",
    "n=1;               #order\n",
    "\n",
    "#Calculation\n",
    "lamda=2*d*math.sin(theta)/n;         #wavelength(nm)\n",
    "N=2*d/lamda;                         #maximum order of diffraction\n",
    "\n",
    "#Result\n",
    "print \"wavelength is\",round(lamda,3),\"nm\"\n",
    "print \"maximum order of diffraction is\",int(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 8, Page number 243"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "maximum order of diffraction is 2\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "lamda=1.5;         #wavelength(AU)\n",
    "d=1.6;             #lattice spacing(AU)\n",
    "\n",
    "#Calculation\n",
    "n=2*d/lamda;                         #maximum order of diffraction\n",
    "\n",
    "#Result\n",
    "print \"maximum order of diffraction is\",int(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 9, Page number 243"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "interatomic spacing is 2.67 angstrom\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "theta=30*math.pi/180;     #glancing angle(radian)\n",
    "h=1;\n",
    "k=1;\n",
    "l=1;\n",
    "lamda=1.5418;             #wavelength(angstrom)\n",
    "n=1;                      #order\n",
    "\n",
    "#Calculation\n",
    "d=n*lamda/(2*math.sin(theta));      #interplanar spacing(angstrom)\n",
    "a=d*math.sqrt((h**2)+(k**2)+(l**2));     #interatomic spacing(angstrom)\n",
    "\n",
    "#Result\n",
    "print \"interatomic spacing is\",round(a,2),\"angstrom\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 10, Page number 243"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "glancing angle is 20.7 degrees\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "h=1;\n",
    "k=1;\n",
    "l=0;\n",
    "lamda=0.065;             #wavelength(nm)\n",
    "n=2;                     #order\n",
    "a=0.26;                  #axial length(nm)\n",
    "\n",
    "#Calculation\n",
    "x=n*lamda*math.sqrt(h**2+k**2+l**2)/(2*a);\n",
    "theta=math.asin(x)*180/math.pi;      #glancing angle(degrees)\n",
    "\n",
    "#Result\n",
    "print \"glancing angle is\",round(theta,1),\"degrees\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 11, Page number 244"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cube edge of unit cell is 4.055 angstrom\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "h=1;\n",
    "k=1;\n",
    "l=1;\n",
    "lamda=1.54;         #wavelength(angstrom)\n",
    "n=1;                        #order\n",
    "theta=19.2*math.pi/180;     #glancing angle(radian)\n",
    "\n",
    "#Calculation\n",
    "d=n*lamda/(2*math.sin(theta));     \n",
    "a=d*math.sqrt(h**2+k**2+l**2);     #cube edge of unit cell(angstrom)\n",
    "\n",
    "#Result\n",
    "print \"cube edge of unit cell is\",round(a,3),\"angstrom\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 12, Page number 244"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cube edge of unit cell is 4.055 *10**-10 m\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "lamda=1.54*10**-10;         #wavelength(m)\n",
    "n=1;                        #order\n",
    "theta=19.2*math.pi/180;     #glancing angle(radian)\n",
    "h=1;\n",
    "k=1;\n",
    "l=1;\n",
    "\n",
    "#Calculation\n",
    "d=n*lamda/(2*math.sin(theta));     \n",
    "a=d*math.sqrt(h**2+k**2+l**2);     #cube edge of unit cell(m)\n",
    "\n",
    "#Result\n",
    "print \"cube edge of unit cell is\",round(a*10**10,3),\"*10**-10 m\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 13, Page number 244"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "interplanar spacing is 0.26 nm\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "lamda=0.12;         #wavelength(nm)\n",
    "n=2;                        #order\n",
    "theta=28*math.pi/180;       #glancing angle(radian)\n",
    "\n",
    "#Calculation\n",
    "d=n*lamda/(2*math.sin(theta));     #interplanar spacing(nm)\n",
    "\n",
    "#Result\n",
    "print \"interplanar spacing is\",round(d,2),\"nm\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 14, Page number 245"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wavelength is 131.3 pm\n",
      "interplanar spacing is 168 pm\n",
      "answer for wavelength given in the book varies due to rounding off errors\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "lamda=97;         #wavelength(pm)\n",
    "n1=1;             #order\n",
    "n2=3;             #order      \n",
    "theta1=23*math.pi/180;       #glancing angle(radian)\n",
    "theta2=60*math.pi/180;       #glancing angle(radian)\n",
    "\n",
    "#Calculation\n",
    "lamda1=n2*lamda*math.sin(theta1)/(n1*math.sin(theta2));     #wavelength(pm)\n",
    "d=n2*lamda/(2*math.sin(theta2))            #interplanar spacing(pm)\n",
    "\n",
    "#Result\n",
    "print \"wavelength is\",round(lamda1,1),\"pm\"\n",
    "print \"interplanar spacing is\",int(d),\"pm\"\n",
    "print \"answer for wavelength given in the book varies due to rounding off errors\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 15, Page number 245"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wavelength for n=3 is 130 pm and for n=4 is 97.23 pm\n",
      "answer for wavelength for n=4 given in the book varies due to rounding off errors\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "theta=45*math.pi/180;       #glancing angle(radian)\n",
    "d=275;            #interplanar spacing(pm)\n",
    "n1=3;\n",
    "n2=4;\n",
    "\n",
    "#Calculation\n",
    "lamda=2*d*math.sin(theta);     #wavelength(pm)\n",
    "lamda1=lamda/n1;               #wavelength for n=3\n",
    "lamda2=lamda/n2;               #wavelength for n=4\n",
    "\n",
    "#Result\n",
    "print \"wavelength for n=3 is\",int(round(lamda1)),\"pm and for n=4 is\",round(lamda2,2),\"pm\"\n",
    "print \"answer for wavelength for n=4 given in the book varies due to rounding off errors\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 17, Page number 246"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lattice parameter in case of bcc are 0.242 nm and 0.296 nm which are not the same. hence the metal is not bcc\n",
      "lattice parameter in case of fcc are 0.296 nm and 0.296 nm which are the same. hence the metal is fcc\n",
      "atomic diameter is 0.20943 nm\n",
      "answer for atomic diameter given in the book varies due to rounding off errors\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "theta1=(30+(0/60))*math.pi/180;       #glancing angle(radian)\n",
    "theta2=(35+(17/60))*math.pi/180;      #glancing angle(radian)\n",
    "lamda=0.171;                          #wavelength(nm)\n",
    "h1=1;\n",
    "k1=1;\n",
    "l1=0;\n",
    "h2=2;\n",
    "k2=0;\n",
    "l2=0;\n",
    "h3=1;\n",
    "k3=1;\n",
    "l3=1;\n",
    "\n",
    "#Calculation\n",
    "d100=lamda/(2*math.sin(theta1));     #wavelength(nm)\n",
    "d200=lamda/(2*math.sin(theta2));     #wavelength(nm)\n",
    "a1=d100*math.sqrt(h1**2+k1**2+l1**2);\n",
    "a2=d200*math.sqrt(h2**2+k2**2+l2**2);    #lattice parameter in case of bcc\n",
    "a3=d100*math.sqrt(h3**2+k3**2+l3**2);\n",
    "a4=d200*math.sqrt(h2**2+k2**2+l2**2);    #lattice parameter in case of bcc\n",
    "d=a3/math.sqrt(2);                       #atomic diameter(nm) \n",
    "\n",
    "#Result\n",
    "print \"lattice parameter in case of bcc are\",round(a1,3),\"nm and\",round(a2,3),\"nm which are not the same. hence the metal is not bcc\"\n",
    "print \"lattice parameter in case of fcc are\",round(a3,3),\"nm and\",round(a4,3),\"nm which are the same. hence the metal is fcc\"\n",
    "print \"atomic diameter is\",round(d,5),\"nm\"\n",
    "print \"answer for atomic diameter given in the book varies due to rounding off errors\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 18, Page number 246"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wavelength of x-rays is 0.842 angstrom\n",
      "maximum order of diffraction is 7\n",
      "answer for wavelength of x-rays given in the book is wrong\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "d=0.282*10**-9;         #lattice spacing(m)\n",
    "theta=(8+(35/60))*math.pi/180;       #glancing angle(radian)\n",
    "maxtheta=90*math.pi/180;\n",
    "n=1;                                 #order\n",
    "\n",
    "#Calculation\n",
    "lamda=2*d*math.sin(theta)/n;         #wavelength of x-rays(m)\n",
    "N=2*d*math.sin(maxtheta)/lamda;      #maximum order of diffraction\n",
    "\n",
    "#Result\n",
    "print \"wavelength of x-rays is\",round(lamda*10**10,3),\"angstrom\"\n",
    "print \"maximum order of diffraction is\",int(round(N))\n",
    "print \"answer for wavelength of x-rays given in the book is wrong\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 19, Page number 247"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "glancing angle is 22.942 degrees\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "d=3.04*10**-10;         #lattice spacing(m)\n",
    "lamda=0.79*10**-10;     #wavelength(m) \n",
    "n=3;                    #order\n",
    "\n",
    "#Calculation\n",
    "x=n*lamda/(2*d);\n",
    "theta=math.asin(x)*180/math.pi;     #glancing angle(degrees)\n",
    "\n",
    "#Result\n",
    "print \"glancing angle is\",round(theta,3),\"degrees\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
