Introduction
In this lab, you will learn how to use Matplotlib to create a gradient plot of an electrical dipole. You will learn how to create a triangulation, refine data, and compute the electrical field. Finally, you will plot the triangulation, the potential iso-contours, and the vector field.
VM Tips
After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.
Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.
If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.
Create the x and y coordinates of the points
n_angles = 30
n_radii = 10
min_radius = 0.2
radii = np.linspace(min_radius, 0.95, n_radii)
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += np.pi / n_angles
x = (radii*np.cos(angles)).flatten()
y = (radii*np.sin(angles)).flatten()
Explanation:
n_anglesis the number of angles in a circle.n_radiiis the number of circles.min_radiusis the minimum radius of the circles.radiiis an array of radii.anglesis an array of angles.xis an array of x-coordinates.yis an array of y-coordinates.
Compute the electrical potential of a dipole
def dipole_potential(x, y):
"""The electric dipole potential V, at position *x*, *y*."""
r_sq = x**2 + y**2
theta = np.arctan2(y, x)
z = np.cos(theta)/r_sq
return (np.max(z) - z) / (np.max(z) - np.min(z))
V = dipole_potential(x, y)
Explanation:
dipole_potentialis a function that calculates the electrical dipole potential.Vis an array of electrical dipole potentials.
Create the Triangulation
triang = Triangulation(x, y)
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
y[triang.triangles].mean(axis=1))
< min_radius)
Explanation:
Triangulationis a class that creates a Delaunay triangulation from a set of points.triangis an instance of theTriangulationclass.triang.set_maskmasks off unwanted triangles.
Refine data
refiner = UniformTriRefiner(triang)
tri_refi, z_test_refi = refiner.refine_field(V, subdiv=3)
Explanation:
UniformTriRefineris a class that refines a triangulation to create a more accurate plot.refineris an instance of theUniformTriRefinerclass.tri_refiandz_test_refiare refined triangulation and potential values, respectively.
Compute the electrical field
tci = CubicTriInterpolator(triang, -V)
(Ex, Ey) = tci.gradient(triang.x, triang.y)
E_norm = np.sqrt(Ex**2 + Ey**2)
Explanation:
CubicTriInterpolatoris a class that interpolates data using a cubic polynomial.tciis an instance of theCubicTriInterpolatorclass.(Ex, Ey)is the electrical field.E_normis the normalized electrical field.
Plot the triangulation, the potential iso-contours, and the vector field
fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.use_sticky_edges = False
ax.margins(0.07)
ax.triplot(triang, color='0.8')
levels = np.arange(0., 1., 0.01)
ax.tricontour(tri_refi, z_test_refi, levels=levels, cmap='hot',
linewidths=[2.0, 1.0, 1.0, 1.0])
ax.quiver(triang.x, triang.y, Ex/E_norm, Ey/E_norm,
units='xy', scale=10., zorder=3, color='blue',
width=0.007, headwidth=3., headlength=4.)
ax.set_title('Gradient Plot: Electrical Dipole')
plt.show()
Explanation:
figandaxare the figure and axes objects, respectively.ax.set_aspectsets the aspect ratio of the axes.ax.use_sticky_edgesandax.marginsset the margins of the axes.ax.triplotplots the triangulation.ax.tricontourplots the potential iso-contours.ax.quiverplots the vector field.ax.set_titlesets the title of the plot.
Summary
In this lab, you learned how to use Matplotlib to create a gradient plot of an electrical dipole. You learned how to create a triangulation, refine data, and compute the electrical field. Finally, you plotted the triangulation, the potential iso-contours, and the vector field.