0% found this document useful (0 votes)
240 views10 pages

GT PHYS 2212 Experiment 1 Lab Guide/Solutions

The document provides instructions for creating a program in GlowScript to represent the electric field of a point charge. It describes organizing the program with sections for constants, objects, initial values, and calculations. It guides the user through defining constants, creating a sphere to represent a charged particle, calculating the relative position vector r from the particle to the observation point, and displaying r with an arrow object. The document explains how to calculate and print the magnitude of r using vector components, exponents, and the square root function supported in GlowScript.

Uploaded by

Chris Kwak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views10 pages

GT PHYS 2212 Experiment 1 Lab Guide/Solutions

The document provides instructions for creating a program in GlowScript to represent the electric field of a point charge. It describes organizing the program with sections for constants, objects, initial values, and calculations. It guides the user through defining constants, creating a sphere to represent a charged particle, calculating the relative position vector r from the particle to the observation point, and displaying r with an arrow object. The document explains how to calculate and print the magnitude of r using vector components, exponents, and the square root function supported in GlowScript.

Uploaded by

Chris Kwak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Calculating and displaying the electric field

of a single charged particle

1 Introduction
In this lab, you will use GlowScript (http://www.glowscript.org) to construct a representation of
the electric field of a point charge. In this lab you will become familiar with:

• the concept of a vector field,

• the relative position vector (and the vectors that describe it),

• using GlowScript to compute vector fields,

• and the pattern of the field surrounding a point charge.

2 Purpose
In previous homework problems, you have calculated the electric field produced by a single
charged particle. The somewhat tedious process of calculating electric field vectors can be auto-
mated by programming a computer to do this. In addition, doing the calculations in GlowScript will
allow us to display the electric field in multiple locations, so we can examine the 3D pattern of field
created by a charged particle.

3 Program organization
You should organize a program in the following way, inserting comments with a hashtag sign (#)
to explain what the different sections of the program are doing:

1. A section giving names and values to any constants that will be used.

2. A section in which visible objects like spheres or arrows are created and named (sometimes
you will end up creating more objects later.)

3. A section in which important variables are named and given initial values, if any.

4. A section defining the new functions you will use in the calculations, if any.

5. A section for the calculations.

1
4 Starting the program
4.1 Create a new program file
Create a new program in your public folder. You will be taken to the GlowScript editor, and your
new program will automatically have the following first line:

GlowScript 2.7 VPython

Leave this line alone; GlowScript needs it to correctly interpret the code you write.

4.2 Constants
Constants should be defined at the beginning of your program. Type the following lines:

# CONSTANTS
oofpez = 9e9
qproton = 1.6e-19

The first line gives the name “oofpez” (which stands for “One Over Four Pi Epsilon-Zero”), to the
number 9e9. The second line gives the name “qproton” to the charge of a proton.

To make sure you have typed everything correctly, add the statement:

print(oofpez)

Run your program by clicking the “Run this program” link. Does the correct value of the constant
print out in the GlowScript window?

4.3 Creating the charged particle


In your program add the comment line:

# OBJECTS

Now we’ll create a red sphere to represent a positively charged particle. Start by writing the line:

particle = sphere(pos=vec(0, 0, 0), radius=1, color = color.blue)

Here, we’ve created a sphere object and given it the name particle, and defined the initial values
for its attributes pos (the position, a vector), radius (its radius, a scalar), and color (a special color
object).

Run the program. You should see a blue sphere in the middle of the screen; you can zoom in and
out with your scroll wheel, and rotate with a right-click-drag.

2
Change the initial values of the attributes of particle to achieve the following goals:

• The sphere should be located at h1 × 10−10 , 0, 0i m

• The radius of the sphere should be 2×10−11 m. (This is much larger than the radius of a
proton, which is about 1×10−15 meters, but we will exaggerate the size of the particle in
order to make it easily visible in our display.)

Run the program. You should see a sphere somewhat to the right of the center of your display
window. If you don’t see anything, first check the GlowScript window for error messages, then try
again.

Now we’ll declare that the particle has the charge of one proton by assigning the object a “charge”
attribute and setting it to the value of qproton. Add this line:

particle.charge = qproton

Note that, before this line is executed by GlowScript, the particle object already has various
attributes like pos and radius but no charge attribute; if you attempt to use particle.charge in
a calculation above this line, you’ll encounter an error. Also note that GlowScript doesn’t “know”
what a charge is and doesn’t by default do anything with it, charge has no special meaning in
terms of the way the code runs—we’ll have to write the code that defines what the charge actually
does.

4.4 Initial values of variables


In your program add this comment line:

# INITIAL VALUES

Now we’ll assign a symbolic name to the observation location. We need a symbolic name for this
location, because we will eventually want to instruct GlowScript to calculate the electric field at
many different observation locations. Since there is no object at the observation location, we must
create a vector variable to represent the observation location. Well call it “obslocation”.

Type the statement:

obslocation = vec(3.1e-10, -2.1e-10,0)

This defines a vector named obslocation to represent the location where we want to find the
electric field. Later we will have the program calculate the electric field at other locations, too. A
vector is not a displayable graphical object, so it doesn’t have a pos attribute. We refer to this
vector simply by its name obslocation. The position attribute of the displayable sphere object is
a vector which is referred to as particle.pos.

3
5 Calculations
5.1 Instructing GlowScript to calculate and display the relative position vector, ~r
When calculating the electric field at an observation location, the relative position vector always
points from the source particle (the initial location) to the observation location (the final location).

• In your program, write a line of code that calculates the relative position vector ~r. Call this
vector r.

• Add a line of code to print the relative position vector from source charge to observation
location:
print("The relative position vector is r=", r)
Note that anything in quotes inside a print statement is printed as text, while anything not in
quotes is assumed to be a symbolic name, and its value is printed; separate quantities to be
printed with commas as shown.

5.1.1 Creating an arrow object

We often use arrow objects in GlowScript to depict vector quantities, and here we’ll use an arrow
to depict ~r. We next add arrows to our program. These will represent vector quantities in future
programs.

• Type the following on a new line, then run the program:

arrow(pos=vec(2,-3,0), axis=vec(3,4,0), color=color.cyan)

• Experiment with other changes to pos, axis, and color, running the program each time you
change something. Change one thing at a time so you can tell what effect your changes
have. For example, try making a second arrow with the same pos but a different axis (and
different color.)

Questions:
Stop for a minute and think about the answers to these questions. You don’t have to write the
answers but if you take notes it might help you in the future.

Is the pos of an arrow the location of its tail, its tip, its middle, or somewhere else?

Is the axis of an arrow the position of its tail, the position of its tip, or the position of
the tip relative to the tail (that is, a vector pointing from tip to tail)?

To create a sphere whose center is at the tip of the cyan arrow above, what should the
pos of the sphere be?

4
5.1.2 Multiplying by a scalar: Scaling an arrow’s axis

Because the axis of an arrow is a vector, we can perform scalar multiplication on it. Start with this
arrow:

arrow(pos=vec(2,-3,0), axis=vec(3,4,0), color=color.cyan)

• Modify the axis of the arrow by changing the statement to the following:

arrow(pos=vec(2,-3,0), axis=-0.5*vec(3,4,0), color=color.cyan)

Questions:
Stop for a minute and think about the answers to these questions. You don’t have to write the
answers but if you take notes it might help you in the future.

To make an arrow 3 times as long without changing its direction, what scalar factor
would you multiply axis by?

To reverse the direction of an arrow without changing its length, what scalar factor would you
multiply axis by?

Now, name this arrow ra, make it green, place its tail at the center of the source charge, and make
its axis equal to ~r, the relative position vector you just calculated. The arrow object is used to
represent the vector.

Run the program. In the display window you should see a green arrow from the source charge to
the observation location, and in the GlowScript window you should see the printed value of ~r.

5.2 Telling GlowScript how to calculate the magnitude of ~r


q
We need to translate the equation |~r| = rx2 + ry2 + rz2 into GlowScript.

5.2.1 Referring to vector components

The components of a vector are attributes of the vector, so they can be referred to with “dot” syn-
tax. The components of a vector named r are r.x, r.y, and r.z.

5.2.2 Exponentiation operator

In GlowScript, use the operator ** to raise a quantity to a power. For example, b3 should be written
as b**3.

5
5.2.3 Square root operator

In GlowScript the function sqrt() is used to compute a square root. For example, 3 should be
written sqrt(3).

5.2.4 |~r|

• In your program, write a line of code to calculate |~r|, the magnitude of the relative position
vector ~r. Call this quantity rmag.

• Add a line of code to print the value of rmag.


print("The magnitude of r is |r|=", rmag)

• Run your program. Compare your printed value to the answer on the last page of this
document.

5.2.5 r̂

• In your program, calculate the unit vector r̂ in the direction of ~r . Call this vector rhat.

• Add a line of code to print the value of rhat:


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

• Run your program. Compare your printed values to the answer on the last page of this
document.

5.3 ~ at the observation location


E
You have now calculated ~r, |~r|, and r̂. Finally, we need to use all of these pieces to calculate
~ as a vector, at the observation location. Write down the symbolic algebraic
the electric field E,
equation used to calculate the electric field vector. Now convert it to GlowScript. Use the
name E for the electric field vector you will calculate.

• To check your work, add a print statement after the calculation of the electric field:
print("The electric field vector is E=", E)

• Run your program. Compare the answer computed by your program to the value given
on the last page of this document.

5.4 Drawing an arrow to represent the electric field vector


We want to represent the electric field vector we calculated by an arrow.

• Create an orange arrow and give the arrow the name ea.

• Remember that the tail of the arrow must be placed at the observation location!

6
• Think about what the axis of the arrow should be, and write the appropriate code.

• Run the program.

Checkpoint:
Is your code running well so far? If you’re running into trouble, you can contact an instructor or
ask your fellow students.

5.5 Scaling the arrow to a reasonable size


The point of a scale factor is to make it possible to see everything in the display at once. You
should now see an orange arrow pointing in the appropriate direction. However, you can’t see the
sphere representing the positively charged particle, or the arrow representing the position vector!
The arrow is so big that when GlowScript positions the “camera” so we can see the arrow, the
sphere is too small to see.

At the moment, the axis of the arrow is a vector equal to the electric field at the observation loca-
tion. We need to “scale down” the arrow so it is not gigantic compared to the sphere representing
the charged particle. To do this, we need to multiply the arrow axis by a scalar, thus changing the
magnitude of the arrow axis without changing the direction of the arrow. You must use the same
scale factor for all electric field arrows.

Although you may be able to find an appropriate scale factor by trial and error, it may be much
faster to print the magnitude of E and use this to figure out an appropriate scalar multiplier.

5.6 What you should have so far


Your program should now include the following, to scale down the size of the arrows:
scalefactor = (your value) # in the constants section of the program
ea = arrow(pos = (appropriate location), axis=scalefactor*E, color=color.orange)

Run the program, and make sure you can see the sphere, the arrow representing the position vec-
tor, and the arrow representing the electric field vector. You may want to change the scalefactor
so the display looks better to you.

Questions:
Check your own work:

• Can you see the green position arrow?

• Does the orange arrow point in the correct direction?

• Is the tail of the orange arrow at the observation location?

7
• Does the orange arrow point in the correct direction?
• Is the tail of the orange arrow at the observation location?
• Make sure everyone in the group agrees that the display looks reasonable. Then discuss your display with
a neighboring group.
6 Add more observation locations
To see the pattern of electric field around a charged particle, you will extend your program to
5 Add more observation locations
calculate the electric field at many locations, all the same distance from the source charge. One
To seewaythe to do thisofiselectric
pattern to copyfield
the around
code you have written
a charged to calculate
particle, you will and display
extend yourthe electrictofield,
program and
calculate
paste it in multiple times, typing new values for the observation location each time.
the electric field at many locations, all the same distance from the source charge. One way to do this is to This is the way
copy thewe’ll do you
code it thishave
time; later we
written to will learn a
calculate more
and flexible
display theway to dofield,
electric this and
using a loop.
paste it in(Optionally, you
multiple times,
typing may
new use
values a loop hereobservation
for the if you can do this quickly.)
location each time. This is the way we’ll do it this time; later we
will learn a more flexible way to do this using a loop. (Optionally, you may use a loop here if you can do
• In your program, copy code so as to calculate and display the electric field at 6 more loca-
this quickly.)
tions.
• In your program, copy code so as to calculate and display the electric field at 6 more locations.
• Do not copy the code for the green arrow representing the position vector.
• Do not copy the code for the green arrow representing the position vector.
• Each observation location should be 3×10−10 m from the source charge. The observation
• Each observation location should be 3 × 10−10 m from the source charge. The observation locations
locations should be directly above, below, to the right, to the left, in front of, and behind
should be directly above, below, to the right, to the left, in front of, and behind the source charge,
the source charge, as shown in the diagram below. So, for example, you should have one
as shown in the diagram −10 below. So, for example, you should have one location at < 4 × 10−10 , 0, 0 >
location at h4 × 10 , 0, 0i m.
m.

• Your program should display 7 orange arrows, representing the electric field at 7 locations. 6
of these orange arrows should be at observation locations displaced from the source charge
along one direction (for example, the +z direction, the -z direction, etc.).

• There should 1 green position arrow from the source to the first observation location.

• The arrows should point in the correct directions!

• Rotate the display to get a feel for the pattern in space.

8
7 Change the sign of the source charge
Change the source charge to be negative, and change the color of the sphere to blue. Adjust the
scalefactor if necessary to make the display understandable. It’s okay if the single green position
arrow and the associated orange field arrow overlap. Make sure that the display looks reason-
able for a negative source charge.

Checkpoint:
How do you know whether the display looks reasonable for a negative source charge?

8 Functions
For convenience, we’ll want to be able to calculate the electric field created by a charged particle
many times without having to copy Coulomb’s law over and over again. We’ll do this by defining our
own function, which we’ll only have to type once; we will be making extensive use of self-defined
functions like these in later labs.

Here, we’re going to replace the multiple copies of the electric field calculation in your code with a
single function definition. The first line of a function definition in GlowScript begins with the special
term def, followed by the name you want to give the function and a parenthetical list of all the input
variables the function needs. In our case, since we’re defining an electric field function, we’ll name
the function Efield. To calculate the electric field of a charged particle, the function needs to know
the position of the charged particle, the charge of the particle, and the position of the observation
location, along with the value of oofpez.

The particle object we created earlier contains the attributes pos and charge, two of the values
we need to calculate the electric field. You’ve already defined various observation locations, which
are also needed. oofpez is already defined and won’t change, so we can just use it directly in the
function without declaring it as an input variable.

We will therefore construct a function that takes a charged particle object (a sphere with a charge
attribute) and observation location object (a vector) as input variables, and returns an electric field
vector as output. Write these lines in your code, being sure to indent the lines beneath the first one:

def Efield(chargedParticle, observationLocation):


q = chargedParticle.charge
r = observationLocation - chargedParticle.pos
E = vec(0,0,0)
return E

The indented return E line defines the output of the function. Now you can use Efield() as a
function just like mag() or sqrt(), as long as you feed it the correct input variables. Right now, of
course, this function does nothing but return a zero-vector.

9
Adapt the function to calculate an electric field, then replace the 6 lines you wrote to calculate the
electric field at different observation locations with 6 calls to this function, in this style:

E2 = Efield(particle, obsLocation2)

9 Turn in your program


Submit a link to your GlowScript program on Canvas. Although this lab is ungraded, as its purpose
is to introduce you to using GlowScript, you should still strive to do your best since it will greatly
help you with subsequent labs this semester.

Your code is correct if your program correctly calculates and displays the 1 position vector and 7
electric field vectors specified in these lab instructions.

10 Answers to selected questions from Section 5


• From Section 5.2.4, the magnitude is approx. 3×10−10 m.

• From Section 5.2.5, the unit vector is approx. h0.707, −0.707, 0i.

• From Section 5.3, the electric field is approx. h1.15 × 1010 , −1.15 × 1010 , 0i N/C.

Last edited: May 11, 2022

10

You might also like