** Visualize the vector field using matplotlib. As an example, we plot the electrostatic and static magnetic fields learned in high school physics. ** **
Every program has a flow
That is.
It will be easier to understand if you check the operation while looking at a concrete example.
(1) Visualization of the electrostatic field created by one point charge (2) Electrostatic field created by two point charges (electric dipole) (3) Static magnetic field created by steady-state current passing through the origin (4) Three-dimensional vector field (electrostatic field created by point charge at origin)
"""
(1)Electrostatic field created by one point charge at the origin
"""
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
LX, LY=2.1,2.1 #Parameters for mesh
gridwidth=0.33
X, Y= np.meshgrid(np.arange(-LX, LX, gridwidth), np.arange(-LY, LY,gridwidth)) #Mesh generation
R = np.sqrt(X**2+Y**2) #Distance from the origin
#Position coordinates of point charge and charge
X1,Y1=0,0 #Placement of charge to the origin
Q1=1 #Charge amount setting
R1=np.sqrt((X-X1)**2+(Y-Y1)**2) #Arbitrary point from this charge(X,Y)Distance to
plt.plot(X1,Y1,'o',color='blue') #Draw point charge
#Vector function F(U(x,y), V(x,y))Is defined. The expression of the electrostatic field is used.
U = Q1*(X-X1)/(R1**2)
V = Q1*(Y-Y1)/(R1**2)
plt.quiver(X,Y,U,V,color='red',angles='xy',scale_units='xy', scale=5.0) #Plot vector field
plt.xlim([-LX,LX]) #Range of X to draw
plt.ylim([-LY,LY]) #Range of y to draw
#Graph drawing
plt.grid()
plt.draw()
plt.show()
An electrostatic field due to a point charge placed at the origin.
"""
(2)Two point charges(Electric dipole)Electrostatic field created by
"""
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
LX, LY=2,2
gridwidth=0.2
X, Y= np.meshgrid(np.arange(-LX, LX, gridwidth), np.arange(-LY, LY,gridwidth))
R = np.sqrt(X**2+Y**2)
#Position coordinates and charge of two point charges
X1,Y1=1.1,0
Q1=1
R1=np.sqrt((X-X1)**2+(Y-Y1)**2)
plt.plot(X1,Y1,'o',color='blue')
X2,Y2=-1.1,0
Q2=-1
R2=np.sqrt((X-X2)**2+(Y-Y2)**2)
plt.plot(X2,Y2,'o',color='blue')
##
#Vector function settings. 2 charges.
U = Q1*(X-X1)/(R1**2)+Q2*(X-X2)/(R2**2)
V = Q1*(Y-Y1)/(R1**2)+Q2*(Y-Y2)/(R2**2)
plt.quiver(X,Y,U,V,color='red',angles='xy',scale_units='xy', scale=6.5)
plt.xlim([-LX,LX])
plt.ylim([-LY,LY])
#Graph drawing
plt.grid()
plt.draw()
plt.show()
An electrostatic field created by two point charges (charges are 1 and -1 respectively) placed at (-1.1,0) and (1.1,0).
"""
(3)A static magnetic field created by a steady-state current passing through the origin
"""
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
LX, LY=3,3
gridwidth=0.3
X, Y= np.meshgrid(np.arange(-LX, LX, gridwidth), np.arange(-LY, LY,gridwidth))
R = np.sqrt(X**2+Y**2)
#Position coordinates of point charge and charge
X1,Y1=0,0
I=1 #Steady current value
R1=np.sqrt((X-X1)**2+(Y-Y1)**2)
plt.plot(X1,Y1,'o',color='blue')
#Vector function settings. Static magnetic field created by steady current F(U(x,y), V(x,y))
U = I*-1*(Y-Y1)/(R1)
V = I*(X-X1)/(R1)
plt.quiver(X,Y,U,V,color='red',angles='xy',scale_units='xy', scale=4.5)
plt.xlim([-LX,LX])
plt.ylim([-LY,LY])
#Graph drawing
plt.grid()
plt.draw()
plt.show()
A static magnetic field created by a steady-state uniform current I that runs perpendicular to the paper from the origin.
"""
(4)Illustration of 3D vector field
Electrostatic field created by point charge at origin
"""
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D #Import for 3D plots
import numpy as np
fig = plt.figure()
ax = Axes3D(fig)
LX, LY, LZ = 2,2,2 #xyz mesh parameters
gridwidth=0.9 #
X, Y, Z= np.meshgrid(np.arange(-LX, LX, gridwidth), np.arange(-LY, LY,gridwidth),np.arange(-LZ, LZ, gridwidth) ) #Mesh generation
R = np.sqrt(X**2+Y**2+Z**2)
#Position coordinates of point charge and charge
X1,Y1, Z1=0,0,0
Q1=1
R1=np.sqrt((X-X1)**2+(Y-Y1)**2+(Z-Z1)**2)
ax.scatter3D(X1,Y1,Z1,"o", color='blue')
U = Q1*(X-X1)/(R1**2)
V = Q1*(Y-Y1)/(R1**2)
W= Q1*(Z-Z1)/(R1**2)
ax.quiver(X, Y, Z, U, V, W, color='red',length=1, normalize=False)
ax.set_xlim([-LX, LX])
ax.set_ylim([-LY, LY])
ax.set_zlim([-LZ, LZ])
plt.show()
A three-dimensional plot of the electric field created by the point charge placed at the origin.