{
 "metadata": {
  "name": "",
  "signature": "sha256:891c986a46f113e35878dc6d6a0b8d702286ad7b4ea91bae03f716286f9bdeaa"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 4: Electrostatic Fields<h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 4.1, Page number: 107<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "from numpy import *\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "ax=array([1,0,0])                   #Unit vector along x direction\n",
      "ay=array([0,1,0])                   #Unit vector along y direction\n",
      "az=array([0,0,1])                   #Unit vector along z direction\n",
      "Q1=1*10**-3                         #charge 1 at (-1,-1,4) in C\n",
      "Q2=-2*10**-3                        #charge 2 at (3,2,-1) in C\n",
      "Q=10*10**-9                         #charge 3 at (0,3,1) in C\n",
      "P1=array([0,3,1])-array([3,2,-1])   #distance vector from charge 3 to 1\n",
      "P2=array([0,3,1])-array([-1,-1,4])  #distance vector from charge 3 to 2\n",
      "e=10**-9/(36*scipy.pi)              #permittivity in Farad/m \n",
      "\n",
      "#Calculations\n",
      "\n",
      "modP1=scipy.sqrt(dot(P1,P1))\n",
      "modP2=scipy.sqrt(dot(P2,P2))\n",
      "F1=(Q*Q1)*P1*10**3/(4*scipy.pi*e*modP1**3)  #force on charge 3 by 1 in mN\n",
      "F2=(Q*Q2)*P2*10**3/(4*scipy.pi*e*modP2**3)  #force on charge 3 by 2 in mN\n",
      "\n",
      " #Total force on charge 3\n",
      " \n",
      "Fx=round(dot(F1+F2,ax),3)\n",
      "Fy=round(dot(F1+F2,ay),3)\n",
      "Fz=round(dot(F1+F2,az),3)\n",
      "F=array([Fx,Fy,Fz])         #Total force in mN\n",
      "E=(10**-6)*(F/Q)            #Electric field in kV/m\n",
      "\n",
      "#Results  \n",
      "\n",
      "print 'Total force on charge at (0,3,1) =',F,'in mN'\n",
      "print 'Electric field at (0,3,1) =',E,'kV/m'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Total force on charge at (0,3,1) = [-6.512 -3.713  7.509] in mN\n",
        "Electric field at (0,3,1) = [-651.2 -371.3  750.9] kV/m\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 4.3, Page number: 109"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "#Variable Declaration\n",
      "\n",
      "E=500*10**3      #electric field in V/m\n",
      "Qm=9*10**-6      #Q/m in C/kg\n",
      "y=0.8            #distance fallen in m\n",
      "g=9.8            #acceleration due to gravity in m/s^2\n",
      "\n",
      "#Calculations\n",
      "\n",
      "t=scipy.sqrt(2*y/g)   #time taken to fall in seconds\n",
      "x=Qm*E*t**2/2         #half the separation between particles in m\n",
      "sep=2*x               #separation between particles in m\n",
      "\n",
      "#Result\n",
      "\n",
      "print 'The separation between particles is',round(sep*100,2),'cm'\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The separation between particles is 73.47 cm\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 4.5, Page number: 120"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "import scipy.integrate\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "Eo=10**-9/(36*scipy.pi)       #permittivity of free space\n",
      "ax=array([1,0,0])             #Unit vector along x direction\n",
      "ay=array([0,1,0])             #Unit vector along y direction\n",
      "az=array([0,0,1])             #Unit vector along z direction\n",
      "q=-1                          #charge in mC\n",
      "\n",
      "#Calculations\n",
      "\n",
      "def charge(x,y): \n",
      " return x*y*(x**2+y**2+25)**(1.5)\n",
      "Q, errq = scipy.integrate.dblquad(lambda y , x: charge(x,y),  #total charge in nC\n",
      "             0, 1, lambda y: 0, lambda y: 1) \n",
      "\n",
      "d=(4*scipy.pi*Eo*(x**2+y**2+25)**(1.5))\n",
      "\n",
      "def elecx(x,y): \n",
      " return 10**-9*x*y*(x**2+y**2+25)**(1.5)*(-x)/d               #x component of electric field\n",
      "Ex, errx = scipy.integrate.dblquad(lambda y , x: elecx(x,y),  \n",
      "             0, 1, lambda y: 0, lambda y: 1) \n",
      "\n",
      "def elecy(x,y): \n",
      " return 10**-9*x*y*(x**2+y**2+25)**(1.5)*(-y)/d               #y component of electric field\n",
      "Ey, erry = scipy.integrate.dblquad(lambda y , x: elecy(x,y),  \n",
      "             0, 1, lambda y: 0, lambda y: 1) \n",
      "\n",
      "def elecz(x,y): \n",
      " return 10**-9*x*y*(5)/(4*scipy.pi*Eo)                        #z component of electric field\n",
      "Ez, errz = scipy.integrate.dblquad(lambda y , x: elecz(x,y),  \n",
      "             0, 1, lambda y: 0, lambda y: 1) \n",
      "\n",
      "E=array([round(Ex,1),round(Ey,1),round(Ez,2)])                #electric field in V/m\n",
      "\n",
      "F=q*E   #force in mN    \n",
      "\n",
      "#Results\n",
      "\n",
      "print 'Total charge =',round(Q,2),'nC'\n",
      "print 'Electric field at (0,0,5) =',E,'V/m'\n",
      "print 'Force experienced by -1mC =',F,'mN'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Total charge = 33.15 nC\n",
        "Electric field at (0,0,5) = [ -1.5   -1.5   11.25] V/m\n",
        "Force experienced by -1mC = [  1.5    1.5  -11.25] mN\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 4.6, Page number: 121<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "from numpy import *\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "ax=array([1,0,0])                   #Unit vector along x direction\n",
      "ay=array([0,1,0])                   #Unit vector along y direction\n",
      "az=array([0,0,1])                   #Unit vector along z direction\n",
      "ps1=10*10**-9                       #Surface charge density of plane 1\n",
      "ps2=15*10**-9                       #Surface charge density of plane 2\n",
      "pl=10*scipy.pi*10**-9               #charge density of line\n",
      "e=(10**-9)/(36*scipy.pi)            #permittivity of free space in Farad/m\n",
      "\n",
      "#Calculations\n",
      "\n",
      "E1=(ps1/(2*e))*-ax/scipy.pi     #field due to plane 1 in multiples of pi in V/m\n",
      "E2=(ps2/(2*e))*ay/scipy.pi      #field due to plane 2 in multiples of pi in V/m\n",
      "\n",
      " #field due to line charge in multiples of pi in V/m\n",
      " \n",
      "a=(ax-3*az) \n",
      "moda=scipy.sqrt(dot((ax-3*az),(ax-3*az)))\n",
      "e3=(pl/(2*scipy.pi*e*moda**2))*a\n",
      "E3=array([dot(e3,ax)/scipy.pi,0,dot(e3,az)/scipy.pi])\n",
      "\n",
      " #total field in multiples of pi in V/m\n",
      " \n",
      "E=E1+E2+E3      \n",
      "\n",
      "#Result\n",
      "\n",
      "print 'The total electric field at (1,1,-1) =',E,'Pi V/m'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The total electric field at (1,1,-1) = [-162.  270.  -54.] Pi V/m\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 4.7, Page number: 123<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "from numpy import *\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "ax=array([1,0,0])               #Unit vector along x direction\n",
      "ay=array([0,1,0])               #Unit vector along y direction\n",
      "az=array([0,0,1])               #Unit vector along z direction\n",
      "Q=-5*scipy.pi*10**-3            #charge at (4,0,0) in C\n",
      "pl=3*scipy.pi*10**-3            #charge density of line charge in C/m\n",
      "r=array([4,0,3])                #point where D is to be found \n",
      "rp=array([4,0,0])               #position of point charge\n",
      "\n",
      "#Calculations \n",
      "\n",
      "R=r-rp \n",
      "modR=scipy.sqrt(dot(R,R)) \n",
      "Dq=(Q*R)/(4*scipy.pi*modR**3)    #flux density due to point charge in C/m^2\n",
      "p=scipy.sqrt(dot(r,r))\n",
      "ap=r/p \n",
      "Dl=(pl/(2*scipy.pi*p))*ap        #flux density due to line charge in C/m^2\n",
      "D=(Dq+Dl)*10**6                  #total flux density in micro C/m^2\n",
      "Dz=round(dot(D,az),0)\n",
      "Dx=round(dot(D,ax),0)\n",
      "Dy=round(dot(D,ay),0)\n",
      "Dround=array([Dx,Dy,Dz])         #value of D rounded to 0 decimal points\n",
      "\n",
      "#Result\n",
      "\n",
      "print 'D at (4,0,0) due to point charge and line charge =',Dround,'micro C/m^2'\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "D at (4,0,0) due to point charge and line charge = [ 240.    0.   41.] micro C/m^2\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 4.8, Page number: 130<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "\n",
      "import scipy\n",
      "from numpy import *\n",
      "import scipy.integrate\n",
      "from fractions import Fraction\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "ap=array([1,0,0])                   #Unit vector along rho direction\n",
      "aph=array([0,1,0])                  #Unit vector along phi direction\n",
      "az=array([0,0,1])                   #Unit vector along z direction\n",
      "point=array([1,scipy.pi/4,3])\n",
      "p1=0\n",
      "p2=1\n",
      "ph1=0\n",
      "ph2=2*scipy.pi\n",
      "\n",
      "#Calculations\n",
      "\n",
      "pointp=dot(point,ap)\n",
      "pointph=dot(point,aph)\n",
      "pv=pointp*scipy.cos(pointph)**2     #charge density at (1,pi/4,3) in C/m^3\n",
      "\n",
      "def ctop(phi,p): \n",
      " return 2*p**2*(scipy.cos(phi)**2)\n",
      "psya, erra = scipy.integrate.dblquad(lambda p , phi: ctop(phi,p),    \n",
      "             ph1, ph2, lambda p: p1, lambda p: p2)\n",
      "\n",
      "def cbot(phi,p): \n",
      " return 2*p**2*(scipy.cos(phi)**2)\n",
      "psyb, errb = scipy.integrate.dblquad(lambda p , phi: cbot(phi,p),    \n",
      "             ph1, ph2, lambda p: p1, lambda p: p2)\n",
      "             \n",
      "psy=psya+psyb                               #Charge in C\n",
      "psyp=psy/scipy.pi                           #Charge in multiples of Pi in C\n",
      "psyf=Fraction(psyp).limit_denominator(100)  #converting to fraction\n",
      "\n",
      "\n",
      "#Results\n",
      "\n",
      "print 'Charge density at (1,pi/4,3) =',pv,'C/m^3'\n",
      "print 'Total charge enclosed by the cylinder =',psyf,'Pi C'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Charge density at (1,pi/4,3) = 0.5 C/m^3\n",
        "Total charge enclosed by the cylinder = 4/3 Pi C\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 4.10, Page number: 136<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "\n",
      "import scipy\n",
      "from numpy import *\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "Q1=-4                   #charge 1 in micro C\n",
      "Q2=5                    #charge 2 in micro C\n",
      "e=10**-9/(36*scipy.pi)  #permittivity of free space in Farad/m \n",
      "\n",
      "#Calculations\n",
      "\n",
      "R1=array([1,0,1])-array([2,-1,3])  #distance vector from (1,0,1) to charge 1\n",
      "R2=array([1,0,1])-array([0,4,-2])  #distance vector from (1,0,1) to charge 2\n",
      "modR1=scipy.sqrt(dot(R1,R1))\n",
      "modR2=scipy.sqrt(dot(R2,R2)) \n",
      "V=10**-9*((Q1/modR1)+(Q2/modR2))/(4*scipy.pi*e)  #potential in kV\n",
      "\n",
      "#Result\n",
      "\n",
      "print 'The potential at (1, 0, 1) =',round(V,3),'kV'\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The potential at (1, 0, 1) = -5.872 kV\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 4.11, Page number: 136"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "Eo=10**-9/(36*scipy.pi)       #permittivity of free space\n",
      "Vo=0                          #potential at O in V\n",
      "Vb=100                        #potential at B in V\n",
      "po=scipy.sqrt(2)\n",
      "ro=5\n",
      "pa=1\n",
      "ra=9\n",
      "pb=1\n",
      "rb=scipy.sqrt(21)\n",
      "pc=scipy.sqrt(20)\n",
      "rc=scipy.sqrt(11)\n",
      "pl=2*10**-9                   #charge density of the line in C/m\n",
      "Q=5*10**-9                    #point charge at (-3,4,0) in C\n",
      "\n",
      "#Calculations\n",
      "\n",
      "Va=Vo-(-pl*scipy.log(po/pa)/(2*scipy.pi*Eo)+Q*(ra-ro)/(4*scipy.pi*Eo*ra*ro))\n",
      "Vc=Vb+(-pl*scipy.log(pc/pb)/(2*scipy.pi*Eo)+Q*(rb-rc)/(4*scipy.pi*Eo*rb*rc))\n",
      "Vbc=Vc-Vb\n",
      "\n",
      "#Results\n",
      "\n",
      "print 'Va =',round(Va,3),'V'\n",
      "print 'Vc =',round(Vc,3),'V'\n",
      "print 'Vbc =',round(Vbc,3),'V'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Va = 8.477 V\n",
        "Vc = 49.825 V\n",
        "Vbc = -50.175 V\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 4.12, Page number: 140<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "\n",
      "import scipy\n",
      "from numpy import *\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "ar=array([1,0,0])                   #Unit vector along radial direction\n",
      "ath=array([0,1,0])                  #Unit vector along theta direction\n",
      "aph=array([0,0,1])                  #Unit vector along phi direction\n",
      "e=(10**-9)/(36*scipy.pi)            #permittivity of free space in Farad/m\n",
      "\n",
      " #The point (2, pi/2, 0)\n",
      "r=2\n",
      "th=scipy.pi/2\n",
      "ph=0\n",
      " #Point A\n",
      "ra=1\n",
      "tha=scipy.pi*30/180\n",
      "pha=scipy.pi*120/180\n",
      " #Point B\n",
      "rb=4\n",
      "thb=scipy.pi/2\n",
      "phb=scipy.pi*60/180\n",
      "\n",
      "q=10*10**-6 \n",
      "\n",
      "#Calculations\n",
      "\n",
      "Er=(20.0/r**3)*scipy.sin(th)*scipy.cos(ph)   #Radial component of E in V/m\n",
      "Eth=-(10/r**3)*scipy.cos(th)*scipy.cos(ph)   #Theta component of E in V/m\n",
      "Eph=(10/r**3)*scipy.sin(ph)                  #Phi component of E in V/m\n",
      "E=array([Er,Eth,Eph])\n",
      "D=E*e*10**12              #Electric flux density D in pC/m^2\n",
      "Dr=round(dot(D,ar),1)     #Radial component of D in V/m rounded to 1 decimal\n",
      "Dth=round(dot(D,ath),0)   #Theta component of D in pC/m^2 rounded to 0 decimal\n",
      "Dph=round(dot(D,aph),0)   #Phi component of D in pC/m^2 rounded to 0 decimal\n",
      "Dc=array([Dr,Dth,Dph])    #Rounded D in pC/m^2\n",
      "\n",
      "Va=10*scipy.sin(tha)*cos(pha)/ra**2    #potential at point A in V\n",
      "Vb=10*scipy.sin(thb)*cos(phb)/rb**2    #potential at point B in V\n",
      "W=q*(Vb-Va)*10**6                      #work done in micro J\n",
      "\n",
      "#Results\n",
      "\n",
      "print 'The electric flux density D at (2, pi/2, 0) =',Dc,'pC/m^2'\n",
      "print 'Work done in moving the charge =',W,'micro J'\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The electric flux density D at (2, pi/2, 0) = [ 22.1  -0.    0. ] pC/m^2\n",
        "Work done in moving the charge = 28.125 micro J\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 4.13, Page number: 145<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "\n",
      "import scipy\n",
      "from numpy import *\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "p1=-5*10**-9               #dipole moment of dipole 1 in C/m\n",
      "p2=9*10**-9                #dipole moment of dipole 2 in C/m\n",
      "z1=2                       #z component of position vector of dipole 1\n",
      "z2=-3                      #z component of position vector of dipole 2\n",
      "e=10**-9/(36*scipy.pi)     #permittivity of free space in Farad/m\n",
      "\n",
      "#Calculation\n",
      "\n",
      "V=(1/(4*scipy.pi*e))*((p1*abs(z1)/z1**3)+(p2*abs(z2)/z2**3))\n",
      "\n",
      "#Result\n",
      "\n",
      "print 'Potential at origin =',V, 'V'\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Potential at origin = -20.25 V\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 4.14, Page number: 148<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "from numpy import *\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "Q1=-1*10**-9            #Charge 1 in C\n",
      "Q2=4*10**-9             #Charge 2 in C\n",
      "Q3=3*10**-9             #Charge 3 in C\n",
      "e=10**-9/(36*scipy.pi)  #permittivity of free space in farad/m\n",
      "\n",
      "#Calculations\n",
      "\n",
      "V1=(1/(4*scipy.pi*e)*(Q2+Q3))\n",
      "V2=(1/(4*scipy.pi*e)*(Q1+Q3/(2**.5)))\n",
      "V3=(1/(4*scipy.pi*e)*(Q1+Q2/(2**.5)))\n",
      "W=0.5*((V1*Q1)+(V2*Q2)+(V3*Q3))*10**9  #Energy in nJ\n",
      "\n",
      "#Result\n",
      "\n",
      "print 'Energy in the system =',round(W,2),'nJ'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Energy in the system = 13.37 nJ\n"
       ]
      }
     ],
     "prompt_number": 8
    }
   ],
   "metadata": {}
  }
 ]
}