Computing Electric Field Along the Axis of a Charged Ring With a Computer

This program computes the net electric field due to a uniformly charged ring of radius $R$ and charge $Q$ at a given point in space.


In [4]:
from __future__ import division, print_function
from vpython import *
from math import *

In [5]:
scene=canvas(title="Electric field due to uniformly charged ring")
scene.background=color.white

R=0.02 #radius of ring in m
Q=1e-9 #charge of ring in C
N=4 #number of unique pieces

#draw the objects
myring=ring(pos=vector(0,0,0), radius=R, axis=vector(0,0,1), color=color.blue, thickness=0.02*R)
zaxis=cylinder(pos=-2*R*myring.axis, radius=0.015*R, axis=4*R*myring.axis, color=color.black)
point=sphere(pos=R*myring.axis, color=color.red, radius=5*zaxis.radius)

oofpez=9e9 #1/(4pi epsilon_0) in N m^2/C^2
dq=Q/N #charge of a piece
dtheta=2*pi/N #theta increment for our loop
theta=dtheta/2 #initial theta for first piece of loop
Enet=vector(0,0,0) #net electric field of all pieces

rpoint=point.pos #location of the point in space to calculate E field

scale=1.2*mag(rpoint)/8000 #used to scale the arrows representing E-field

while theta<2*pi:
    rpiece=R*vector(cos(theta),sin(theta),0) #location of piece
    r=rpoint-rpiece #vector from piece to point in space
    rmag=mag(r) #magnitude of r
    rhat=norm(r) #unit vector for r
    dE=oofpez*dq/rmag/rmag*rhat #Electric field due to piece at rpoint
    Enet=Enet+dE #net electric field of the first one up to this one
    particle=sphere(pos=rpiece, radius=point.radius, color=color.yellow) #draw a particle at center of piece
    dEvector=arrow(pos=rpoint, axis=scale*dE, color=color.magenta, shaftwidth=point.radius/2)
    theta=theta+dtheta

print("The net electric field = ",Enet, "N/C")
Evector=arrow(pos=rpoint, axis=scale*Enet, color=color.orange, shaftwidth=point.radius/2)


The net electric field =  <0.000000, -0.000000, 7954.951288> N/C

In [ ]: