You are on page 1of 3

GlowScript 2.

7 VPython
GlowScript 2.6 VPython
from visual import *

# - CODE ANNOTATIONS -
# We are interested in your physical interpretation of the code below (except in 'Annotation
9').
# With that in mind, your answers should reflect your understanding of the relevant physics
ideas.
# Write 3-5 sentences (at most!) responding to the questions associated with each of the
ten annotations.
# Write your response after the 'Ans:' prompt below each annotation.
#

# ANNOTATION 1 - What is the purpose of the following three lines? Why do you need a
scale factor? What happens if you change the scale factor?
# Ans: oofpez: Capability of a vaccuum to permit electric field lines (1/4 pi epsilon zero)
qproton: Charge of the proton. Scale factor: how much the arrow/resultant vector is
enlarged or decreased by. S.F. represents arrows magnitude and desired length. We need
this to show the resultant vector (magnitude and direction of "r" vector). Changing the scale
factor will change the position of the "r" vector.
oofpez=9e9
qproton=1.6*10**-19
scalefactor=1e-20

# NOTE: in glowscript, you can right click and drag to rotate the view

scene = canvas(title='E-Field of a point charge', background=color.white, width=800,


height=500, forward = vector(-0.5,-0.3,-0.5))

# ANNOTATION 2 - What are the next 6 lines creating?


# Ans: The next 6 lines are creating the x,y,z axis and their corresponding directional arrows.
They are labelled x - axis, y-axis and z - axis. At the centre of these lines is the particle. The
centre of the axis can be thought of as the "root" of all vectors (in this case)
rodx = cylinder(pos=vector(-5e-10,0,0), axis=vector(10e-10,0,0), radius=1e-12,
color=color.black)
rody = cylinder(pos=vector(0,-5e-10,0), axis=vector(0,10e-10,0), radius=1e-12,
color=color.black)
rodz = cylinder(pos=vector(0,0,-5e-10), axis=vector(0,0,10e-10), radius=1e-12,
color=color.black)
label(pos=vector(-5e-10,0,0), text='x - axis', color = color.black, background= color.white,
box=False, height=15, line=False)
label(pos=vector(0,-5e-10,0), text='y - axis', color = color.black, background= color.white,
box=False, height=15, line=False)
label(pos=vector(0,0,-5e-10), text='z - axis', color = color.black, background= color.white,
box=False, height=15, line=False)
# ANNOTATION 3 - What does the object 'particle' represent?
# Ans: The 'particle' represents the point charge which is a proton.
particle = sphere(pos=vector(0,0,0), radius=2e-11, color=color.red)

# NOTE: We can bring the sphere's properties back up later using lines of code such as:
# position = particle.pos
# radius = particle.radius
# color=particle.color
# We can also change these values after the fact, if we want! (For example, we could
change the position using 'particle.pos = vector(1,0,0)')

# ANNOTATION 4 - Why do you see an "e"?


# Ans: "e" stands for 10 to the power of (x). It would be too long to write the full value of
the given number.
obslocation = vector(-2.8e-10,2.3e-10,1.9e-10) # We will need this to be a vector later, so
I've set it as a vector here.

# ANNOTATION 5 - What is this line constructing?


# Ans: This line is constructing the resultant vector of the proton.
r = obslocation - particle.pos

print ("The relative position vector r is " '<{0}, {1}, {2}>'.format(r.x,r.y,r.z))

# ANNOTATION 6 - What does this arrow represent? What are the arrow's important
features?
# Ans: Direction of the travel of the proton. The important features of is length and the
direction. Both these features give us a vector which has a direction and a magnitude.
ra = arrow(pos=(particle.pos), axis=(r), color=color.green)

rmag = r.mag # NOTE: This calculates the magnitude of the relative position vector
# 'vector.mag' is the easy way to do this, but you could also calculate it explicitly:
#
# rmag=sqrt(r.x**2 + r.y**2 + r.z**2), where r.x, r.y and r.z are the x, y and z components of
the vector r, respectively.
print ("The magnitude of r is", rmag)

# ANNOTATION 7 - Why are we dividing r by rmag? What is rhat?


# Ans: We need to give r by its magnitude in order to find what Electric field value is. "rhat"
is the unit vector of the proton.
rhat = r/rmag

print ("The unit vector rhat is", rhat)

# ANNOTATION 8 - Explain this calculation


# Ans: This calculation gives us the electric field of the proton by using the vaccuum
permitivity constant "k" and where the proton will travel with respect to the resulatant
vector.
E = (oofpez * (qproton/(rmag**2))) * rhat

# ANNOTATION 9 - Code: how would you change this line to print the size of the electric
field instead?
# Ans: print(vector.mag)
print ("The Electric field vector at the observation location is", E)

# ANNOTATION 10 - What does this create? What does the scale factor do?
# Ans: This creates the resulatant vector. The resultant vector should show the direction of
the electric field and its corresponding value (magnitude).
ea = arrow(pos=(obslocation), axis=scalefactor*E, color=color.orange)

# -----------------\
# <~> EXTENSION <~> |
# _________________/

veclist=[]
obsdist=3*10**(-10)
# So, for my nested for-loops, I know I need observation locations that are both + and -3e-
10m away from the particle along the x,
# y and z axes. I need to account for both the + and - directions (two cases) along the three
axes (three cases)
# So that's
for direction in range(2): # 0 = negative direction, 1 = positive direction
for axis in range(3): # 0 = x axis, 1 = y axis, 2 = z axis
tempvec=[0,0,0]#vector(0,0,0) # This starts me off with a list of three 0s (0,0,0)
tempvec[axis]=(2*direction - 1)*obsdist # I then replace the value in slot 'axis' (see
above where I define which number
# is which axis) with the position of the observation location
along that axis
my_vec=vector(tempvec[0],tempvec[1],tempvec[2]) # I then use the three tempvec
entries to define my observation location!
veclist.append(my_vec+particle.pos) # we then each observation location to the list of
observation locations
# By the end of these for-loops, every obsloc we need will have been added to the list!

for obs in veclist: # This for-loop sets 'obs' equal to a different item in 'veclist' each loop, and
uses that value of obs
# for the code located in the loop itself
r=obs-particle.pos
E=oofpez*qproton*r/r.mag**3
obsarrow=arrow(pos=obs,axis=E*1e-20,color=color.orange) # The rest of this is what we
did earlier!

You might also like