VPython For Introductory Mechanics - Complete Version
VPython For Introductory Mechanics - Complete Version
Dickinson Scholar
VPython for Introductory Mechanics
2019
Lars Q. English
Dickinson College
Recommended Citation
Morgan, Windsor A. and English, Lars Q., "VPython for Introductory Mechanics: Complete Version" (2019). VPython for Introductory
Mechanics. 1.
https://scholar.dickinson.edu/vpythonphysics/1
This Book is brought to you for free and open access by Dickinson Scholar. It has been accepted for inclusion in VPython for Introductory Mechanics
by an authorized administrator of Dickinson Scholar. For more information, please contact [email protected].
VPython for Introductory
Mechanics
Incorporating numerical simulations
in the introductory physics
curriculum
1 Introduction 1
10 Conclusion 75
Bibliography 77
About Authors 79
Preface
The exact order of topics was chosen to enable its easy integration
into our Workshop Physics curriculum.1 While the pedagogical ap-
proach, format and presentation of Workshop Physics is very differ-
ent from the more standard lecture-based course, its content selec-
tion is fairly conventional. Thus, we believe that this presentation
should also work well in conjunction with a more traditional intro-
ductory mechanics course (either at the senior high-school or first-
year college level). Furthermore, swapping neighboring chapters or
omitting certain sections should create only minimal disruption.
For Chapter 7, it is assumed that the course has now reached a dis-
cussion of momentum and energy. We chose the binary star system
4 Chapter 1 Introduction
Lastly, before we get started, all the VPython codes featured in this
book can be accessed online at
https://scholar.dickinson.edu/vpythonphysics/ .
Getting Started with VPython
2.1 “Hello.”
VPython is a language that is pretty easy to use, once you get used to
it. Python is becoming widely used by scientists around the world,
and VPython is a visual implementation of it. It will help you visu-
alize the physics you will be learning.1
1
See also D. Schroeder, “Physics Simulations in Python” (2018).
6 Chapter 2 Getting Started with VPython
6. You will now see an editing space that is blank except for the
line “GlowScript 2.8 VPython” (possibly with a different ver-
sion number). Click in the white space below that line and
type the following, verbatim:
print("Hello, world!")
7. Then click the “Run this program” link at the top of the page.
Your editing space should then vanish, replaced by a new text
area with the words “Hello, world!”
If the program didn’t work, check the spelling and capitalization and
punctuation. Make sure that you typed the line precisely as shown
above.
If you click “Edit this program”, you can see the program you just
typed. The first line, which is the same for all GlowScript programs,
tells the system the language (VPython) and version (2.8) that is to
be used. You must have this line, and you should always just leave
it alone.
The next line tells the computer to call the print function and passes
to it a string of characters. Don’t worry that you don’t know what
the italicized words mean – we’ll explain them here:
8 Chapter 2 Getting Started with VPython
On a new line below your print command, type the following com-
mand:
box()
You can change the attributes of your box by passing some differ-
ent parameters to the box function. Try typing this to replace the
original box line:
10 Chapter 2 Getting Started with VPython
box(pos=vector(1,0,0),size=vector(.5,.3,.2),
color=color.green)
You can learn more about other colors by clicking the Help link
at the upper-right corner of the window. It might be beneficial to
open the help page in a new browser tab. Then, from the second
drop-down menu in the left sidebar, choose “Color/Opacity”.
There you will find a list of pre-defined colors, and also see how you
can use the vector function to create arbitrary colors.
sphere(radius=0.25)
The sphere function can also accept the pos and color parame-
ters, so use those now to change the defaults according to your taste.
Don’t forget to separate the parameters by commas!
2.4 Comments
Examine the program provided for you in Figure 3.1. Type it in.
Please note that the indentations are important (as is capitalization,
usually).
What is each line doing? You can probably figure it out using the
commented-out lines. Now run the program. What is happening?
You probably found that this program is having a sphere move hor-
izontally at a constant velocity. The command in line 14:
14 Chapter 3 Moving Objects Using Formulas
x = x0 +v*t
is telling the program to take the original value of x, x0, and to add
a constant velocity times the time elapsed t. This works, but we
will see in the next chapter that it is not the most elegant way of
updating the value of x.
Looking at the motion of the ball is fine, but plotting the values of
position, velocity, or acceleration versus time is a way to visualize
what is happening.
Take the first program, copy it with a new name, and add commands
to plot the ball’s position with respect to time. To do this, you must
set up a graph and declare its width and height:
Your program should look something like the one in Figure 3.2:
3.2 Motion of a ball with constant velocity with a plot of
15
position vs. time
Usar em MRU
The command graph in line 3 sets up a graph with width 400 pixels,
height 200 pixels, with an x-axis labeled “time” and a y-axis labeled
“position”. The set-up graph is called “g1” for ease of use. Line 4
uses the command gdots to say how the data is to be plotted, and
call it “xDots”. Here, it says that green dots should be used in the
graph called “g1” that was set up in the line before. Finally, line 21
says that “xDots” should be plotted with the values of “t” (for time)
for the x-coordinates, and the values of “x” (for position) for the y-
coordinates.
Your plot should like what is shown in Figure 3.3. Is this what you
would expect? Why?
16 Chapter 3 Moving Objects Using Formulas
Figure 3.3: Plot of position versus time for ball with constant
velocity.
Now, let’s simulate the motion of a ball being dropped from a height
of two meters.
What does the position-time plot look like now? How is it different
from the previous plot? Why?
Plot the velocity versus time. How does this compare to the current
position versus time plot and the previous position versus time plot?
3.4 Stop and Go 17
Now let’s use a similar idea to make the ball move to the right at a
constant speed for 2 seconds, then to have it stop and sit there for 2
second, and then to have it move to the left at a constant speed for
2 seconds.
Discuss a possible strategy with your partners. You may find that
there are multiple ways in which you could accomplish this objec-
tive. One way would be to have three different WHILE loops - one
after the other. However, perhaps a more elegant approach uses the
if-then-else syntax - a core syntax structure of any programming
language. In VPython, it gets implemented in a very intuitive way:
If (“Condition to be tested”):
“Execute these commands”
Else:
“Execute those commands”
Can you incorporate if-statements to make the ball move to the right,
stop, and then move to the left without any discontinuities? It may
18 Chapter 3 Moving Objects Using Formulas
The term omega, written as ω, is called the angular velocity (or the
frequency). You will see this term later in the course when we dis-
cuss rotational motion. Its value in the program will help you to see
3.5 Motion of a Planet 19
how it is defined.
This approach was quite powerful already, but it also limits what
we can do. For example, it would already be quite difficult to make
an object move forward, stop for a while and then move backward
using this approach, as you probably discovered in the last chapter.
Furthermore, this approach also imposes solutions rather than nu-
merically finding them, thus sort of bypassing the underlying phys-
ical laws, as we will see later.
Let’s start with a piece of physics that you already know – the for-
mula for average velocity in one dimension – and then translate it
to a statement a computer can understand. So, average velocity is
defined as,
22 Chapter 4 A First Look at Simulating Motion
∆x x2 − x1
vavg = = (4.1)
∆t ∆t
The upshot is that we will want to make our time interval ∆t really
small in Equation (4.1). Over the course of this small time interval
we would not expect the velocity to change very much, so a pretty
good assumption is that it is simply constant over this interval. This
also means that the average velocity is just the same as this con-
stant velocity and we can drop the subscript “avg”. If we then solve
Equation (4.1) for x2 , we get:
x2 = x1 + v∆t (4.2)
Now all we have to do is repeat the step in Equation (4.2) over and
over again. Every time we advance forward in time by the small
4.2 The Basic Code 23
We see that the core of the program is contained in the WHILE loop,
as before. Let’s start our discussion with the central line, namely
LINE 22:
x = x + v ∗ dt (4.3)
the right of the equal sign is computed first based on the current
value of x. Secondly, the “x = ” part then assigns the result of that
computation to the variable x. The upshot is that x is updated and
now holds the new value.
x2 = x1 + v∆t,
where x2 is the new value and x1 is the old value of position. Note
also that “dt” in Eq. 4.3 should not be interpreted as a differential in
the Calculus sense but represents a small time interval, defined in
LINE 12 as 0.05.
The last line, Line 23, updates the time variable. This is necessary in
order to test the conditional of the WHILE loop (t<3), as well as for
the purpose of plotting the position (and velocity) graph. It does not,
however, come into play in calculating the position of the object – an
important difference from the previous approach of simply plotting
the function x(t).
4.3 Exercises
In this activity, you will extend what you learned in Chapter 3 about
the kinematics equations into two dimensions. These equations de-
scribe the motion of a projectile undergoing a constant acceleration.
While “x” is used above in Equations (5.1), (5.2), (5.3), they are simply
placeholders; one can easily replace them with “y”:
1
y = y0 + vy0 t + ay t2 ; (5.4)
2
vy = vy0 + ay t; (5.5)
and
vt2 = vy0
2
+ 2ay (y − y0 ). (5.6)
28 Chapter 5 Visualizing Projectile Motion
5.2 Exercises
Use Equations (5.1), (5.2), (5.4), (5.5), (5.7), and (5.8) to write VPython
code that simulates the flight of a projectile. Make the projectile a
sphere of radius 0.1. Assume that there is no air resistance. Assume
that we launch the projectile at the origin (0,0), and stop the pro-
jectile motion once it hits the ground at y = 0. This means that the
terrain is flat. Draw a line (or horizontal plane) that indicates this.
5.2 Exercises 29
while(y>-0.001)
One thing to keep in mind is the built-in unit for angles in VPython
is always radians. This means that you will need to convert any
angle in the argument of a trig-function from degrees to radians.
Now play around with launch angle at constant launch speed. Make
a table of the range for each launch angle. For what angle θ do
we seem to get the largest range? Is this range consistent with the
theoretical formula for the range of a projectile, given by
30 Chapter 5 Visualizing Projectile Motion
v02 sin 2θ
R= . (5.9)
g
Now let’s make the ground not level (or flat). Let’s have a steady
downward slope. Redraw the line (or plane) so that it tilts down
with a slope of rise/run = -0.1.
The way we can simulate that in the code is by modifying the WHILE
statement. How do you have to do that? Once your group finds a
solution, implement it in the code.
Repeat the simulation, but with a terrain that gently slopes up, with
a slope of +0.1. What is the best angle now?
Make the code stop if or when the projectile hits the object, which
we will define as anywhere within the volume of the object. If we
do not hit the sphere, then make the code stop as before when the
ground it hit. Because we are only thinking in two dimensions, in
order to hit the sphere we need to just have the projectile get within
0.5 units of the center. How can we do that?
One thing that will probably need here is the following command:
BREAK
Scan the following VPython code and annotate each line. Discuss
what you think this code will do. Make a prediction as to the kind
of motion that would be highlighted.
32 Chapter 5 Visualizing Projectile Motion
So, let’s assume that we know the force that is acting on an object as
a function of the object’s location. What this means is that no matter
where the object happens to be, we can calculate the force that it
experiences there. Mathematically speaking, what we are given is
the force-function, F⃗ (⃗r). This notation communicates that we can
evaluate a force vector, F⃗ , by inserting a position vector, ⃗r, into a
function. Mathematicians would call such a function a map from R3
to R3 . A good example are the so-called central-force problems, such
as the gravitational force a comet feels in the vicinity of the sun.
We also know that the force on the object is related to the accelera-
tion of the object via Newton’s second law:
F⃗ = m⃗a (6.1)
34 Chapter 6 Simulating Central-Force Problems
So here is the basic idea. Let start from this position and velocity in
our code, call them: ⃗x1 and ⃗v1 . From ⃗x1 we can compute the force
at that location and thus the acceleration ⃗a1 . Using this ⃗a1 , we now
update the velocity. Here we make use of the formula for average
acceleration,
∆⃗v
⃗aavg = . (6.2)
∆t
As the time interval gets very small (and, in the limit, infinitesi-
mal), the average acceleration becomes the instantaneous acceler-
ation, and so,
⃗v2 = ⃗v1 + ⃗a ∗ ∆t. (6.3)
In the third and final step, we now use this new velocity to update
the position, using the vector-verson of Equation (4.2), namely:
Now that we have computed the new position vector ⃗x2 , we can
start the whole procedure over again by first computing the new
force and acceleration (F⃗2 , ⃗a2 ), then the new velocity (⃗v3 ), and then
finally the new position (⃗x3 ). Schematically, we are proceeding in
steps given by the following flow chart:
1 A.
Cromer, Stable solutions using the Euler Approximation, American Journal of Physics,
49, 455 (1981).
36 Chapter 6 Simulating Central-Force Problems
To illustrate the method, let’s get our feet wet with a relatively sim-
ple problem - a mass on a horizontal spring. All we have to remem-
ber is Hooke’s law for ideal springs,
Let’s look at the basic VPython code, shown in Figure 6.1, to see
how the Euler-Cromer method gets implemented in practice here.
The basic steps (outlined in the flowchart in the previous section) are
contained in lines 24 through 27. Line 28 is not absolutely necessary,
but it is included for the purpose of making position and velocity-
time graphs.
Figure 6.1: The basic code simulating a mass at the end of a spring
spring. The only thing we have to take care of is to deform the helix
in dependence upon the position of the end-mass.
6.3 Exercises
• Find the place in the code where the initial conditions (x0
and v0 ) are specified. Try running the program with a
few different sets of initial conditions. Does the period of
oscillation seem to depend on this choice?
• What if you double (or triple) both the mass and the spring
constant? Do the graphs change?
√
m
T = 2π
k
You might be thinking that the changing direction of the drag force
would be difficult to “handle”, and ordinarily you would be right.
In fact, this facet of the problem coupled with the quadratic depen-
dence on speed makes this problem impossible to solve in closed
form with pencil and paper. So here we now encounter our first in-
stance of a problem that has no closed-form analytical solution and
where we rely on a computer to give us the solution numerically.
• mag(A) and mag2(A), where the output yields the length and
length-squared of that vector, respectively.
• A.hat, which produces from the vector A its unit vector (by
dividing it by its length). This syntax treats the directionality
(given by the unit vector) as a property of the vector. It is just
like any other property of a vector, such as A.x, which yields
the x-component of the vector A.
40 Chapter 6 Simulating Central-Force Problems
What is interesting is that ball.v has itself all the properties that a
vector can have, and so, for instance ball.v.x would refer to the x-
component of the ball’s velocity vector. Similarly, ball.v.hat would
give the unit vector in the direction of motion.
F=vector(0,-m*g,0) - 0.5*C*rho*Area*mag2(ball.v)*ball.v.hat.
You should recognize this as the net force acting on the projectile.
The first part is the gravitational force near the surface of the earth
and the second is the drag force. Notice that VPython automatically
adds these two parts as vectors.
When you run the program you should get something like the fol-
lowing:
42 Chapter 6 Simulating Central-Force Problems
Notice that the code returns the approximate range of the projectile
in the print statement. Also observe the shape of the trajectory. It
clearly deviates from the parabola - the highest point in the trajec-
tory is not reached at the horizontal half-way point, and the descent
is steeper then the ascent.
6.5 Exercises
• Add to the code given above so that you can get two trajecto-
ries on screen simultaneously. These two trajectories should
correspond to two different projectiles (differentiated either
by mass or drag coefficient).
Our final example is also the most famous in the sense of historical
significance - the Keplerian orbits of planets and comets around the
sun. The perfectly circular orbit is one special solution to Newton’s
second law. This is usually demonstrated in introductory physics
by setting the formula for the centripetal force equal to the gravita-
tional force,
mv 2 GM m
= , (6.7)
r r2
We set them equal because gravitation actually provides us with the
centripetal force necessary for moving in a circle. Solving Equation
(6.7) for v yields the orbital speed as a function of the orbital radius,
√
GM
v= (6.8)
r
44 Chapter 6 Simulating Central-Force Problems
the parameters more realistic; the mass ratio that appears here is
only 1:100, for instance. Nonetheless, we can use this simplified
code to explore the possible orbits.
6.7 Exercises
• If you doubled the initial distance of the comet from the sun,
what speed would be necessary now for a circular orbit? Try
it in the code.
46 Chapter 6 Simulating Central-Force Problems
• If you make the initial speed too small, you will see that
the simulation eventually breaks down and returns non-
sensical results. Why is that and what can you adjust to
remedy the situation? [Hint: When the comet gets very
close to the sun, the acceleration it experiences becomes very
large. What line in the code is going to be adversely affected?]
• Now lower the speed and observe the orbits that result. What
is the most obvious qualitative difference about the orbits that
we get now in this alternative universe?
Conservation of Momentum
and Energy
m1 x1 + m2 x2 + ...
xcom = ,
m1 + m2 + ...
and similarly for the y-coordinate. Since VPython deals well with
vector quantities, we can also dispense with the individual compo-
nents and refer only the position vectors, ⃗ri , of the objects them-
selves: ∑
mi⃗ri
⃗rcom = ∑ . (7.1)
mi
You may have learned that whenever the total momentum of a sys-
tem is conserved, i.e., when there is no net external force acting on
the system, then the center of mass cannot experience any accelera-
tion. In our notion, we can write,
d2
⃗acom = (⃗rcom ) = 0. (7.2)
dt2
The proof of this statement is actually not difficult and well within
the reach of an introductory physics student. See if you can work it
out yourself.
Let us again start with a code template - see Figure 7.1. One thing
you will notice immediately is that we are now not working in ar-
tificial units (as before) and instead use the true value for the gravi-
tational constant. That also forces us to use astronomically realistic
values for all other quantities in the problem, such as for the masses
and distances involved. Again, as long as all the inputs are in SI-
units, so will all the computed outputs.
As you can see, in the first six command lines, we basically set up
the properties about the two stars. For instance, the bigger star has
a mass of 3 × 1030 kg - three times that of the smaller star. For
50 Chapter 7 Conservation of Momentum and Energy
comparison, our sun’s mass is about 2 × 1030 kg. One of the things
you will have a chance to play around with is the mass ratio of the
two stars.
The next line (Line 17) defines the computational time step. This
might strike you as an incredibly large time step, especially com-
pared to the values we have used before, but remember - everything
has to be astronomical, including time. The time step of 105 sec-
onds translates to a duration of slightly longer than an earth day.
Compared to the period of revolution this is still quite small.
The iterative part of the program starts with Line 25. We define the
vector, r, that runs from the small star to the big star. We could have
also reversed it, but it is important to be consistent. The way it is
defined, it will be parallel to the force on the small star and anti-
parallel to the force on the big star. By Newton’s third law, these
two interaction forces must be equal and opposite.
7.2 Exercises
• You will also notice that the center of mass does not stay
stationary, but that it moves on the screen. We can make that
motion cease if we give the two stars initial momenta that
add to zero. Show that right now (i.e., in Figure 7.1) they two
initial momenta do not add to zero.
• Now change the initial conditions such that the total mo-
mentum is in fact zero initially. What do you notice about
the center of mass motion on the screen? Also change
the viewer’s perspective to see the two-body motion from
different angles.
Figure 7.2 shows the new setup for the nuclear context.1 The first
thing we notice is the new scene.range which catapults us into the
microcosm. Next we define the nucleus and its properties of mass,
charge and momentum.
Next, we define the alpha particle and its properties of mass, charge
and momentum. Notice that we give the alpha particle some ini-
tial speed to the right. In fact, this initial speed is quite large,
v0 = 5 × 107 m/s, about 17 percent of the speed of light. We need
large speeds for the alpha particle to get close to the gold nucleus.
This initial speed is another parameter we will be able to adjust later.
Line 22-25 set up arrows for the momenta of the two particles (alpha
1 This code is loosely based on the code by R. Chabay, “03-particle collision”, available in the
and Gold nucleus) and the total momentum. In order to see these
arrows on the screen, they have to be stretched by the scale-factor,
scale.
Finally, we have added here for the first time a start-pause button,
which will allow us to start the code and pause it at any time. The
details of this section of code need not concern us here.
The main part of the code, namely the WHILE loop, is shown in Fig-
ure 7.3. We should recognize large chunks of it from before. Lines
52 to 55 are new. They will update the momentum vectors through-
out the interaction of the two particles. The lengths and direction of
the three momenta (alpha, nucleus, and total) are set via the .axis
assignment. Additionally, we have displaced the tail of the momen-
tum vector associated with the nucleus to coincide with the head of
56 Chapter 7 Conservation of Momentum and Energy
the alpha’s momentum vector. This way we can verify visually that
the two individual momentum vectors do indeed add to the same to-
tal momentum vector (via the head-to tail method of adding vectors).
7.5 Exercises
• Run the code and observe the trajectories of the two par-
ticles. Also observe the particle’s momentum vectors in
the upper-right corner. You should see something like this:
7.5 Exercises 57
• You can now explore the role of the initial speed of the alpha
particle. How does the angle of deflection seem to depend on
this initial speed?
58 Chapter 7 Conservation of Momentum and Energy
Rotational Motion, Torque, and
Angular Momentum
GM m
F⃗ = − r̂ (8.1)
r2
(Note this looks similar to Equation (7.4), the Coulomb’s Law force
between two charges).
Note that the direction of the position unit vector r̂ is always point-
ing in the exact opposite direction (because of the minus sign) of the
force vector.
product ⃗r and F⃗
⃗τ = ⃗r × F⃗ (8.2)
Equation (8.2) shows that the torque is the cross product of the
position vector ⃗r and the force applied F⃗ . The cross product is a
vector. Remember that the magnitude of the cross product here is
|⃗r||F⃗ | sin θ,
cross(A,B)
Look back at Equation (8.1). Recalling that the unit vector r̂ is ro-
tated 180◦ from F⃗ , what is the value of ⃗τ ?
8.2 Exercises 61
⃗ must be conserved.
Because τ = 0, then L
8.2 Exercises
• Add to the code in Figure 8.1 to calculate the torque and plot
it (remember it is a vector and you can print the components
of a vector!).
Chapter 8 Rotational Motion, Torque, and Angular
62
Momentum
In the previous chapters of this book you have gone from investigat-
ing the simple motions of an object with constant velocity to explor-
ing the physics of a comet about the Sun. While these phenomena
are “real life”, we want to end with a consideration and discussion of
a truly “Space Age” phenomenon that is used regularly in sending
space probes to the outer solar system – gravitational assist, or
the “slingshot” maneuver.
But how to get there? Traveling directly away from the Sun to ar-
rive at a distant planet would take an extraordinary amount of fuel
to travel – and that extra fuel means more mass, and the more mass,
the more fuel needed. It would be extremely costly, and at this writ-
ing we do not have the technology to develop rockets to transport
a space probe those distances. The Hohmann orbit (least energy
method) saves on fuel, but takes quite a while to complete the jour-
ney.
1 An astronomical unit is the average distance of the Earth to the Sun. It is equal to 1.50×108
kilometers.
64 Chapter 9 Two Capstone Projects
So, if the kinetic energy of the probe increases, then the kinetic en-
ergy of the planet decreases. The planet slows down! However, the
amount of decrease of kinetic energy is negligible to the planet, be-
cause the mass of a planet is huge compared to the mass of a space
probe. For example, the mass of Uranus or Neptune is approximately
1026 kg, compared to the mass of a space probe, which is typically
on the order of 103 kg.
In the rest frame of the Sun, the Sun “sees” the planet moving with
⃗i . The space probe is moving with initial velocity
initial velocity U
v⃗i . The two objects are not necessarily going to meet head-on; that
would certainly result in the loss of the spacecraft. Instead, their
velocity vectors are separated by a distance b, called the impact
parameter. If b ≤ the radius of the planet Rp , then there would
be a head-on collision.
frame of reference, the planet sees the probe approach with speed
v + U . As it gets closer, the speed of the probe increases, and after
the encounter, the probe’s speed decreases until it appears to the
planet to have a speed v + U again, as shown in Figure 9.2. (We are
9.1 The Gravitational “Slingshot” 65
In the Sun’s frame of reference, then, the kinetic energy of the probe
is 21 m(v + 2U )2 . The increase of energy to the probe is 2mU (v+U ).
(By the same token, because the energy is conserved in this elastic
collision, the planet’s kinetic energy goes down by the same amount,
but the change is negligible for the planet).
In Figure 9.4 the kinetic energies of the probe and the planet are
p2
calculated (e.g., in line 11) as 2m , which is the same as 21 mv 2 . In line
27, the gravitational force is calculated, and lines 34 and 35 update
the planet’s and the probe’s positions at each instant.
2 Voyager
1, launched in 1977, is now almost 150 a.u. from the Sun and is the farthest human-
made object in existence.
68 Chapter 9 Two Capstone Projects
9.1.3 Exercises
In the previous chapters you have gone from investigating the sim-
ple motions of an object with constant velocity to exploring the
physics of a comet about the Sun. In all of these examples, the
motion did not exhibit what physicists call deterministic chaos. In
the gravitational problem of two bodies attracting one another, solu-
tions can, in fact, be found by pencil and paper. Interestingly, when
we add a third body all bets are off. The orbits can no longer be pre-
dicted with a mathematical formula, but worse than that, the orbits
exhibit what is known as extreme sensitivity to initial conditions - a
hallmark of chaos.
every time step, and this is given by the universal law of gravitation
as,
m1 m2
⃗a = −G 2 r̂1 − G 2 r̂2 , (9.1)
r1 r2
where ⃗r1 is the vector from first star to the asteroid, and ⃗r2 is the
vector from the second star to the asteroid, and the m’s in the nu-
merator are the star masses.
So here is the idea. Let us start with the code in Figure 7.1 which
will take care of the motion of the two stars. To give us a little more
“room”, let’s start these two stars out along the x-axis at -5e11 and
+5e11 (instead of ±2e11. Also, to keep the center of mass of the
system fixed, let’s adjust the y-component of the small start from
-1e4 to -9e3. Finally, turn off the trails on these two stars, and delete
all calculations of the center of mass (we won’t need that anymore).
This takes care of the binary-star system.
Now let’s introduce the asteroid. Add a line defining this object
near the top that defines the asteroid as a sphere. Give it an initial
position of vector(1.50e12,0,0). Then define a asteroid ve-
locity as one of its attribute as
asteroid.v = vector(0,-7.5e3,0).
Now that we have defined the asteroid and its initial position and
velocity, we need to turn our attention to its motion. At the end of
the while-loop, add two lines that compute the vectors ⃗r1 and ⃗r2 ,
appearing in Equation 9.1. Each of them are just the differences in
the position vectors of the asteroid and the respective star. With
these two vectors in hand, we can compute the asteroid’s accelera-
tion, asteroid.a, using Equation 9.1 directly.
72 Chapter 9 Two Capstone Projects
This program would now run, but it is a good idea to add two more
things. The first one is straightforward. If the asteroid comes too
close it burns up in the stellar atmosphere. So we should include an
if-statement to terminate the program if the asteroid comes within,
say, 2e9 (or 2 million kilometers) of either star.
Figure 9.5 shows one simple way we can accomplish this. Keep in
9.2 Asteroid near a Binary Star System - Chaotic orbits, or the
73
“Three-Body Problem”
9.2.2 Exercises
Now that you have completed the coding, let us have fun and run
the program with a few initial conditions. You should get output
like the following:
One thing that is immediately clear is that the orbits are highly ir-
regular and non-periodic. Furthermore, what you can do is to vary
the initial position of the asteroid slightly. For instance, you can
check the orbits that result from a initial x-coordinate of 1.49, 1.50,
and 1.51 × 1012 meters.
Find initial conditions that lead to the three possible long-term out-
comes for the asteroid: (a) it crashes into one of the stars, (b) it
74 Chapter 9 Two Capstone Projects
escapes the binary-star system and drifts further and further into
space, and (c) it settles into a semi-stable orbit around one of the
stars.
Conclusion
We hope that the previous chapters helped you learn the material in
your introductory physics textbook a little better by giving you the
opportunity to explore and play with the ideas in a concrete way.
We also hope that these exercises made you appreciate the power of
scientific computation. It can be an invaluable tool in finding and
visualizing solutions and also in helping us build up our physical
intuition and our conceptual understanding of physics.
Keep VPython in mind also for the second semester of your intro-
ductory physics sequence. There should be plenty of chances to use
it for, say, visualizing electric and magnetic fields and so on. In
fact, there are a number of excellent sample programs already on
the glowscript.org platform. Additional programs and resources can
be found at multiple online repositories.1
Finally, if you think you want to pursue the physics major, remem-
ber VPython can come in handy for your upper-level physics course-
work. At that time, it is likely that you have become more familiar
with scientific computing and may be comfortable using the Python
programming language. You can choose from a number of excellent
1 see,
for instance:
https://vpython.org/contents/contributed.html
https://bphilhour.trinket.io
https://phys221.wordpress.com/vpython-tutorials/
76 Chapter 10 Conclusion
2 For instance:
R.H. Landau and M.J. Páez, Computational Problems for Physics: With Guided Solutions
Using Python (CRC Press, 2018).
J.M. Kinder and P. Nelson, A Student’s Guide to Python for Physical Modeling (Princeton
U. Press, 2018).
M. Newman, Computational Physics (2013).
Bibliography