{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6: Crystallography"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 1, Page number 207"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "density is 5.38 *10**4 kg/m**3\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "n=8;             #number of atoms per unit cell\n",
    "a=5.6*10**-10;   #lattice constant(m)\n",
    "M=710.59;        #atomic weight(amu)\n",
    "N=6.02*10**26;   #avagadro number(kg/mol)\n",
    "\n",
    "#Calculation\n",
    "rho=n*M/(a**3*N);     #density(kg/m**3)    \n",
    "\n",
    "#Result\n",
    "print \"density is\",round(rho/10**4,2),\"*10**4 kg/m**3\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 2, Page number 207"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lattice constant is 0.29 angstrom\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "n=2;             #number of atoms per unit cell\n",
    "M=55.85;         #atomic weight(amu)\n",
    "N=6.02*10**23;   #avagadro number(kg/m**3)\n",
    "rho=7860;        #density(kg/m**3)    \n",
    "\n",
    "#Calculation \n",
    "a=(n*M/(rho*N))**(1/3);     #lattice constant(m)    \n",
    "\n",
    "#Result\n",
    "print \"lattice constant is\",round(a*10**8,2),\"angstrom\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 3, Page number 208"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lattice constant is 3.52 angstrom\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "n=2;             #number of atoms per unit cell\n",
    "M=6.94;         #atomic weight(amu)\n",
    "N=6.02*10**26;   #avagadro number(kg/mol)\n",
    "rho=530;        #density(kg/m**3)    \n",
    "\n",
    "#Calculation \n",
    "a=(n*M/(rho*N))**(1/3);     #lattice constant(m)    \n",
    "\n",
    "#Result\n",
    "print \"lattice constant is\",round(a*10**10,2),\"angstrom\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 4, Page number 208"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number of atoms per unit cell is 2\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "M=55.85;         #atomic weight(amu)\n",
    "N=6.02*10**26;   #avagadro number(kg/mol)\n",
    "rho=7870;        #density(kg/m**3)    \n",
    "a=2.9*10**-10;   #lattice constant(m)    \n",
    "\n",
    "#Calculation \n",
    "n=a**3*rho*N/M;     #number of atoms per unit cell\n",
    "\n",
    "#Result\n",
    "print \"number of atoms per unit cell is\",int(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 5, Page number 208"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "density is 53771 kg/m**3\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "n=8;             #number of atoms per unit cell\n",
    "a=5.6*10**-10;   #lattice constant(m)\n",
    "M=710.59;        #atomic weight(amu)\n",
    "N=6.02*10**26;   #avagadro number(kg/mol)\n",
    "\n",
    "#Calculation\n",
    "rho=n*M/(a**3*N);     #density(kg/m**3)    \n",
    "\n",
    "#Result\n",
    "print \"density is\",int(rho),\"kg/m**3\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 6, Page number 209"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lattice constant is 0.2869 angstrom\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "n=2;             #number of atoms per unit cell\n",
    "M=55.85;         #atomic weight(amu)\n",
    "N=6.02*10**23;   #avagadro number(kg/m**3)\n",
    "rho=7860;        #density(kg/m**3)    \n",
    "\n",
    "#Calculation \n",
    "a=(n*M/(rho*N))**(1/3);     #lattice constant(m)    \n",
    "\n",
    "#Result\n",
    "print \"lattice constant is\",round(a*10**8,4),\"angstrom\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 7, Page number 209"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lattice constant is 3.517 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",
    "n=2;             #number of atoms per unit cell\n",
    "M=6.94;         #atomic weight(amu)\n",
    "N=6.02*10**26;   #avagadro number(kg/mol)\n",
    "rho=530;        #density(kg/m**3)    \n",
    "\n",
    "#Calculation \n",
    "a=(n*M/(rho*N))**(1/3);     #lattice constant(m)    \n",
    "\n",
    "#Result\n",
    "print \"lattice constant is\",round(a*10**10,3),\"angstrom\"\n",
    "print \"answer given in the book varies due to rounding off errors\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 8, Page number 209"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number of atoms per unit cell is 2\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "M=55.85;         #atomic weight(amu)\n",
    "N=6.02*10**26;   #avagadro number(kg/mol)\n",
    "rho=7870;        #density(kg/m**3)    \n",
    "a=2.9*10**-10;   #lattice constant(m)    \n",
    "\n",
    "#Calculation \n",
    "n=a**3*rho*N/M;     #number of atoms per unit cell\n",
    "\n",
    "#Result\n",
    "print \"number of atoms per unit cell is\",int(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 9, Page number 210"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "density is 8933.25 kg/m**3\n",
      "answer given in the book is wrong\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "r=0.1278*10**-9;   #atomic radius(m)\n",
    "n=4;               #number of atoms per unit cell\n",
    "M=63.5;            #atomic weight(amu)\n",
    "N=6.02*10**26;     #avagadro number(kg/mol)\n",
    "\n",
    "#Calculation\n",
    "a=math.sqrt(8)*r;   #lattice constant(m)\n",
    "rho=n*M/(a**3*N);   #density(kg/m**3)    \n",
    "\n",
    "#Result\n",
    "print \"density is\",round(rho,2),\"kg/m**3\"\n",
    "print \"answer given in the book is wrong\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 10, Page number 210"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "percent volume change is 0.5 %\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "r1=1.258*10**-10;     #radius(m)\n",
    "r2=1.292*10**-10;     #radius(m)\n",
    "n1=2;                 #number of atoms per unit cell\n",
    "n2=4;                 #number of atoms per unit cell\n",
    "\n",
    "#Calculation\n",
    "a_bcc=4*r1/math.sqrt(3);\n",
    "v=a_bcc**3;\n",
    "V1=v/n1;\n",
    "a_fcc=2*math.sqrt(2)*r2;\n",
    "V2=a_fcc**3/n2;\n",
    "V=(V1-V2)*100/V2;           #percent volume change is\",V,\"%\"\n",
    "\n",
    "#Result\n",
    "print \"percent volume change is\",round(V,1),\"%\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 12, Page number 211"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number of atoms per unit volume is 4.49 *10**22 atoms/cm**3\n",
      "distance between adjacent atoms is 2.81 angstrom\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "w=23+35.5;           #molecular weight of NaCl(gm/mole)\n",
    "N=6.023*10**23;      #avagadro number(gm/mol)\n",
    "rho=2.18;            #density of NaCl(gm/cm**3)\n",
    "n=2;                 #number of atoms\n",
    "\n",
    "#Calculation\n",
    "m=w/N;               #mass of NaCl(gm)\n",
    "nm=rho/m;            #number of molecules(mole/cm**3)\n",
    "N_NaCl=n*nm;         #number of atoms per unit volume(atoms/cm**3) \n",
    "a=(1/N_NaCl)**(1/3);     #distance between adjacent atoms(cm)\n",
    "\n",
    "\n",
    "#Result\n",
    "print \"number of atoms per unit volume is\",round(N_NaCl/10**22,2),\"*10**22 atoms/cm**3\"\n",
    "print \"distance between adjacent atoms is\",round(a*10**8,2),\"angstrom\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 13, Page number 212"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "glancing angle is 21.01 degrees\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "lamda=0.071*10**-9;      #wavelength(m)\n",
    "h=1;\n",
    "k=1;\n",
    "l=0;                     #miller indices\n",
    "a=0.28*10**-9;           #lattice constant(m)\n",
    "n=2;                     #order\n",
    "\n",
    "#Calculation\n",
    "d=a/math.sqrt(h**2+k**2+l**2);     \n",
    "theta=math.asin(n*lamda/(2*d))*180/math.pi;    #glancing angle(degrees)\n",
    "\n",
    "#Result\n",
    "print \"glancing angle is\",round(theta,2),\"degrees\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 14, Page number 213"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "space of reflecting plane is 2.3336 angstrom\n",
      "volume of unit cell is 1.27 *10**-29 m**3\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "lamda=3*10**-10;      #wavelength(m)\n",
    "h=1;\n",
    "k=0;\n",
    "l=0;                     #miller indices\n",
    "theta=40*math.pi/180;    #glancing angle(radian)\n",
    "n=1;                     #order\n",
    "\n",
    "#Calculation\n",
    "d=n*lamda/(2*math.sin(theta));      #space of reflecting plane(m)\n",
    "a=d*math.sqrt(h**2+k**2+l**2);      \n",
    "V=a**3;                  #volume of unit cell(m**3)\n",
    "\n",
    "#Result\n",
    "print \"space of reflecting plane is\",round(d*10**10,4),\"angstrom\"\n",
    "print \"volume of unit cell is\",round(V*10**29,2),\"*10**-29 m**3\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 15, Page number 213"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "space of reflecting plane is 0.42 angstrom\n",
      "answer given in the book is wrong\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "lamda=0.82;      #wavelength(m)\n",
    "theta=75.86*math.pi/180;    #glancing angle(radian)\n",
    "n=1;                     #order\n",
    "a=3;                     #lattice constant(angstrom)\n",
    "\n",
    "#Calculation\n",
    "d=n*lamda/(2*math.sin(theta));      #space of reflecting plane(angstrom)\n",
    "#here the value of d comes to 0.422 angstrom which is not equal to the value of a. hence the problem cannot be solved further \n",
    "\n",
    "#Result\n",
    "print \"space of reflecting plane is\",round(d,2),\"angstrom\"\n",
    "print \"answer given in the book is wrong\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 16, Page number 214"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wavelength of X-ray beam is 3 angstrom\n",
      "energy of X-ray beam is 4.14 *10**5 eV\n",
      "answer for energy given in the book is wrong\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "a=5.63*10**-10;          #lattice constant(m)\n",
    "h=1;\n",
    "k=1;\n",
    "l=1;                     #miller indices\n",
    "theta=27.5*math.pi/180;    #glancing angle(radian)\n",
    "n=1;                     #order\n",
    "h=6.625*10**-34;         #planck's constant\n",
    "c=3*10**10;              #velocity of light(m/sec)\n",
    "e=1.6*10**-19;           #charge of electron(c)\n",
    "\n",
    "#Calculation\n",
    "d111=a/math.sqrt(h**2+k**2+l**2);      \n",
    "lamda=2*d111*math.sin(theta)/n;      #wavelength of X-ray beam(m) \n",
    "lamda=int(lamda*10**10);             #wavelength of X-ray beam(angstrom) \n",
    "E=h*c/(lamda*10**-10*e);         #energy of X-ray beam(eV)    \n",
    "\n",
    "#Result\n",
    "print \"wavelength of X-ray beam is\",lamda,\"angstrom\"\n",
    "print \"energy of X-ray beam is\",round(E/10**5,2),\"*10**5 eV\"\n",
    "print \"answer for energy given in the book is wrong\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 17, Page number 215"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lattice constant is 3.7935 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",
    "h=2;\n",
    "k=0;\n",
    "l=2;                     #miller indices\n",
    "theta=34*math.pi/180;    #glancing angle(radian)\n",
    "n=1;                     #order\n",
    "lamda=1.5*10**-10;       #wavelength of X-ray beam(m) \n",
    "\n",
    "#Calculation\n",
    "d=n*lamda/(2*math.sin(theta));      \n",
    "a=d*math.sqrt(h**2+k**2+l**2);          #lattice constant(m)\n",
    "\n",
    "#Result\n",
    "print \"lattice constant is\",round(a*10**10,4),\"angstrom\"\n",
    "print \"answer given in the book varies due to rounding off errors\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 19, Page number 216"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ratio of d100:d110:d111 is math.sqrt( 6 ) : math.sqrt( 3 ) : math.sqrt( 2 )\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "h1=1;\n",
    "k1=0;\n",
    "l1=0;                     #miller indices\n",
    "h2=1;\n",
    "k2=1;\n",
    "l2=0;                     #miller indices\n",
    "h3=1;\n",
    "k3=1;\n",
    "l3=1;                     #miller indices\n",
    "a=1;                      #assume\n",
    "\n",
    "#Calculation\n",
    "Dr1=(h1**2+k1**2+l1**2);\n",
    "Dr2=(h2**2+k2**2+l2**2);\n",
    "Dr3=(h3**2+k3**2+l3**2);\n",
    "d100=a/math.sqrt(Dr1);\n",
    "d110=a/math.sqrt(Dr2);\n",
    "d111=a/math.sqrt(Dr3);\n",
    "def lcm(x, y):  \n",
    "   if x > y:  \n",
    "       z = x  \n",
    "   else:  \n",
    "       z = y  \n",
    "  \n",
    "   while(True):  \n",
    "       if((z % x == 0) and (z % y == 0)):  \n",
    "           lcm = z  \n",
    "           break  \n",
    "       z += 1  \n",
    "  \n",
    "   return lcm\n",
    "lcm=lcm(Dr2,Dr3);\n",
    "r=math.sqrt(lcm);\n",
    "r_d100=d100*r;\n",
    "r_d110=d110*r;\n",
    "r_d111=d111*r;\n",
    "\n",
    "#Result\n",
    "print \"ratio of d100:d110:d111 is math.sqrt(\",int(round(r_d100**2)),\") : math.sqrt(\",int(round(r_d110**2)),\") : math.sqrt(\",int(round(r_d111**2)),\")\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 20, Page number 217"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "interplanar spacing is 159.1 nm\n",
      "answer given in the book is wrong\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "h=2;\n",
    "k=2;\n",
    "l=0;                     #miller indices\n",
    "a=450;                   #length(nm)  \n",
    "\n",
    "#Calculation\n",
    "d220=a/math.sqrt(h**2+k**2+l**2);          #interplanar spacing(nm)\n",
    "\n",
    "#Result\n",
    "print \"interplanar spacing is\",round(d220,1),\"nm\"\n",
    "print \"answer given in the book is wrong\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 21, Page number 217"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "interplanar spacing is 2.09 *10**-10 m\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;                     #miller indices\n",
    "r=1.278*10**-10;         #radius(m)\n",
    "\n",
    "#Calculation\n",
    "a=4*r/math.sqrt(2);       \n",
    "d111=a/math.sqrt(h**2+k**2+l**2);          #interplanar spacing(m)\n",
    "\n",
    "#Result\n",
    "print \"interplanar spacing is\",round(d111*10**10,2),\"*10**-10 m\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 22, Page number 217"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wavelength of X-rays is 1.549 angstrom\n",
      "answer in the book varies due to rounding off errors\n",
      "energy of X-rays is 8 *10**3 eV\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;                     #miller indices\n",
    "n=4;\n",
    "A=107.87;                #atomic weight(amu)\n",
    "N=10500*6.052*10**26;    #density(kg/m**3)\n",
    "theta=(19+(12/60))*math.pi/180;      #angle(radian)\n",
    "r=1.278*10**-10;         #radius(m)\n",
    "hp=6.625*10**-34;        #plancks constant(Js)\n",
    "c=3*10**8;               #velocity of light(m/sec)\n",
    "e=1.6*10**-19;           #charge of electron(coulomb)\n",
    "\n",
    "#Calculation\n",
    "a=(n*A/N)**(1/3);        #lattice constant(m)\n",
    "d=a/math.sqrt(h**2+k**2+l**2);          #interplanar spacing(m)\n",
    "lamda=2*d*math.sin(theta);       #wavelength of X-rays(m)\n",
    "E=hp*c/(e*lamda);                    #energy of X-rays(eV) \n",
    "\n",
    "#Result\n",
    "print \"wavelength of X-rays is\",round(lamda*10**10,3),\"angstrom\"\n",
    "print \"answer in the book varies due to rounding off errors\"\n",
    "print \"energy of X-rays is\",int(E/10**3),\"*10**3 eV\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 23, Page number 217"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "glancing angle is 21 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;                     #miller indices\n",
    "d100=0.28;               #lattice constant(nm)\n",
    "n=2;\n",
    "lamda=0.071;             #wavelength(nm)\n",
    "\n",
    "#Calculation\n",
    "d110=d100/math.sqrt(h**2+k**2+l**2);          #interplanar spacing(m)\n",
    "theta=math.asin(n*lamda/(2*d110))*180/math.pi;   #glancing angle(degrees)\n",
    "\n",
    "#Result\n",
    "print \"glancing angle is\",int(theta),\"degrees\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 24, Page number 218"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "distance between the planes is 0.27 nm\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;              #miller indices\n",
    "a=0.38;           #lattice constant(nm)\n",
    "\n",
    "#Calculation\n",
    "d=a/math.sqrt(h**2+k**2+l**2);    #distance between the planes(nm)\n",
    "\n",
    "#Result\n",
    "print \"distance between the planes is\",round(d,2),\"nm\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 26, Page number 219"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "intercept along y axis is 0.123 nm\n",
      "intercept along y axis is 0.394 nm\n"
     ]
    }
   ],
   "source": [
    "#importing modules\n",
    "import math\n",
    "from __future__ import division\n",
    "\n",
    "#Variable declaration\n",
    "h=2;\n",
    "k=3;\n",
    "l=1;              #miller indices\n",
    "a=0.121;          \n",
    "b=0.184;\n",
    "c=0.197;          #parameters(nm)\n",
    "\n",
    "#Calculation\n",
    "OB=2*b/3;       #intercept along y axis(nm)\n",
    "OC=2*c;         #intercept along z axis(nm) \n",
    "\n",
    "#Result\n",
    "print \"intercept along y axis is\",round(OB,3),\"nm\"\n",
    "print \"intercept along y axis is\",OC,\"nm\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 27, Page number 219"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "interplanar distance between (123) planes is 0.213 nm\n",
      "interplanar distance between (246) planes is 0.1066 nm\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",
    "h1=1;\n",
    "k1=2;\n",
    "l1=3;              #miller indices\n",
    "h2=2;\n",
    "k2=4;\n",
    "l2=6;              #miller indices\n",
    "a=0.82;          \n",
    "b=0.94;\n",
    "c=0.75;            #parameters(nm)\n",
    "\n",
    "#Calculation\n",
    "d123=(((h1/a)**2)+((k1/b)**2)+((l1/c)**2))**(-1/2);     #interplanar distance between (123) planes\n",
    "d246=d123/2;                  #interplanar distance between (246) planes\n",
    "\n",
    "#Result\n",
    "print \"interplanar distance between (123) planes is\",round(d123,3),\"nm\"\n",
    "print \"interplanar distance between (246) planes is\",round(d246,4),\"nm\"\n",
    "print \"answers given in the book are wrong\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 28, Page number 219"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wavelength of Xrays is 0.159 nm\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;              #miller indices\n",
    "a=0.2;            #lattice parameter(nm)\n",
    "theta=(87/2)*math.pi/180;       #angle(radian)\n",
    "\n",
    "#Calculation\n",
    "d=a/math.sqrt(h**2+k**2+l**2);\n",
    "lamda=2*d*math.sin(theta);      #wavelength of Xrays(nm)\n",
    "\n",
    "#Result\n",
    "print \"wavelength of Xrays is\",round(lamda,3),\"nm\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 29, Page number 219"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "effective temperature of neutrons is 169 K\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;              #miller indices\n",
    "a=0.352;            #lattice parameter(nm)\n",
    "theta=(28+(30/60))*math.pi/180;       #angle(radian)\n",
    "hp=6.626*10**-34;       #plancks constant(Js)\n",
    "m=1.67*10**-27;        #mass of proton(kg)\n",
    "kB=1.38*10**-23;       #boltzmann constant\n",
    "\n",
    "#Calculation\n",
    "d=a/math.sqrt(h**2+k**2+l**2);\n",
    "lamda=2*d*math.sin(theta);      #wavelength(nm)\n",
    "T=(hp**2)/(3*m*kB*(lamda*10**-9)**2);           #effective temperature of neutrons(K)\n",
    "\n",
    "#Result\n",
    "print \"effective temperature of neutrons is\",int(round(T)),\"K\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 30, Page number 220"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lattice parameter for regular crystal is 0.3609 nm\n",
      "lattice parameter for sample A is 0.3673 nm\n",
      "lattice parameter for sample B is 0.361 nm\n",
      "lattice parameter of sample A is 1.75% greater than that of pure copper\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;              #miller indices\n",
    "lamda=0.152;            #wavelength(nm)\n",
    "D=0.2552;               #diameter(nm)\n",
    "theta1=21*math.pi/180;       #angle(radian)\n",
    "theta2=(21+(23/60))*math.pi/180;       #angle(radian)\n",
    "\n",
    "#Calculation\n",
    "a=D*math.sqrt(2);            #lattice parameter for regular crystal(nm)\n",
    "d111_1=lamda/(2*math.sin(theta1));\n",
    "alpha1=d111_1*math.sqrt(h**2+k**2+l**2);    #lattice parameter for sample A(nm)\n",
    "d111_2=lamda/(2*math.sin(theta2));\n",
    "alpha2=d111_2*math.sqrt(h**2+k**2+l**2);    #lattice parameter for sample B(nm)\n",
    "\n",
    "#Result\n",
    "print \"lattice parameter for regular crystal is\",round(a,4),\"nm\"\n",
    "print \"lattice parameter for sample A is\",round(alpha1,4),\"nm\"\n",
    "print \"lattice parameter for sample B is\",round(alpha2,3),\"nm\"\n",
    "print \"lattice parameter of sample A is 1.75% greater than that of pure copper\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example number 31, Page number 220"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the value of h**2+k**2+l**2 is 11.69\n",
      "answer given in the book is wrong\n",
      "highest possible values of (hkl) are (222)\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;              #miller indices\n",
    "lamda=0.154;      #wavelength of X-rays(nm)\n",
    "D=0.228;          #diameter(nm)\n",
    "\n",
    "#Calculation\n",
    "x=lamda/2;\n",
    "y=2*D/(x*math.sqrt(h**2+k**2+l**2));\n",
    "z=y**2;\n",
    "\n",
    "#Result\n",
    "print \"the value of h**2+k**2+l**2 is\",round(z,2)\n",
    "print \"answer given in the book is wrong\"\n",
    "print \"highest possible values of (hkl) are (222)\""
   ]
  }
 ],
 "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
}
