Example Problem
We will calculate and visualize the electric field generated by a uniform line charge along the $ x $-axis.
Problem Description
Uniform Line Charge:
- A line charge with linear charge density $( \lambda )$ (charge per unit length) extends from $( x = -L )$ to $( x = +L )$.
- The total charge is $( Q = 2L \lambda )$.
Electric Field Calculation:
- The electric field at a point $ P $ $(0, y )$ is:
$$
\vec{E} = \frac{1}{4 \pi \epsilon_0} \int_{-L}^{L} \frac{\lambda , dx}{r^2} \hat{r}
$$ - Here, $ r = \sqrt{x^2 + y^2} $ and $ \hat{r} $ is the unit vector.
- The electric field at a point $ P $ $(0, y )$ is:
Python Code Implementation
Here’s the $Python$ code to compute and visualize the electric field.
1 | import numpy as np |
Code Explanation
Grid and Initialization:
- A $2D$ grid of points is created where the electric field will be calculated.
- The field components $( E_x )$ and $( E_y )$ are initialized to zero.
Numerical Integration:
- The line charge is divided into small segments $( dx )$.
- For each segment, the contribution to the field at every grid point is calculated using Coulomb’s law:
$$
d\vec{E} = \frac{\lambda , dx}{4 \pi \epsilon_0 r^2} \hat{r}
$$ - The total electric field is obtained by summing the contributions.
Vector Normalization:
- The field vectors are normalized for plotting, and their magnitude is used as the color map.
Plot:
- The
quiverplot shows the direction and relative magnitude of the electric field at various points. - The line charge is displayed in blue along the $ x $-axis.
- The
Results Visualization

Electric Field Pattern:
- The electric field vectors point away from the line charge for a positive charge density $( \lambda > 0 )$.
- The field strength decreases as the distance from the line charge increases.
Color Mapping:
- The color represents the magnitude of the electric field, with warmer colors indicating stronger fields.
This example demonstrates how numerical integration can be used to compute and visualize complex electric fields.










