{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 4 - Diffraction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 4 - pg 101"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "theta (degrees) =   0.412\n",
      "When p =  0\n",
      "theta (degrees) =  0.026\n",
      "When p =  1\n",
      "theta (degrees) =  0.078\n",
      "When p =  2\n",
      "theta (degrees) =  0.129\n",
      "When p =  3\n",
      "theta (degrees) =  0.181\n",
      "When p =  4\n",
      "theta (degrees) =  0.233\n",
      "When p =  5\n",
      "theta (degrees) =  0.285\n",
      "When p =  6\n",
      "theta (degrees) =  0.337\n",
      "When p =  7\n",
      "theta (degrees) =  0.388\n",
      "When p =  8\n",
      "theta (degrees) =  0.44\n",
      " When p >= 8 ,  theta > 0.412042642025 degrees .\n",
      "\n",
      "Between the first two diffraction minima , 16 interference minima are possible.\n"
     ]
    }
   ],
   "source": [
    "#pg 101\n",
    "#calculate the no. of interference minima\n",
    "#Given :\n",
    "import math\n",
    "d = 8.8*10**-2 ; # slit width in mm\n",
    "b = 0.7;# seperation between slits in mm\n",
    "lambd = 6328. ; #Wavelength in A\n",
    "#First diffraction minima is possible, when  d*sin(theta) = lambd\n",
    "# 1 A = 1.0*10**-7 mm\n",
    "#cakculations and results\n",
    "theta = math.asin((lambd*10**-7)/d)*57.3; # angle in degrees\n",
    "print\"theta (degrees) =  \",round(theta,3)\n",
    "#interference minima is possible , when sin(theta) = ((p + 1/2)*lambd)/b\n",
    "for p in range  (0,10):\n",
    "    #1 A = 1.0*10**-7 mm\n",
    "    theta1 = math.asin((p + 1/2.)*(lambd*10**-7/b))*57.3; # angle in degrees \n",
    "    print\"When p = \",p\n",
    "    print\"theta (degrees) = \",round(theta1,3)\n",
    "    if(theta1 > theta):\n",
    "    \tprint\" When p >=\",p,\",  theta >\",theta,\"degrees .\\n\\nBetween the first two diffraction minima ,\",2*p,\"interference minima are possible.\"\n",
    "    \tbreak;\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 6 - pg 111"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For line D1  and Wavelength 5890 A: \n",
      " Angles at which first order and second order maxima will be observed are : \n",
      " Order : 1 , 20.356 degrees \n",
      " Order : 2 , 44.084 degrees \n",
      "For line D2 and Wavelength 5895.9 A : \n",
      " Angles at which first order and second order maxima will be observed are : \n",
      "Order : 1 , 20.378 degrees \n",
      "Order : 2 , 44.139 degrees \n",
      " When n = 3,  sin(theta)= ((n*lambda*10**-8)/a_plus_b)>1 , which falls outside the sine range, hence third order maximum is not visible\n"
     ]
    }
   ],
   "source": [
    "#pg 111\n",
    "#calculate the angles requried\n",
    "#Given :\n",
    "import math\n",
    "# a+b = (2.54/N)cm\n",
    "N = 15000.;#grating has 15000 lines\n",
    "#calculations and results\n",
    "a_plus_b = 2.54/N ; # grating element in cm\n",
    "#Grating equation, (a+b)*sin(theta_n) = n*lambda, we get :  theta_n = asind((n*lamba)/(a+b))\n",
    "print\"For line D1  and Wavelength 5890 A: \"\n",
    "print\" Angles at which first order and second order maxima will be observed are : \"\n",
    "lambda1 = 5890.; #Wavelength in A\n",
    "for n in range(1,3): # First and second order maxima\n",
    "# 1 A = 1.0*10**-7 mm\n",
    "\ttheta1_n = math.asin((n*lambda1*10**-8)/a_plus_b)*57.3;# angle in degrees\n",
    "\tprint\" Order :\",n,\",\",round(theta1_n,3),\"degrees \"\n",
    "\n",
    "print\"For line D2 and Wavelength 5895.9 A : \"\n",
    "print\" Angles at which first order and second order maxima will be observed are : \"\n",
    "lambda2 = 5895.9 ; #Wavelength in A\n",
    "for n1 in range(1,3): #First and second order maxima\n",
    "# 1 A = 1.0*10**-7 mm\n",
    " \ttheta2_n = math.asin((n1*lambda2*10**-8)/a_plus_b)*57.3;# angle in degrees\n",
    " \tprint\"Order :\",n1,\",\",round(theta2_n,3),\"degrees \"\n",
    "\n",
    "print\" When n = 3,  sin(theta)= ((n*lambda*10**-8)/a_plus_b)>1 , which falls outside the sine range, hence third order maximum is not visible\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 8 - pg 112"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Number of lines \tGrating element (in A)\t Angle of diffraction(degrees)\t Dispersion (degrees/A) \t Resolving Power\n",
      "15000.0 /inch\t\t\t16933.33 \t\t20.36 \t\t\t\t0.371627947644 x 10**-3\t\t\t15000.0\n",
      "15000.0 /cm\t\t\t6666.67 \t\t62.07  \t\t\t\t1.88928508374  x 10**-3\t\t\t15000.0\n",
      "5906.0 /cm\t\t\t16932.2 \t\t20.36  \t\t\t\t0.371659571465  x 10**-3\t\t\t5906.0\n",
      "Error in textbook for dispersion values . Error in decimal point placement .\n"
     ]
    }
   ],
   "source": [
    "#pg 112\n",
    "#calculate the dispersion and resolving power\n",
    "# Given :\n",
    "#(a) 15000 lines per inch \n",
    "import math\n",
    "N1 = 15000.; #15000 lines per inch\n",
    "a1_plus_b1 = (2.54/N1)*10**8 ; #grating element in A\n",
    "lambda1 = 5890.; #Wavelength in A\n",
    "lambda2 = 5895.9 ; # Wavelength in A\n",
    "#calculations\n",
    "deltalambda1 = lambda2-lambda1; #in A\n",
    "#For first order\n",
    "n =1.;\n",
    "theta1 = 20.355; # in degrees\n",
    "deltatheta1 = ((n*deltalambda1)/((a1_plus_b1)*math.cos(theta1/57.3)));# dispersion in degrees/A\n",
    "rp1 = n*N1; # resolving power\n",
    "\n",
    "\n",
    "#(b)15000 lines per cm\n",
    "# 1 cm = 0.393701 inches, so We have 15000 lines per 0.393701 inches. \n",
    "#Therefore, For 1 inch we have 15000/0.393701 = 38099.979 or 38100 lines\n",
    "N2 = 38100 ; #38100 lines per inch\n",
    "a2_plus_b2 = (2.54/N2)*10**8 ; #grating element in A\n",
    "#For first order\n",
    "theta_1 = math.asin((n*lambda1)/(a2_plus_b2))*57.3;# in degrees\n",
    "deltatheta_1 = ((n*deltalambda1)/((a2_plus_b2)*math.cos(theta_1/57.3)));#  dispersion in degrees/A\n",
    "rp2 = n*15000; # resolving power\n",
    "\n",
    "#(c)5906 lines per cm\n",
    "# 1 cm = 0.393701 inches, so We have 5906 lines per 0.393701 inches. \n",
    "#Therefore, For 1 inch we have 5906/0.393701 =  15001.232  or 15001 lines\n",
    "N3 = 15001; #15001 lines per inch\n",
    "a3_plus_b3 = (2.54/N3)*10**8; #grating element in A\n",
    "#For first order\n",
    "theta__1 = math.asin((n*lambda1)/(a3_plus_b3))*57.3; # in degrees\n",
    "deltatheta__1 = ((n*deltalambda1)/((a3_plus_b3)*math.cos(theta__1/57.3))); #  dispersion in degrees/A\n",
    "rp3 = n*5906; # resolving power\n",
    "#results\n",
    "print\" Number of lines \\tGrating element (in A)\\t Angle of diffraction(degrees)\\t Dispersion (degrees/A) \\t Resolving Power\"\n",
    "print N1,\"/inch\\t\\t\\t\",round(a1_plus_b1,2),\"\\t\\t\",round(theta1,2),\"\\t\\t\\t\\t\",deltatheta1*10**3,\"x 10**-3\\t\\t\\t\",rp1\n",
    "print 15000.,\"/cm\\t\\t\\t\",round(a2_plus_b2,2),\"\\t\\t\",round(theta_1,2),\" \\t\\t\\t\\t\",deltatheta_1*1000.,\" x 10**-3\\t\\t\\t\",rp2\n",
    "print 5906.,\"/cm\\t\\t\\t\",round(a3_plus_b3,2),\"\\t\\t\",round(theta__1,2),\" \\t\\t\\t\\t\",deltatheta__1*1000.,\" x 10**-3\\t\\t\\t\",rp3\n",
    "print \"Error in textbook for dispersion values . Error in decimal point placement .\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 9 - pg 114"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a)Wavelength : 4680.0  A \n",
      " Angle of 1st order Diffraction : 28.0 degrees \n",
      " Spacing = 9969.3 A\n",
      "(a)Wavelength : 4800.0  A \n",
      " Angle of 1st order Diffraction : 28.7 degrees \n",
      " Spacing = 9996.0 A\n",
      "(a)Wavelength : 5770.0  A \n",
      " Angle of 1st order Diffraction : 35.5 degrees \n",
      " Spacing = 9936.9 A\n",
      "Mean Spacing (A) =  9967.40613803\n"
     ]
    }
   ],
   "source": [
    "#pg 114\n",
    "#calculate the Wavelength and spacing, angle\n",
    "#Given:\n",
    "#Wavelength\n",
    "import math\n",
    "from math import sin\n",
    "n=1; # first order diffraction\n",
    "lambda1 = 4680. ;# Wavelength in A\n",
    "lambda2 = 4800.; #Wavelength in A\n",
    "lambda3 = 5770. ; # Wave;ength in A\n",
    "# First order diffraction angle\n",
    "theta1 = 28.0/57.3; # angle in radians\n",
    "theta2 = 28.7/57.3; # angle in radians\n",
    "theta3 = 35.5/57.3; #angle in radians\n",
    "#calculations\n",
    "#Grating equation : (a+b) = n*lambda/sin(theta) \n",
    "a1_plus_b1 = (n*lambda1)/sin(theta1); #spacing in A\n",
    "a2_plus_b2 = (n*lambda2)/sin(theta2); #spacing in A\n",
    "a3_plus_b3 = (n*lambda3)/sin(theta3); #spacing  in A\n",
    "mean_spacing = (a1_plus_b1 + a2_plus_b2 + a3_plus_b3)/3; # mean spacing in A \n",
    "#results\n",
    "print\"(a)Wavelength :\",lambda1,\" A \\n Angle of 1st order Diffraction :\",round(theta1*57.3,1),\"degrees \\n Spacing =\",round(a1_plus_b1,1),\"A\"\n",
    "print\"(a)Wavelength :\",lambda2,\" A \\n Angle of 1st order Diffraction :\",round(theta2*57.3,1),\"degrees \\n Spacing =\",round(a2_plus_b2,1),\"A\"\n",
    "print\"(a)Wavelength :\",lambda3,\" A \\n Angle of 1st order Diffraction :\",round(theta3*57.3,1),\"degrees \\n Spacing =\",round(a3_plus_b3,1),\"A\"\n",
    "print\"Mean Spacing (A) = \",mean_spacing\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 10 - pg 115"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The first order maximum will be obtained at (degrees) =  0.003\n"
     ]
    }
   ],
   "source": [
    "#pg 115\n",
    "#calculate the first order maximum\n",
    "#Given:\n",
    "import math\n",
    "N = 15000.;#Number of lines per inch\n",
    "a_plus_b = (2.54/N)*10**8 ;#Grating period in A\n",
    "lambd = 1 ; #Wavelength in A\n",
    "#Grating equation :(a+b)*sin(theta_n) = n*lambd\n",
    "#First order maximum \n",
    "#calculations\n",
    "theta1 = math.asin(lambd/a_plus_b)*57.3; # angle in degrees\n",
    "#results\n",
    "print \"The first order maximum will be obtained at (degrees) = \",round(theta1,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 11 - pg 118"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Maximum value for L should be (mm) =  3633.9\n"
     ]
    }
   ],
   "source": [
    "#pg 118\n",
    "#calculate the Maximum value for L\n",
    "#Given:\n",
    "import math\n",
    "lambd = 6000.; #Wwavelength in A\n",
    "mu = 1.33; #Refractive index for cornea\n",
    "D = 2.; #Diameter of pupil in mm\n",
    "#calculations\n",
    "#Yellow light wavelength in eye:\n",
    "lambd1 = lambd/mu ; #Wavelength in A\n",
    "#The angular resolution \n",
    "#1 A = 1.0*10^-7 mm\n",
    "theta_c = (1.22*lambd1*10**-7)/D; # angle in rad\n",
    "#Maximum value for L\n",
    "L = 1/math.tan(theta_c); # in mm\n",
    "#results\n",
    "print \"Maximum value for L should be (mm) = \",round(L,1)\n"
   ]
  }
 ],
 "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
