I am creating a program for the electric field of a charged rod. I am unfamiliar
ID: 3792390 • Letter: I
Question
I am creating a program for the electric field of a charged rod. I am unfamiliar with Vpython and need help in figuring out what is wrong with my code because it is not running. (It doesn't show an error after running it, it just loads forever and nothing happenes) I believe the error is in my loop. All my constants and everything else should be correct. The code is copied and pasted below. (Everything under the loop is indented, it may not show up here).
from visual import*
##constants
oofpez=9e9
L=4from visual import*
##constants
oofpez=9e9
L=4 #length
q=5e-8 #charge
Nq=30 #number of point charges used to model a uniformly charged rod
dxq=L/Nq
dq=q/Nq
xq= -L/2 + dxq/2
scalefactor=abs((L/2)/(oofpez*64*q/(L**2)))
r=2e-9
e=1.6e-19
xobs= -.1 * L/2
yobs = L/4
obslocation = vector (-1.2, .3, 0) # observation location
Enet=vector(0,0,0)
##objects
rod=cylinder(pos=(-L/2,0,0), axis=(L,0,0), radius=0.02, opacity =0.2)
##display Nq spheres
while xq < L/2:
sphere(pos=(xq,0,0), radius=.01, color=color.red)
sourcelocation = vector(xq,0,0)
r1=obslocation-sourcelocation #electric field
r1mag=r1.mag #electric field
r1hat=r1/r1mag #electric field
E1=(oofpez*dq/r1mag**2)*r1hat #electric field
Enet=Enet+E1
E1arrow = arrow(pos=obslocation, axis=Enet*scalefactor, color=color.orange)
xq=xq + dxq
print Enet
Explanation / Answer
##constants
oofpez=9e9
L=4from visual import*
##constants
oofpez=9e9
L=4 #length
q=5e-8 #charge
Nq=30 #number of point charges used to model a uniformly charged rod
dxq=L/Nq
dq=q/Nq
xq= -L/2 + dxq/2
scalefactor=abs((L/2)/(oofpez*64*q/(L**2)))
r=2e-9
e=1.6e-19
xobs= -.1 * L/2
yobs = L/4
obslocation = vector (-1.2, .3, 0) # observation location
Enet=vector(0,0,0)
##objects
rod=cylinder(pos=(-L/2,0,0), axis=(L,0,0), radius=0.02, opacity =0.2)
##display Nq spheres
while xq < L/2:
sphere(pos=(xq,0,0), radius=.01, color=color.red)
sourcelocation = vector(xq,0,0)
r1=obslocation-sourcelocation #electric field
r1mag=r1.mag #electric field
r1hat=r1/r1mag #electric field
E1=(oofpez*dq/r1mag**2)*r1hat #electric field
Enet=Enet+E1
E1arrow = arrow(pos=obslocation, axis=Enet*scalefactor, color=color.orange)
xq=xq + dxq
print Enet
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.