Electromagnetic Wave Propagation with Python, Electromagnetic (EM) waves are essential in physics, engineering, and modern technology. They consist of oscillating electric and magnetic fields that propagate through space, carrying energy and information.
This article focuses on using Python to simulate EM wave propagation, with an emphasis on the fundamentals, coding examples, and a 2D visualization.
Electromagnetic waves are solutions to Maxwell’s equations, which govern the behavior of electric and magnetic fields. For simplicity, we can use the 2D wave equation to simulate EM wave propagation in free space. The general form of the wave equation is:

where:
- uuu represents the electric or magnetic field,
- ccc is the speed of light in free space,
- ∇2\nabla^2∇2 is the Laplacian operator, indicating spatial variation.
For simplicity, let’s model only the electric field (E-field) in 2D space. This simulation assumes a monochromatic wave (single frequency) in free space.
Setting Up the Python Environment
Electromagnetic Wave Propagation with Python We’ll use libraries like NumPy and Matplotlib to handle numerical computation and visualization.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
Discretizing the Wave Equation
To simulate wave propagation, we need to convert the continuous wave equation into a discrete form. One approach is to use the Finite Difference Method (FDM), which approximates derivatives with differences between adjacent grid points.
Let:
- E[i,j,n]E[i, j, n]E[i,j,n] be the electric field at position (i,j)(i, j)(i,j) and time nnn,
- dxdxdx and dydydy represent spatial grid spacing,
- dtdtdt is the time step.
The discretized form of the wave equation is:

Implementing the Simulation Code
Let’s set up a 2D grid, initialize the wave, and apply boundary conditions to simulate wave propagation.
# Constants
c = 3e8 # Speed of light in m/s
dx = 1e-2 # Spatial resolution (10mm)
dy = dx # Same spatial resolution in both dimensions
dt = dx / (2 * c) # Time step for stability (Courant condition)
# Grid dimensions
nx, ny = 100, 100 # 100x100 grid
nt = 200 # Number of time steps
# Field initialization
E = np.zeros((nx, ny, 3)) # 3rd dimension for current, previous, and next timesteps
# Initial disturbance (Gaussian pulse)
x_center, y_center = nx // 2, ny // 2
E[x_center, y_center, 0] = 1
# Simulation loop
for n in range(1, nt - 1):
# Apply finite difference method
E[1:-1, 1:-1, 2] = (2 * E[1:-1, 1:-1, 1] - E[1:-1, 1:-1, 0] +
(c**2 * dt**2 / dx**2) *
(E[2:, 1:-1, 1] + E[:-2, 1:-1, 1] +
E[1:-1, 2:, 1] + E[1:-1, :-2, 1] -
4 * E[1:-1, 1:-1, 1]))
# Shift the timesteps
E[:, :, 0] = E[:, :, 1]
E[:, :, 1] = E[:, :, 2]
# Visualization
plt.imshow(E[:, :, 1], cmap='inferno')
plt.title('Electric Field Propagation')
plt.colorbar()
plt.show()
This code initializes a Gaussian pulse at the center of the grid, then updates the field at each grid point over time, visualizing the propagation as a color map.
Output:

Visualizing the Propagation in Real Time
We can animate the propagation of the electric field to observe how it evolves over time.
# Animation setup
fig, ax = plt.subplots()
im = ax.imshow(E[:, :, 1], cmap='inferno', animated=True)
plt.colorbar(im, ax=ax)
def update(n):
global E
# Update the electric field with finite difference calculations
E[1:-1, 1:-1, 2] = (2 * E[1:-1, 1:-1, 1] - E[1:-1, 1:-1, 0] +
(c**2 * dt**2 / dx**2) *
(E[2:, 1:-1, 1] + E[:-2, 1:-1, 1] +
E[1:-1, 2:, 1] + E[1:-1, :-2, 1] -
4 * E[1:-1, 1:-1, 1]))
# Shift timesteps
E[:, :, 0] = E[:, :, 1]
E[:, :, 1] = E[:, :, 2]
# Update plot data
im.set_array(E[:, :, 1])
return [im]
ani = animation.FuncAnimation(fig, update, frames=nt, blit=True)
plt.show()
This script creates an animated plot that shows the propagation of the electric field as it spreads out from the initial point of disturbance, providing a clearer visualization of wave dynamics.
Output:

Discussion of the Results
The propagation of the electric field in this 2D grid captures the nature of EM waves in free space. The wave initially spreads out from the central source (Gaussian pulse), showing the characteristic radial propagation pattern of EM waves. This pattern is consistent with real-world wave behavior, where each point in the wavefront acts as a secondary source, leading to a circular or spherical wave propagation in 2D and 3D spaces, respectively.
Boundary conditions play a crucial role in such simulations. Here, simple Dirichlet boundary conditions are used, where the field at the grid edges remains zero, simulating free-space propagation without reflection.
This article provided a step-by-step guide to simulating electromagnetic wave propagation using Python. We used the finite difference method to discretize the 2D wave equation, simulating the behavior of an electric field in a confined space. Visualizing the propagation in real time gave insight into wave behavior, helping us understand the dynamics of EM waves.
Python’s simplicity and the power of libraries like NumPy and Matplotlib make it a practical choice for simulating physical phenomena, enabling researchers, students, and enthusiasts to model complex systems like electromagnetic wave propagation with relative ease. Further improvements, such as implementing absorbing boundary conditions, could make the simulation more realistic.
This completes the article on simulating electromagnetic wave propagation with Python. For advanced simulations, consider exploring 3D grids, frequency dispersion, and anisotropic media to broaden the scope of EM simulations further.





Leave a Reply