{
 "metadata": {
  "name": "",
  "signature": "sha256:02fdf3d71909ffefa3365717544dbd108f534a28cb15d60e71866e85ef9ac76f"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 3: Vector Calculus<h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 3.1, Page number: 58<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "\n",
      "import scipy\n",
      "from numpy import *\n",
      "import scipy.integrate\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "A=array([5,0,0])\n",
      "B=array([0,5,0])\n",
      "C=array([0,5,10])\n",
      "D=array([5,0,10])\n",
      "\n",
      "#Calculations\n",
      "\n",
      " #A,B,C,D in cylindrical coordinates\n",
      " \n",
      "A=array([5,0,0])\n",
      "B=array([5,scipy.pi,0])\n",
      "C=array([5,scipy.pi,10])\n",
      "D=array([5,0,10])\n",
      "\n",
      "p=5\n",
      "\n",
      "def BC(z): \n",
      " return 1\n",
      "ansa, erra = scipy.integrate.quad(BC, 0, 10)\n",
      " \n",
      "def CD(phi): \n",
      " return p\n",
      "ansb, errb = scipy.integrate.quad(CD, 0, scipy.pi/2)\n",
      "ansbb=ansb/scipy.pi         #answer in multiples of pi\n",
      "\n",
      "def ABCD(phi,z): \n",
      " return p\n",
      "ansc, errc = scipy.integrate.dblquad(lambda z , phi: ABCD(phi,z),    \n",
      "             0, scipy.pi/2, lambda z: 0, lambda z: 10) \n",
      "anscc=ansc/scipy.pi         #answer in multiples of pi\n",
      "             \n",
      "def ABO(phi,rho): \n",
      " return rho\n",
      "ansd, errd = scipy.integrate.dblquad(lambda rho , phi: ABO(phi,rho),    \n",
      "             0, scipy.pi/2, lambda rho: 0, lambda rho: 5)\n",
      "ansdd=ansd/scipy.pi         #answer in multiples of pi\n",
      "\n",
      "def AOFD(rho,z): \n",
      " return 1\n",
      "anse, erre = scipy.integrate.dblquad(lambda z , rho: AOFD(rho,z),    \n",
      "             0, 10, lambda z: 0, lambda z: 5)\n",
      "             \n",
      "def ABDCFO(z,phi,rho):\n",
      " return rho\n",
      "ansf, errf=scipy.integrate.tplquad(ABDCFO,0,5,lambda rho:0,\n",
      "  lambda rho:scipy.pi/2,lambda rho,phi:0,lambda rho,phi:10)\n",
      "ansff=ansf/scipy.pi         #answer in multiples of pi\n",
      "\n",
      "#Results\n",
      "\n",
      "print 'The distance BC =',ansa\n",
      "print 'The distance CD =',ansbb,'pi'\n",
      "print 'The surface area ABCD =',anscc,'pi'\n",
      "print 'The surface area ABO =',ansdd,'pi'\n",
      "print 'The surface area AOFD =',anse\n",
      "print 'The volume ABDCFO =',ansff,'pi'\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The distance BC = 10.0\n",
        "The distance CD = 2.5 pi\n",
        "The surface area ABCD = 25.0 pi\n",
        "The surface area ABO = 6.25 pi\n",
        "The surface area AOFD = 50.0\n",
        "The volume ABDCFO = 62.5 pi\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 3.2, Page number: 61<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "from numpy import *\n",
      "import scipy.integrate\n",
      "from fractions import Fraction\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",
      "\n",
      "#Calculations\n",
      "\n",
      "def C1(x): \n",
      " return x**2\n",
      "seg1, err1 = scipy.integrate.quad(C1, 1, 0)     #segment 1\n",
      "Seg1=Fraction(seg1).limit_denominator(100)      #converting to fraction\n",
      "\n",
      "def C2(y): \n",
      " return 0\n",
      "seg2, err2 = scipy.integrate.quad(C2, 0, 1)     #segment 2\n",
      "\n",
      "def C3(x): \n",
      " return (x**2-1)\n",
      "seg3, err3 = scipy.integrate.quad(C3, 0, 1)     #segment 3\n",
      "Seg3=Fraction(seg3).limit_denominator(100)      #converting to fraction\n",
      "\n",
      "def C4(y): \n",
      " return (-y-y**2)\n",
      "seg4, err4 = scipy.integrate.quad(C4, 1, 0)     #segment 4\n",
      "Seg4=Fraction(seg4).limit_denominator(100)      #converting to fraction\n",
      "\n",
      "seg=Seg1+seg2+Seg3+Seg4                         #total circulation around path\n",
      "Seg=Fraction(seg).limit_denominator(100)        #converting to fraction\n",
      "\n",
      "#Results\n",
      "\n",
      "print 'F along segment 1 is',Seg1\n",
      "print 'F along segment 2 is',seg2\n",
      "print 'F along segment 3 is',Seg3\n",
      "print 'F along segment 4 is',Seg4\n",
      "print 'Circulation of F around the path is',Seg"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "F along segment 1 is -1/3\n",
        "F along segment 2 is 0.0\n",
        "F along segment 3 is -2/3\n",
        "F along segment 4 is 5/6\n",
        "Circulation of F around the path is -1/6\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 3.4, Page number: 68"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "from fractions import Fraction\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",
      "Al=array([3,4,12])\n",
      "x=2\n",
      "y=-1\n",
      "z=0\n",
      "\n",
      "#Calculations\n",
      "\n",
      "gradW=(2*x*y**2+y*z)*ax+(2*x**2*y+x*z)*ay+(x*y)*az\n",
      "gradWl=Fraction(dot(gradW,Al)/scipy.sqrt(dot(Al,Al))).limit_denominator(1000)\n",
      "\n",
      "#Result\n",
      "\n",
      "print 'dW/dl =',gradWl\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "dW/dl = -44/13\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Example 3.7, Page number: 74"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "import scipy.integrate\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "ap=array([1,0,0])              #Unit vector along radial direction\n",
      "az=array([0,0,1])              #Unit vector along z direction\n",
      "\n",
      "#Calculations\n",
      "\n",
      "def psi1(phi,p): \n",
      " return 10*scipy.e**(-2)*p\n",
      "psit, errt = scipy.integrate.dblquad(lambda p , phi: psi1(phi,p),    #flux through top\n",
      "             0, 2*scipy.pi, lambda p: 0, lambda p: 1) \n",
      "\n",
      "def psi2(phi,p): \n",
      " return -10*p\n",
      "psib, errb = scipy.integrate.dblquad(lambda p , phi: psi2(phi,p),    #flux through bottom\n",
      "             0, 2*scipy.pi, lambda p: 0, lambda p: 1) \n",
      "\n",
      "def psi3(phi,z): \n",
      " return 10*scipy.exp(-2*z)\n",
      "psis, errs = scipy.integrate.dblquad(lambda z , phi: psi3(phi,z),    #flux through side\n",
      "             0, scipy.pi*2, lambda z: 0, lambda z: 1) \n",
      "\n",
      "psi=psit+psib+psis    #total flux through cylinder\n",
      "\n",
      "#Results\n",
      "\n",
      "print 'The total flux through the cylinder is',psi\n",
      "print 'The total flux through cylinder using divergence theorem is also 0'\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The total flux through the cylinder is 0.0\n",
        "The total flux through cylinder using divergence theorem is also 0\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 3.9, Page number: 81<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "from numpy import *\n",
      "import scipy.integrate\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "ap=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",
      "\n",
      "#Calculations\n",
      "\n",
      " #segment 1\n",
      "def ab(phi): \n",
      " return 2*scipy.sin(phi)\n",
      "seg1,err1 = scipy.integrate.quad(ab,scipy.pi*60/180,scipy.pi*30/180) \n",
      "\n",
      " #segment 2\n",
      "def bc(p): \n",
      " return p*scipy.cos(scipy.pi*30/180)\n",
      "seg2,err2 = scipy.integrate.quad(bc,2,5)    \n",
      "\n",
      " #segment 3\n",
      "def cd(phi): \n",
      " return 5*scipy.sin(phi)\n",
      "seg3,err3 = scipy.integrate.quad(cd,-scipy.pi*30/180,scipy.pi*60/180)\n",
      "\n",
      " #segment 4\n",
      "def da(p): \n",
      " return p*scipy.cos(scipy.pi*60/180)\n",
      "seg4,err4 = scipy.integrate.quad(da,5,2)\n",
      "\n",
      "I1=seg1+seg2+seg3+seg4\n",
      "\n",
      " #using stoke's theorem\n",
      "\n",
      "def curlA(phi,p): \n",
      " return ((1+p)*scipy.sin(phi))\n",
      "I2, err = scipy.integrate.dblquad(lambda p , phi: curlA(phi,p),    \n",
      "             scipy.pi*30/180, scipy.pi*60/180, lambda p: 2, lambda p: 5)\n",
      "\n",
      "#Results\n",
      "\n",
      "print 'The integral calculated segment wise =',round(I1,3)\n",
      "print 'The integral calculated using Stokes Theorem =',round(I2,3)\n",
      "print 'Since I1 = I2, Stokes theorem is verified'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The integral calculated segment wise = 4.941\n",
        "The integral calculated using Stokes Theorem = 4.941\n",
        "Since I1 = I2, Stokes theorem is verified\n"
       ]
      }
     ],
     "prompt_number": 3
    }
   ],
   "metadata": {}
  }
 ]
}