Electromagnetics 03, Python Coding
Chapter title: Coordinate Systems
1. Example 1
1) ¹®Á¦
Given the coordinate (x, y,
z) in the rectangular coordinate of a
point P,
a) find the coordinate (r, ¥è, ¥õ)
in the spherical coordinate of the point P, express ¥è and ¥õ in degree unit
and
b) express base vectors at P in terms of base vectors
.
2) ¼ö½Ä
a)
To convert ¥è and ¥õ expressed in radian, multiply them with 180/¥ð.
b)
3) ÄÚµù
#
EM-03-Python-Ex1
#
Given a point P(x,y,z),
#
1) express P in the spherical coordinate
#
2) express base vectors <r>,<theta>,<phi> in terms of base
vectors <x>,<y>,<z>
from
math import *
pi=3.141593
while
True:
x,y,z=map(float,
input('x,y,z=').split())
r=sqrt(x**2+y**2+z**2)
theta=acos(z/r)
rho=sqrt(x**2+y**2)
phi=acos(x/rho)
if y < 0:
phi=pi+phi
print('(r,theta,phi)=(',r,',',theta*180/pi,',',phi*180/pi,')')
rx=sin(theta)*cos(phi)
ry=sin(theta)*sin(phi)
rz=cos(theta)
thetax=cos(theta)*cos(phi)
thetay=cos(theta)*sin(phi)
thetaz=-sin(theta)
phix=-sin(phi)
phiy=cos(phi)
phiz=0
print('<r> =(',rx,',',ry,',',rz,')')
print('<theta>=(',thetax,',',thetay,',',thetaz,')')
print('<phi>
=(',phix,',',phiy,',',phiz,')')
4) ÄÚµå
¼öÇà°á°ú
x,y,z=
1
1 1
(r,theta,phi)=(
1.7320508075688772 , 54.73560428178051 , 44.99999503803985 )
<r> =( 0.5773502691896257 ,
0.5773502691896258 , 0.5773502691896258 )
<theta>=(
0.408248290463863 , 0.4082482904638631 , -0.816496580927726 )
<phi> =( -0.7071067811865476 ,
0.7071067811865475 , 0 )
x,y,z=
1
1 -1
(r,theta,phi)=(
1.7320508075688772 , 125.26437587037888 , 44.99999503803985 )
<r> =( 0.5773502691896255 ,
0.5773502691896256 , -0.5773502691896261 )
<theta>=(
-0.4082482904638632 , -0.40824829046386324 , -0.8164965809277258 )
<phi> =( -0.7071067811865476 ,
0.7071067811865475 , 0 )
x,y,z=
1
-1 1
(r,theta,phi)=(
1.7320508075688772 , 54.73560428178051 , 224.99999503803986 )
<r> =( -0.5773500691895651 ,
-0.5773504691896172 , 0.5773502691896258 )
<theta>=(
-0.4082481490424639 , -0.4082484318852132 , -0.816496580927726 )
<phi> =( 0.7071070261355112 ,
-0.707106536237499 , 0 )
x,y,z=
-1
1 1
(r,theta,phi)=(
1.7320508075688772 , 54.73560428178051 , 134.9999851141195 )
<r> =( -0.5773502691896257 ,
0.5773502691896258 , 0.5773502691896258 )
<theta>=(
-0.408248290463863 , 0.4082482904638631 , -0.816496580927726 )
<phi> =( -0.7071067811865476 ,
-0.7071067811865475 , 0 )
x,y,z=
-1
-1 1
(r,theta,phi)=(
1.7320508075688772 , 54.73560428178051 , 314.9999851141195 )
<r> =( 0.5773504691896169 ,
-0.5773500691895654 , 0.5773502691896258 )
<theta>=(
0.40824843188521304 , -0.40824814904246415 , -0.816496580927726 )
<phi> =( 0.7071065362374993 ,
0.7071070261355109 , 0 )
x,y,z=
-1
-1 -1
(r,theta,phi)=(
1.7320508075688772 , 125.26437587037888 , 314.9999851141195 )
<r> =( 0.5773504691896167 ,
-0.5773500691895652 , -0.5773502691896261 )
<theta>=(
-0.40824843188521315 , 0.4082481490424643 , -0.8164965809277258 )
<phi> =( 0.7071065362374993 ,
0.7071070261355109 , 0 )
x,y,z=**
Process Stopped **
Press Enter to exit terminal
2. Example 2
1) ¹®Á¦
¹Ý°æ aÀÎ ±¸¸é
»óÀÇ µÎÁ¡ P = (a, ¥è1, ¥õ1) and Q = (a, ¥è2, ¥õ2)ÀÌ
ÁÖ¾îÁú °æ¿ì ±¸¸é
»ó¿¡¼ P¿¡¼ Q ·Î
À̵¿ÇÒ ¶§ ÃÖ¼Ò°æ·Î
±æÀÌ L¸¦ ±¸Ç϶ó. Theta¿Í phi °¢Àº degree ´ÜÀ§·Î ÀÔ·ÂÇÑ´Ù.
(Âü°í) LÀ» great-circle distance, orthodromic
distance, spherical distance¶ó°í
ºÎ¸¥´Ù.
https://en.wikipedia.org/wiki/Great-circle_distance
2) ¼ö½Ä
L °ø½Ä:
L = a¥á
R1 : P1ÀÇ
À§Ä¡º¤ÅÍ
R2 : P2ÀÇ
À§Ä¡º¤ÅÍ
¥á : R1, R2
»çÀÌÀÇ °¢µµ
R1, R2¸¦
Á÷°¢ÁÂÇ¥°è·Î Ç¥Çö
R1 = (x1, y1,
z1) ; R2 = (x2,
y2, z2)
R1, R2ÀÇ
´ÜÀ§º¤ÅÍ:
R1, R2 »çÀÌÀÇ
°¢µµ ±¸Çϱâ:
3) ÄÚµù
#
EM-03-Python-Ex2
#
Given two points on a sphere, find the great-circle distance between them.
import
from math *
pi=3.141593
a=float(input('Sphere
radius: a='))
while
True:
t1,p1=map(float, input('Point P:
theta(deg), phi(deg)=').split())
t2,p2=map(float, input('Point Q:
theta(deg), phi(deg)=').split())
t1=t1*pi/180 ; p1=p1*pi/180 ;
t2=t2*pi/180 ; p2=p2*pi/180 # degree to radian
x1=sin(t1)*cos(p1) ; y1=sin(t1)*sin(p1)
; z1=cos(t1)
x2=sin(t2)*cos(p2) ; y2=sin(t2)*sin(p2)
; z2=cos(t2)
r1_dot_r2=x1*x2+y1*y2+z1*z2
alpha=acos(r1_dot_r2)
print('Great-circle distance from P to Q
=',a*alpha)
4) ÄÚµå
¼öÇà°á°ú
Sphere
radius: a=
1
Point
P: theta, phi=
0
0
Point
Q: theta, phi=
90
0
Great-circle
distance from P to Q = 1.5707965
Point
P: theta, phi=
0
0
Point
Q: theta, phi=
90
90
Great-circle
distance from P to Q = 1.5707965
Point
P: theta, phi=
45
45
Point
Q: theta, phi=
90
90
Great-circle
distance from P to Q = 1.0471976926179716
Point
P: theta, phi=** Process Stopped **
Press Enter to exit terminal
3. Example 3
1) ¹®Á¦
¹Ý°æ aÀÎ ¿øÅë¸é
»óÀÇ µÎÁ¡ A = (a, ¥õ1, z1) and B = (a, ¥õ2, z2)ÀÌ
ÁÖ¾îÁú °æ¿ì ¿øÅë¸é
»ó¿¡¼ A¿¡¼ B·Î À̵¿ÇÒ
¶§ ÃÖ¼Ò°æ·Î ±æÀÌ
S¸¦
±¸Ç϶ó. ¥õ1°ú ¥õ2´Â µµ(degree) ´ÜÀ§·Î ÀԷµȴÙ.
2) ¼ö½Ä
¥õ1°ú ¥õ2¸¦ radianÀ¸·Î º¯È¯
¥õ1 (rad) = ¥õ1 (deg) ¡¿ ¥ð / 180
Á¡ A¿Í Á¡
BÀÇ
¥õ °¢µµ Â÷ÀÌ:
µÎ Á¡
»çÀÌÀÇ °î¼± ÃּҰŸ®:
3) ÄÚµù
#
EM-03-Python-Ex3
#
Given two points on a circular cylinder, find the surface distance between
them.
from
math import *
pi=3.141593
a=float(input('Cylinder
radius: a='))
while
True:
p1,z1=map(float, input('Point A:
phi(deg), z=').split())
p2,z2=map(float, input('Point B:
phi(deg), z=').split())
p1=p1*pi/180 ; p2=p2*pi/180
dp=abs(p1-p2)
if dp > pi:
dp=2*pi-dp
s=sqrt((z1-z2)**2+(a*dp)**2)
print('Surface distance between A and B = ',s)
4) ÄÚµå
¼öÇà°á°ú
Cylinder
radius: a=
1
Point
A: phi(deg), z=
45
1
Point
B: phi(deg), z=
90
2
Surface
distance between A and B =
1.2715543288051292
Point
A: phi(deg), z=** Process Stopped **
Press Enter to exit terminal
4. Example 4
1) ¹®Á¦
Æò¸é»óÀÇ xy ÁÂÇ¥ ±âÁØÀ¸·Î
Á¡ P = (x, y)°¡
ÁÖ¾îÁ³´Ù. xy ÃàÀ» ¹Ý½Ã°è¹æÇâÀ¸·Î ¥è ¸¸Å
ȸÀü½ÃŲ »õ·Î¿î ÁÂÇ¥ÃàÀ»
x'y' ÁÂÇ¥ÃàÀ̶ó ÇÑ´Ù. Á¡ PÀÇ ÁÂÇ¥¸¦
x'y' ÁÂÇ¥·Î Ç¥½ÃÇ϶ó. ¥è´Â µµ(degree) ´ÜÀ§·Î ÀԷµȴÙ.
2)
¼ö½Ä
3) ÄÚµå
#
EM-03-Python-Ex4
#
A point P(x, y) is given. X'Y' axis is the XY axis rotated CCW by theta.
Express the coordinate of P in
#
the new X'Y' coordinate system.
from
math import *
pi=3.141593
while
True:
x,y=map(float, input('Point P: x,
y=').split())
theta=float(input('XY axis CCW rotation
angle: theta(deg)='))
theta=theta*pi/180
xp=x*cos(theta)+y*sin(theta);
yp=-x*sin(theta)+y*cos(theta)
print('Point P: xp, yp =',xp,yp)
4) ÄÚµå
¼öÇà°á°ú
Point
P: x, y=
1
2
XY
axis CCW rotation angle: theta(deg)=
45
Point
P: xp, yp = 2.121320404796886 0.7071065974747902
Point
P: x, y=
1
0
XY
axis CCW rotation angle: theta(deg)=
45
Point
P: xp, yp = 0.7071067199492933 -0.7071068424237964
Point
P: x, y=** Process Stopped **
Press Enter to exit terminal