# Chapter 2: Coordinate Systems and Transformation

### Example 2.1, Page number: 36

In [1]:

import scipy
from numpy import *

#Variable Declaration

x=-2
y=6
z=3

#Calculations

r=scipy.sqrt(x**2+y**2)
phid=180-(phi*180/scipy.pi)     #phi in degrees in second quadrant
R=scipy.sqrt(x**2+y**2+z**2)
theta=scipy.arctan(r/z)

#P in cylindrical coordinates

Pcyl=array([round(r,2),round(phid,2),z])

#P in spherical coordinates

Psph=array([round(R,2),round(theta*180/scipy.pi,2),
round(phid,2)])

#Vector A in cylindrical coordinate system

Xc=r*scipy.cos(phic)
Yc=r*scipy.sin(phic)
Zc=z
Ar=Yc*scipy.cos(phic)+(Xc+Zc)*scipy.sin(phic)
Aphi=-Yc*scipy.sin(phic)+(Xc+Zc)*scipy.cos(phic)
Az=0
Acyl=array([round(Ar,4),round(Aphi,3),Az])

#Vector A in spherical coordinate system

Xs=R*scipy.cos(phic)*scipy.sin(theta)
Ys=R*scipy.sin(phic)*scipy.sin(theta)
Zs=R*scipy.cos(theta)
AR=Ys*scipy.sin(theta)*scipy.cos(phic)+(Xs+Zs)*scipy.sin(theta)*scipy.sin(phic)
Ath=Ys*scipy.cos(theta)*scipy.cos(phic)+(Xs+Zs)*scipy.cos(theta)*scipy.sin(phic)
Aph=-Ys*scipy.sin(phic)+(Xs+Zs)*scipy.cos(phic)
Asph=array([round(AR,4),round(Ath,4),round(Aph,3)])

#Results

print 'P in cylindrical coordinates =',Pcyl
print 'P in spherical coordinates =',Psph
print 'A in cylindrical coordinates =',Acyl
print 'A in spherical coordinates =',Asph

P in cylindrical coordinates = [   6.32  108.43    3.  ]
P in spherical coordinates = [   7.     64.62  108.43]
A in cylindrical coordinates = [-0.9487 -6.008   0.    ]
A in spherical coordinates = [-0.8571 -0.4066 -6.008 ]


### Example 2.2, Page number: 39

In [2]:

import scipy
from numpy import *

#Variable Declaration

x=-3
y=4
z=0
p=5
phi=scipy.pi/2
Zc=-2

#Calculations

#B in cartesian coordinates

R=scipy.sqrt(x**2+y**2+z**2)
r=scipy.sqrt(x**2+y**2)
f=10/R
Bx=f*scipy.sin(P)*scipy.cos(Q)+R*(scipy.cos(P))**2*scipy.cos(Q)-scipy.sin(Q)
By=f*scipy.sin(P)*scipy.sin(Q)+R*(scipy.cos(P))**2*scipy.sin(Q)+scipy.cos(Q)
Bz=f*scipy.cos(P)-R*scipy.cos(P)*scipy.sin(P)
Bcart=array([round(Bx,0),round(By,0),round(-Bz,0)])

#B in cylindrical coordinates

Rc=sqrt(p**2+Zc**2)
Br=(10/Rc)*scipy.sin(Pc)+Rc*(scipy.cos(Pc))**2
Bp=1
Bzc=(10/Rc)*scipy.cos(Pc)-Rc*scipy.cos(Pc)*scipy.sin(Pc)
Bcyl=array([round(Br,3),Bp,round(Bzc,3)])

#Results

print 'B(-3,4,0) in cartesian coordinates is',Bcart
print 'B(5,pi/2,-2) in cylindrical coordinates is',Bcyl

B(-3,4,0) in cartesian coordinates is [-2.  1.  0.]
B(5,pi/2,-2) in cylindrical coordinates is [ 2.467  1.     1.167]


### Example 2.3, Page number: 44

In [3]:

import scipy
from numpy import *

#Variable Declaration

E=array([-5,10,3])              #in cylindrical coordinates
F=array([1,2,-6])               #in cylindrical coordinates
P=array([5,scipy.pi/2,3])       #in cylindrical coordinates

#Calculations

exf=cross(E,F)
ansa=scipy.sqrt(dot(exf,exf))   #|EXF|
ay=array([round(scipy.sin(scipy.pi/2),0),
round(scipy.cos(scipy.pi/2),0),0])
ansb=dot(E,ay)*ay
modE=scipy.sqrt(dot(E,E))
az=array([0,0,1])
thetaEz=(180/scipy.pi)*arccos(dot(E,az)/modE)   #in degrees
ansc=90-thetaEz                                 #in degrees

#Results

print '|EXF| =',round(ansa,2)
print 'The vector component of E at P parallel to the line x=2,z=3 =',ansb,','
print 'in spherical coordinates'
print 'The angle E makes with the surface z = 3 at P =',round(ansc,2),'degrees'

|EXF| = 74.06
The vector component of E at P parallel to the line x=2,z=3 = [-5. -0. -0.] ,
in spherical coordinates
The angle E makes with the surface z = 3 at P = 15.02 degrees


### Example 2.4, Page number: 45

In [10]:

import scipy
from numpy import *

#Variable Declaration

aR=array([1,0,0])              #Unit vector along radial direction
ath=array([0,1,0])             #Unit vector along theta direction
aph=array([0,0,1])             #Unit vector along phi direction
P=array([10,scipy.pi*150/180,scipy.pi*330/180])

#Calculations

r=dot(P,aR)
q=dot(P,aph)
p=dot(P,ath)
R=r*scipy.sin(q)
Ph=-scipy.sin(p)*scipy.cos(q)/r
Q=r*r
D=array([R,Ph,Q])               #D at P(10,150°,330°)
rDth=round(dot(-ath,D),3)       #theta component of D
rDph=round(dot(aph,D),0)        #phi component of D

Dn=array([r*scipy.sin(q),0,0])  #Component of D normal to surface r=10
Dt=D-Dn                         #Component of D tangential to surface r=10
Dtth=round(dot(-ath,Dt),3)      #theta component of Dt
Dtph=round(dot(aph,Dt),0)       #phi component of Dt
rDt=array([Dtr,Dtth,Dtph])

#Unit vector normal to D and tangential to cone theta=45 degrees

U=cross(D,ath)
u=U/scipy.sqrt(dot(U,U))
ru=array([round(dot(aR,u),4),round(dot(ath,u),4),round(dot(aph,u),4)])

#Results

print 'D at P(10,150°,330°) = [',rDr,'  ',rDth,'  ',rDph,']'
print 'The component of D tangential to the spherical surface r = 10 at P ='
print '[',Dtr,'  ',Dtth,'  ',Dtph,']'
print 'A unit vector at P perpendicular to D and tangential to cone 0 = 150° ='
print ru

D at P(10,150°,330°) = [ -5.0    0.043    100.0 ]
The component of D tangential to the spherical surface r = 10 at P =
[ 0.0    0.043    100.0 ]
A unit vector at P perpendicular to D and tangential to cone 0 = 150° =
[-0.9988  0.     -0.0499]