-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathch7ex4.py
More file actions
53 lines (44 loc) · 1.59 KB
/
ch7ex4.py
File metadata and controls
53 lines (44 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from numpy import arange, meshgrid,sqrt,log
from matplotlib import pyplot
x = arange(-5.25,5.25,0.5) # define the x and y grids (avoid (0,0))
y = arange(-5.25,5.25,0.5) # define the x and y grids (avoid (0,0))
X,Y=meshgrid(x,y)
# Electric field of a long charged wire
Ex=X/(X**2+Y**2)
Ey=Y/(X**2+Y**2)
pyplot.quiver(X,Y,Ex,Ey) # make the field arrow plot
pyplot.title('E of a long charged wire')
pyplot.axis('equal') # make the x and y axes be equally scaled
# Magnetic field of a long current-carrying wire
Bx=-Y/(X**2+Y**2)
By=X/(X**2+Y**2)
# make the field arrow plot
pyplot.figure()
pyplot.quiver(X,Y,Bx,By)
pyplot.axis('equal') # make the x and y axes be equally scaled
pyplot.title('B of a long current-carrying wire')
# The big magnitude difference across the region makes most arrows too small
# to see. This can be fixed by plotting unit vectors instead
# (losing all magnitude information, but keeping direction)
B=sqrt(Bx**2+By**2)
Ux=Bx/B
Uy=By/B
pyplot.figure()
pyplot.quiver(X,Y,Ux,Uy);
pyplot.axis('equal') # make the x and y axes be equally scaled
pyplot.title('B(wire): unit vectors')
# Or, you can still see qualitative size information without such a big
# variation in arrow size by having the arrow length be logarithmic.
Bmin=B.min()
Bmax=B.max()
# s is the desired ratio between the longest arrow and the shortest one
s=2 # choose an arrow length ratio
k=(Bmax/Bmin)**(1/(s-1))
logsize=log(k*B/Bmin)
Lx=Ux*logsize
Ly=Uy*logsize
pyplot.figure()
pyplot.quiver(X,Y,Lx,Ly)
pyplot.axis('equal') # make the x and y axes be equally scaled
pyplot.title('B(wire): logarithmic arrows')
pyplot.show()