Free Coding Seminar

Electric Field of a Point Charge

This time, the exercises are a bit more complicated and perhaps only for the more advanced. You may, however, do some training with the easier repetitions before and come back and test yourself. We will focus on creating visualizations of vector fields using meshgrids and quiver plots. Vector fields are used to represent the spatial distribution of physical quantities like electric fields, fluid velocities, or magnetic fields. By visualizing vector fields, we can gain insights into the behavior of these quantities in different regions of space.

Note that matplotlib and numpy are already imported and may be used with plt and np respectively.

Self-Exercise 1: Electric Field of a Point Charge

Create a 2D vector field plot of the electric field from a point charge located at the origin. The electric field vectors should point radially outward from the charge, with magnitude proportional to 1/r². This visualization helps understand the spatial distribution of electric fields.

The electric field is given by: \(\vec{E}(\vec{r}) = \frac{q}{4\pi\epsilon_0} \frac{\vec{r}}{r^3}\)

where:

  • \(\vec{E}\) is the electric field vector
  • \(q\) is the charge
  • \(\epsilon_0\) is the permittivity of free space
  • \(\vec{r}\) is the position vector
  • \(r\) is the distance from the charge

Look up the plt.quiver() function in the Matplotlib documentation for plotting vector fields or check out the hint.

  1. Use np.meshgrid(x, y) to create X, Y grids
  2. Calculate R = sqrt(X² + Y²) for distance
  3. Calculate Ex = X/R³ and Ey = Y/R³
  4. Use plt.quiver(X, Y, Ex, Ey) for the vector field
  5. Remember to avoid division by zero at the origin
Solution

Standing Wave Pattern

Self-Exercise 2: Standing Wave Pattern

Visualize a 2D standing wave pattern that might occur in a square membrane (like a drum head). This type of visualization is useful in understanding wave modes in musical instruments or quantum mechanical systems.

The wave function is given by: \(\psi(x,y,t) = \sin(mx)\cos(ny)\)

where: - \(m, n\) are mode numbers (integers) - \(x, y\) are positions on the membrane - \(\psi\) is the displacement amplitude

  1. Use np.meshgrid(x, y) to create X, Y grids
  2. Try different mode numbers (m, n) for different patterns
  3. Use plt.contourf() for filled contours
  4. Add a colorbar to show amplitude scale
  5. Look up the matplotlib plotting of surfaces with ax = plt.subplot(122, projection='3d') surf = ax.plot_surface()
Solution

Quantum Wave Packet

Self-Exercise 3: Quantum Wave Packet

Create a 3D visualization of a Gaussian wave packet, which represents a localized quantum particle. This type of visualization is fundamental in understanding quantum mechanical states and probability distributions.

The wave function is given by: \(\psi(x,y) = A\exp\left(-\frac{(x-x_0)^2 + (y-y_0)^2}{2\sigma^2}\right)\)

where: - \(A\) is the amplitude - \((x_0, y_0)\) is the center position - \(\sigma\) is the width of the packet

  1. Use np.meshgrid(x, y) to create X, Y grids
  2. Calculate ψ using the Gaussian formula
  3. Create both a 3D surface plot and a 2D probability density plot
  4. Use as the probability density is |ψ|²
  5. Look up the matplotlib plotting of surfaces with ax = plt.subplot(133, projection='3d') surf = ax.plot_surface(X, Y, prob_density, cmap='viridis')
Solution