{
 "metadata": {
  "name": "",
  "signature": "sha256:d78c85d754b20d2817dcaff01d1e4d9adbe09676da8e1b1ab97dcc0ae047a88a"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 2: Coordinate Systems and Transformation<h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2.1, Page number: 36<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "from numpy import *\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "x=-2\n",
      "y=6\n",
      "z=3 \n",
      "\n",
      "#Calculations\n",
      "\n",
      "r=scipy.sqrt(x**2+y**2)\n",
      "phi=scipy.arctan(-y/x)          #phi in radians in 1st quadrant\n",
      "phid=180-(phi*180/scipy.pi)     #phi in degrees in second quadrant\n",
      "phic=scipy.pi*phid/180.0        #phi in radians in second quadrant\n",
      "R=scipy.sqrt(x**2+y**2+z**2) \n",
      "theta=scipy.arctan(r/z) \n",
      "\n",
      " #P in cylindrical coordinates\n",
      "  \n",
      "Pcyl=array([round(r,2),round(phid,2),z])     \n",
      "\n",
      " #P in spherical coordinates\n",
      " \n",
      "Psph=array([round(R,2),round(theta*180/scipy.pi,2),\n",
      " round(phid,2)]) \n",
      "\n",
      " #Vector A in cylindrical coordinate system\n",
      "\n",
      "Xc=r*scipy.cos(phic)\n",
      "Yc=r*scipy.sin(phic)\n",
      "Zc=z \n",
      "Ar=Yc*scipy.cos(phic)+(Xc+Zc)*scipy.sin(phic)\n",
      "Aphi=-Yc*scipy.sin(phic)+(Xc+Zc)*scipy.cos(phic)\n",
      "Az=0\n",
      "Acyl=array([round(Ar,4),round(Aphi,3),Az])\n",
      "\n",
      " #Vector A in spherical coordinate system\n",
      "\n",
      "Xs=R*scipy.cos(phic)*scipy.sin(theta)\n",
      "Ys=R*scipy.sin(phic)*scipy.sin(theta)\n",
      "Zs=R*scipy.cos(theta)\n",
      "AR=Ys*scipy.sin(theta)*scipy.cos(phic)+(Xs+Zs)*scipy.sin(theta)*scipy.sin(phic) \n",
      "Ath=Ys*scipy.cos(theta)*scipy.cos(phic)+(Xs+Zs)*scipy.cos(theta)*scipy.sin(phic) \n",
      "Aph=-Ys*scipy.sin(phic)+(Xs+Zs)*scipy.cos(phic)\n",
      "Asph=array([round(AR,4),round(Ath,4),round(Aph,3)])\n",
      "\n",
      "#Results\n",
      "\n",
      "print 'P in cylindrical coordinates =',Pcyl\n",
      "print 'P in spherical coordinates =',Psph\n",
      "print 'A in cylindrical coordinates =',Acyl\n",
      "print 'A in spherical coordinates =',Asph"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "P in cylindrical coordinates = [   6.32  108.43    3.  ]\n",
        "P in spherical coordinates = [   7.     64.62  108.43]\n",
        "A in cylindrical coordinates = [-0.9487 -6.008   0.    ]\n",
        "A in spherical coordinates = [-0.8571 -0.4066 -6.008 ]\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2.2, Page number: 39<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "from numpy import *\n",
      " \n",
      "#Variable Declaration\n",
      "\n",
      "x=-3\n",
      "y=4\n",
      "z=0\n",
      "p=5\n",
      "phi=scipy.pi/2          \n",
      "Zc=-2\n",
      "\n",
      "#Calculations\n",
      "\n",
      " #B in cartesian coordinates\n",
      "\n",
      "R=scipy.sqrt(x**2+y**2+z**2)\n",
      "r=scipy.sqrt(x**2+y**2)  \n",
      "P=scipy.arcsin(r/R)             #in radians\n",
      "Q=scipy.arccos(x/r)             #in radians \n",
      "f=10/R \n",
      "Bx=f*scipy.sin(P)*scipy.cos(Q)+R*(scipy.cos(P))**2*scipy.cos(Q)-scipy.sin(Q) \n",
      "By=f*scipy.sin(P)*scipy.sin(Q)+R*(scipy.cos(P))**2*scipy.sin(Q)+scipy.cos(Q) \n",
      "Bz=f*scipy.cos(P)-R*scipy.cos(P)*scipy.sin(P) \n",
      "Bcart=array([round(Bx,0),round(By,0),round(-Bz,0)])\n",
      "\n",
      " #B in cylindrical coordinates\n",
      " \n",
      "Rc=sqrt(p**2+Zc**2)             \n",
      "Pc=scipy.arccos(Zc/Rc)          #in radians\n",
      "Br=(10/Rc)*scipy.sin(Pc)+Rc*(scipy.cos(Pc))**2  \n",
      "Bp=1 \n",
      "Bzc=(10/Rc)*scipy.cos(Pc)-Rc*scipy.cos(Pc)*scipy.sin(Pc) \n",
      "Bcyl=array([round(Br,3),Bp,round(Bzc,3)])\n",
      "\n",
      "#Results\n",
      "\n",
      "print 'B(-3,4,0) in cartesian coordinates is',Bcart\n",
      "print 'B(5,pi/2,-2) in cylindrical coordinates is',Bcyl"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "B(-3,4,0) in cartesian coordinates is [-2.  1.  0.]\n",
        "B(5,pi/2,-2) in cylindrical coordinates is [ 2.467  1.     1.167]\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2.3, Page number: 44<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import scipy\n",
      "from numpy import *\n",
      "\n",
      "#Variable Declaration\n",
      "\n",
      "E=array([-5,10,3])              #in cylindrical coordinates\n",
      "F=array([1,2,-6])               #in cylindrical coordinates\n",
      "P=array([5,scipy.pi/2,3])       #in cylindrical coordinates\n",
      "\n",
      "#Calculations\n",
      "\n",
      "exf=cross(E,F)\n",
      "ansa=scipy.sqrt(dot(exf,exf))   #|EXF|\n",
      "ay=array([round(scipy.sin(scipy.pi/2),0),\n",
      " round(scipy.cos(scipy.pi/2),0),0])\n",
      "ansb=dot(E,ay)*ay\n",
      "modE=scipy.sqrt(dot(E,E))\n",
      "az=array([0,0,1])\n",
      "thetaEz=(180/scipy.pi)*arccos(dot(E,az)/modE)   #in degrees\n",
      "ansc=90-thetaEz                                 #in degrees\n",
      "\n",
      "#Results\n",
      "\n",
      "print '|EXF| =',round(ansa,2)\n",
      "print 'The vector component of E at P parallel to the line x=2,z=3 =',ansb,','\n",
      "print 'in spherical coordinates'\n",
      "print 'The angle E makes with the surface z = 3 at P =',round(ansc,2),'degrees'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "|EXF| = 74.06\n",
        "The vector component of E at P parallel to the line x=2,z=3 = [-5. -0. -0.] ,\n",
        "in spherical coordinates\n",
        "The angle E makes with the surface z = 3 at P = 15.02 degrees\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 2.4, Page number: 45<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \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",
      "P=array([10,scipy.pi*150/180,scipy.pi*330/180])\n",
      "\n",
      "#Calculations\n",
      "\n",
      "r=dot(P,aR)\n",
      "q=dot(P,aph)\n",
      "p=dot(P,ath)\n",
      "R=r*scipy.sin(q)\n",
      "Ph=-scipy.sin(p)*scipy.cos(q)/r\n",
      "Q=r*r\n",
      "D=array([R,Ph,Q])               #D at P(10,150\u00b0,330\u00b0)\n",
      "rDr=round(dot(aR,D),0)          #radial component of D\n",
      "rDth=round(dot(-ath,D),3)       #theta component of D\n",
      "rDph=round(dot(aph,D),0)        #phi component of D\n",
      "\n",
      "Dn=array([r*scipy.sin(q),0,0])  #Component of D normal to surface r=10\n",
      "Dt=D-Dn                         #Component of D tangential to surface r=10\n",
      "Dtr=round(dot(aR,Dt),0)         #radial component of Dt\n",
      "Dtth=round(dot(-ath,Dt),3)      #theta component of Dt\n",
      "Dtph=round(dot(aph,Dt),0)       #phi component of Dt\n",
      "rDt=array([Dtr,Dtth,Dtph])\n",
      "\n",
      " #Unit vector normal to D and tangential to cone theta=45 degrees\n",
      "\n",
      "U=cross(D,ath)\n",
      "u=U/scipy.sqrt(dot(U,U))    \n",
      "ru=array([round(dot(aR,u),4),round(dot(ath,u),4),round(dot(aph,u),4)])\n",
      "\n",
      "#Results\n",
      "\n",
      "print 'D at P(10,150\u00b0,330\u00b0) = [',rDr,'  ',rDth,'  ',rDph,']'\n",
      "print 'The component of D tangential to the spherical surface r = 10 at P ='\n",
      "print '[',Dtr,'  ',Dtth,'  ',Dtph,']'\n",
      "print 'A unit vector at P perpendicular to D and tangential to cone 0 = 150\u00b0 ='\n",
      "print ru"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "D at P(10,150\u00b0,330\u00b0) = [ -5.0    0.043    100.0 ]\n",
        "The component of D tangential to the spherical surface r = 10 at P =\n",
        "[ 0.0    0.043    100.0 ]\n",
        "A unit vector at P perpendicular to D and tangential to cone 0 = 150\u00b0 =\n",
        "[-0.9988  0.     -0.0499]\n"
       ]
      }
     ],
     "prompt_number": 10
    }
   ],
   "metadata": {}
  }
 ]
}