Jump Start Guide for Computational Physics

It’s the beginning of a new school year—and I’ve got you covered. You want to do something with coding in your physics class, but you don’t know where to start? I’m going to give you a jump start.

I know you are nervous, but don’t worry. You don’t need to be a ‘l33t h4x0rz’ (that’s cool-speak for elite hacker). You just need to get started. Just remember, everyone had to start programing at some point. They all did it—so can you.

What the heck do you call it?

I like to call this stuff “numerical calculations”. I think this is the best name for it because it sort of describes what’s going on. Here’s the general idea:

  • Take a physics problem (or any problem, really).
  • Break the problem into a bunch of small and easier problems.
  • Maybe make some approximations.
  • Solve all the small problems by using numbers.

Numbers are the key. You have to use numbers in a numerical calculation. The other solution is an analytical calculation. This is the process of solving a problem in terms of known functions—like the trig functions. For an analytic solution, you don’t really have to put in the numbers until the end.

Of course, there isn’t a huge difference in these two solutions (analytical vs. numerical). A great example from Bruce Sherwood (in a discussion at the recent AAPT meeting in Utah) points out the following:

Suppose you get a solution for a mass oscillating on a spring. The analytical solution will be in terms of the cosine function. But then, how do you get values for something like cos(0.33) = ? Well, you put it in your calculator or you look it up in a table. Oh, you could find the value for cosine by summing an infinite series. But you see—we are back to a numerical calculation.

That’s not exactly what Bruce said, but that’s the basic idea.

Here are some other names for numerical calculations that you might see:

  • Computational physics
  • Coding in physics
  • I’m drawing a blank here—there must be some other words.

But I also like numerical calculations because it doesn’t explicitly say “computer” in it.

Why do numerical calculations in physics?

Solving for the motion of a mass on a spring

Let me be brief and just list some points.

  • Numerical calculations are just part of physics. There are countless physics problems that can only be solved numerically.
  • Once students get the idea of numerical calculations, they can solve more interesting problems that would otherwise be inaccessible to them.
  • What about other fields? Meteorology, digital animations, protein folding, economics…the list goes on.
  • Tools. The tools for numerical calculations are both free and easy to access. You don’t need to install anything and you could even do it on a smart phone (not recommend—but possible).
  • Finally, numerical calculations helps student understand physics. I’ve always been surprised that when working on a problem with students on a computer, they ask questions. But these questions are rarely about computer syntax. They are usually things about vectors or forces. It’s awesome.

Who is this for?

I’m going to get you started—so this tutorial is geared towards very introductory classes. I use this same stuff in a physics lab for an algebra-based physics course at that college level. I think this would be fine for high school classes also.

If you want more advanced stuff—this might also work as an introduction. For my calculus-based physics course, I start with more complicated stuff.

Also, I am careful to emphasize that students (and faculty) don’t need any prior experience with coding.

Where to start

I like to have a workshop format for my lab or class. I use a projector at the front of the room to go over some points and then stop and let the students work on code either individually or in groups (here is a version of my presentation—feel free to use it). I tell students to bring a computer or tablet if possible. Otherwise they will be in groups of 4 per computer (which is not ideal). Of course some students don’t want to get involved, so a 4 person group is what they want.

Here is the general outline of the workshop format lesson.

  • Give an overview of numerical calculations (motivation).
  • Start with an object moving at a constant velocity in one dimension. Let them solve it analytically (hopefully, this is a review).
  • Next have them take this SAME PROBLEM but solve it by breaking into 7 time steps—but still solving it on paper. NO COMPUTERS YET.
  • I actually give them a table to fill out. It has 7 rows with columns for time, time step, and position. After a short time, I stop them and go over the calculation for the first row (and maybe the second). Some students can finish this table very quickly, and others not so quick.

Next, they do this same set of calculations with some python code. I give them this program that runs as it is and I go over each line.

The two parts that might be new for students:

After going over the code, I send them to this page (https://trinket.io/rhettallain_gmail_com/courses/physics-python-for-mere-mortals#/beginning-numerical-calculations/using-small-pieces). It’s a trinket.io page with the code right in the browser. They don’t even need to log in or anything. It even has all the instructions there too so that they could do this on their own. The trinket site is the BEST. Oh, I also made this shortened-url (http://bit.ly/trinket-physics). That page includes everything. I make sure to tell them to click on the “using small pieces” tab on the left to get to the code.

So, the students run the code and then modify the code to answer some questions such as:

  • Where will the car be at a different time? Say 2.2 seconds.
  • What if you change the velocity the 0.62 m/s, where will it be after 2.2 seconds?
  • What if the car starts at -0.5 meters?

Stuff like that. Really, I just want them to be able to run the code, read the output, and change the code. It’s sort of a coding ice-breaker.

I’m not going to go over the rest of the workshop—but it’s all there (and more) on the trinket.io site along with the instructor slides. After that first small activity, the students do the following:

  • A similar problem but with a constant (non-zero) acceleration. This is great because you get a different final answer for the numerical calculation that depends on the size of the time step.
  • How to make graphs (or at least print out values) so you can get more data.
  • Solving a problem with two cars—one moving at a constant velocity and one accelerating. This is the classic “police chase” problem. I set up the program (not all the way) but I let them figure out how to change the while loop to get it to run. It’s great because students come up with their own ways of making it work. Sometimes, this is where I stop the class.
  • Projectile motion.
  • Mass oscillating on a spring.

What do you need?

If you want to do this in class, you need some computers or tablets and some time. You could probably do this in sections, just break it into 30 minute activities if you like.

Some other things to consider:

  • Make sure you work through the material first. It’s important to really know what’s going on so that you can easily help students when they get stuck.
  • If a group has a program that’s not running right, I really try to get them unstuck. If it’s a silly syntax error, I try to find that right away so they don’t get frustrated.
  • If you have any questions or need help. let me know.

How Do you Put Python in your Introductory Course? EN 01

This is just a resource page for my talk at AAPT. Here are some links (in order of appearance)

Numerical Calculation for Work Done Near a Dipole

I’ll be honest. This connection between the electric potential (change in electric potential) and the electric field can get sort of crazy. But let’s just start with a problem and then solve it in more ways than you wanted.

Here is the problem.

Let’s start with the energy to bring an electron to point B. The energy needed would be equal to the change in electric potential energy which is equal to:

\Delta U_E = q\Delta V

That means I just need to calculate the change in electric potential from infinity to point B. Yes, you could also calculate the work needed to move the charge—I’ll do that also.

Since I am dealing with two point charges, I can use the following expression for the potential due to a point charge (with respect to infinity):

V = k\frac{q}{r}

Where k is the Coulomb constant (k = 9 \times 10^9 \text{ Nm}^2\text{/C}^2 and r is the distance from the point charge to the final location. Since there are two point charges, the total potential will just be the sum of the two potentials. Let me call the positive charge “1” and the negative charge “2”. That means the total potential will be:

V = V_1 + V_2 = k\frac{q_1}{r_1} +k\frac{q_2}{r_2}

From the original problem, q_1 = 2 \times 10^{-9}\text{ C} and q_2 = -2 \times 10^{-9}\text{ C} . The distance r_1 will be 6 mm and the distance r_2 will be 4 mm (need to convert these to meters).

Putting this all together, I get the following. I will do my calculations in python. Here is the code.

Running gives the following output.

Boom. There is your first answer. What about point A instead of B? Well, in this case, I just have different distances. The distance for both r_1 AND r_2 are the same. Since they have the same distances but equal and opposite charges, the two potentials will be opposite. When added together, the total potential is zero volts. Yes, the energy needed to put a point charge at A from infinity is zero Joules.

What? Yes. How about this? Suppose you take the electron from infinity on the positive y-axis. As you move down the axis to point A, the electric field is in the x-direction. That means the electric force is in the negative x-direction. You would have to push it in the positive x-direction and move in the y-direction. But that requires ZERO work since the force and displacement are perpendicular.

Oh. You want to get to A from a point of infinity on the positive x-axis? OK. That still works. Remember that for electric potential, the path doesn’t matter—only the change in position (path independent). I can take whatever path I like. I’m going to move in a circle from positive infinity x to positive infinity y. The electric field is zero out there, so that requires zero work. Now I’m at positive infinity y—and I just did that problem. Zero work.

Another way—by calculating the work

Remember that work-energy principle? It says this:

W = \Delta E

And the work can be defined as the following (if the force and displacement are constant):

W = F\Delta s \cos \theta

Oh, and the force will be the opposite of the electric force where:

\vec{F} = q\vec{E}

So, as you push a charge towards point B (point A is boring—for now) the electric field changes. That means we have a problem. We can’t use the above formula to calculate the work—unless we cheat. Let’s cheat.

Instead of calculating the total work to move the charge to point B, I’m just going to move it a tiny bit. During this tiny move, the electric field (and thus the force) will be approximately constant. Then I can do another tiny move. Keep repeating this until I get to point B. Here is a diagram.

If this distance is short (\Delta \vec{s}) then the force is approximately constant. That means the tiny amount of work (which I will call \Delta W) would be equal to:

\Delta W = eE\Delta s

OK, just to be clear. This is the force needed to PUSH the electron (with a charge e)—it’s not the electric force on the electron (which is in the opposite direction). Also, the angle between F and the displacement is zero. That means the cosine term is just one. I wrote the force and displacement as scalars because it’s just the magnitude that matters.

Now we are ready for some stuff. Here are the steps I am going to use.

  • Start at some position far away (but not actually infinity because that would be crazy). It just needs to be far enough away such that the electric force is negligible.
  • Calculate the total electric field and the force needed to push the electron at this point.
  • Move some short distance towards point B.
  • Over this distance, assume the force is constant and calculate the small work done—add this to the total work.
  • Repeat until you get to point B.

Before making this one program, I’m going to just make a program to plot the electric field from some value up to point B. Here is the plot from that program. (here is the code)

Note that I started from just 5 cm away from the origin—which is TOTALLY not infinity. However, it makes the graph look nice. But still, this is good because it looks like the calculation is working. Now I can use this same calculation go find the work needed to move the electron. Here is the code.

And the output:

Notice that gives a close, but wrong answer (compared to my previous calculation). Why is it wrong? Is it because I started at y = 0.5 meters (I just realized I’ve been using the variable y instead of x—but it should be fine). Or is it wrong because my step size is too big?

The answer can be found by just changing up some stuff. If you move the starting point to 1 meter, you get about the same answer. However, if you change dy to 0.0001, you get the following output.

That works. Oh, I added some more stuff to the output.

Non-straight Path

One more thing (and then I will look at the electric field in another post). What if I use a different path to get to point B? Instead of coming along the x-axis (which I previously called “y”), I come parallel to the axis a distance of 2 mm above it. Once I get right over point B, I turn down.

Like this.

This introduces some “special” problems.

  • I can break this path into two straight pieces (path 1 is parallel to x-axis and path 2 is parallel to y-axis).
  • Along path 1, the force needed to push the electron is NOT parallel to the path. So, the angle is not zero in \cos \theta. This means I’m going to have to calculate the actual vector value of the electric field at every step along this path.
  • The same is true along path 2.
  • But in the end, I should get the same work required—right?

OK, hold on because this is going to get a little more complicated. Let me just include one sketch and then I will share the code for this new path. Here is how to calculate the electric field and work for a particular step in path 1.

Here’s what needs to happen to calculate the electric field (and force) for each step:

  • Find the vector from the positive charge to step location.
  • Use this vector to find a unit vector (to give the electric field a direction).
  • Use that vector to also find the magnitude of the electric field.
  • Calculate the electric field due to the positive charge (as a vector).
  • Repeat this for the negative charge.
  • Add the two vector values for the electric field to get the total electric field.
  • Multiply by the charge to get the force (which would be in the opposite direction).

Now, to calculate the work done during each small step, I could use the angle between the force and displacement. But I don’t know that. Instead, I can use the vector definition of work:

W = \vec{F} \cdot \Delta \vec{s}

Yes, that is the dot product. Fortunately, the dot product is already built into VPython (Glowscript). So, once I get a vector value for the force and the displacement I can just use the “dot()” function.

OK, let’s do it. Here is the code (warning—vector stuff in the code) and the output.

Wow. I didn’t think that would work the first time. I’m pumped.

OK, the real reason for this post was to look at the connection between the electric field and the change in electric potential. I’ll make that in a follow up post.

RC Circuit as an Example of the Loop Rule

Batteries and bulbs are fun, but they can only go so far. How about a capacitor and a bulb? Yes, let’s do that.

Here is the setup.

This has a battery (2 1.5 volt batteries) connected to a 1 Farad capacitor with a switch. This capacitor is then in parallel with a light bulb. When the switch is closed, the capacitor is charged up to 3 volts. When the switch is opened, the capacitor discharges through the bulb. Notice how it slowly gets dim.

Here, I even made this same (almost the same) circuit in a PhET simulator (java warning).

Of course the full circuit doesn’t really matter. I don’t care about charging the capacitor, just the discharging. So here’s the important part.

Let’s start off by applying the Loop Rule to this circuit. If I start from the lower left corner and then go around counterclockwise, I get the following. Oh, I’m assuming zero resistance in the wires.

\Delta V = \frac{Q}{C} - IR=0

Where the voltage across the capacitor depends on the charge.

\Delta V_C = \frac{Q}{C}

But wait! The current is the flow of charge. Since there is a current, the will be a decrease in charge on the capacitor. A decrease in charge means there will be a lower voltage. This lower voltage makes a smaller current. Maybe you can see the problem. Don’t worry, we can still solve this.

Let’s create a numerical calculation to model the current running in this circuit. The key here is to break the problem into very small time steps. Let me start by using the loop rule and using the following definition of electric current.

I = \frac{\Delta Q}{\Delta t}

Now the loop rule looks like this.

\frac{Q}{C} - \left(\frac{\Delta Q}{\Delta t}\right)R = 0

If I use a very small time step, then I can assume that during this time interval the current is constant (it’s not, but this isn’t a bad approximation). From this I can solve for the change in charge.

\Delta Q = \frac{Q}{RC} \Delta t

But what does this change in charge do during this time interval? Yup, it decreases the charge on the capacitor—which in turn decreases the capacitor voltage—which in turn decreases the current. I think I already said that.

After this short time interval, I can find the new charge on the capacitor.

Q_2 = Q_1 - \Delta Q

Note: the minus sign is there because the current DECREASES the charge on the capacitor.

That’s it. We are all set. Here is the plan. Break this problem into small time steps. During each step, I will do the following:

  • Use the current value of charge and the loop rule to calculate the change in charge during the time interval.
  • Use this change in charge to update the charge on the capacitor.
  • Repeat until you get bored.

OK. Suppose I am going to do this. I decide to break the problem into a time interval of 0.001 seconds. How many of these intervals would I need to calculate to determine the current in the circuit after 1 second? Yes. That would be 1000 intervals. Who wants to do that many calculations? I sure don’t.

The simplest way to do this many calculations is to train a middle school student how to do each step. It shouldn’t be hard. Oh wait, the middle school student is still busy playing Fortnite. Oh well. Maybe I will train a computer to do it instead. Yes, that’s exactly what I will do.

In this case, I’m going to use python—but you could use really any programming language (or even no computer programming language). The idea of a numerical calculation is to break a problem into small steps. The idea is NOT to use a computer. It just happens that using a computer program makes things easier.

Here is the code (below is just a picture of the code—but you can get it online too).

Let me just make a couple of comments on different lines.

  • Line 4,5 just sets up the stuff to make a graph. Graphing in super easy in this version of python (Glowscript).
  • Line 14 is the length of the time interval. This is something you could try changing to see what happens. Yes, if you use the trinket.io link above, you can edit the code.
  • Line 21 looks tricky. It looks like Q will cancel in that equation. Ah HA! But that’s not an algebraic equation. In python, the “=” sign means “make equal” not “it is equal”. So this takes the old value of Q and then updates it to the new Q.
  • Line 25—same thing happens with time. You have to update time or the loop will run FOREVER!
  • Line 26. This is how you add a data point to the graph.

This is what you get when you run it.

OK. That looks nice. As we see in real life, the brightness of the light bulb dims rapidly at first and then slowly dies down. This plot seems to agree with actual data (always good for a model to agree with real life).

But what does the textbook say about a circuit like this (called an RC circuit because it has a capacitor and a resistor in it)? Note: this is an algebra-based physics textbook. It gives the following equation for a discharging capacitor.

I(t) = \frac{V_0}{R} e^{-t/\tau} \tau = RC

In this case the V_0 is the initial voltage on the capacitor. Well, then let’s plot this solution along with my numerical calculation. Here is the code https://trinket.io/glowscript/f4a3ff8264—and I get the following plot.

Those two plots are right on top of each other. Winning. Oh, go ahead and try to change the time step. Even with a much larger step, this still works.

Some final notes. Why? Why do a numerical calculation?

  • Numerical calculations are real. They are used in real life. There are plenty of problems that can only be solved numerically.
  • I think that if physics students create a numerical calculation, they get a better understanding of the physics concepts.
  • What if you want to treat the bulb as a real lightbulb? In that case the resistance is not constant. Instead, as the bulb heats up the resistance increases. With this numerical calculation you should be able to modify the code to account for a real bulb. It would be pretty tough if you solved this analytically.
  • What is the point of having students (in an algebra-based course) memorize or even just use the exponential solution for an RC circuit. It might as well just be a magic spell if you just use the equation. I don’t really see the point. However, with the numerical calculation the students can do all the physics.

Projectile in Polar Coordinates

Why?  There is no why.

Suppose a ball is shot at an angle θ with an initial velocity v0.  What will the motion be like in polar coordinates?

First, let me start with Newton’s 2nd Law in polar coordinates (I derived this in class).

F_r = m(\ddot{r}-r\dot{\theta}^2)
F_\theta = m(r\ddot{\theta}+2\dot{r}\dot{\theta})

Once the ball is in the air (and ignoring air resistance) the only force on the ball is the gravitational force. Yes, this would be -mg in the y-direction, but we don’t have a y-direction. Instead, we have polar coordinates. Maybe this picture will help.

Photo - Google Photos 2018-02-18 08-55-07.png

The r and θ components of the gravitational force will change as:

F_r = -mg\sin\theta
F_\theta = -mg\cos\theta

If I use these forces with Newton’s law in polar coordinates, I get:

F_r = -mg\sin\theta=m(\ddot{r}-r\dot{\theta}^2)
F_\theta = -mg\cos\theta = m(r\ddot{\theta}+2\dot{r}\dot{\theta})

Of course the mass cancels – but now I can solve the first equation for \ddot{r} and the second equation for \ddot{\theta}.

\ddot{r} = r\dot{\theta}^2-g\sin\theta
\ddot{\theta} = \frac{-2\dot{r}\dot{\theta}}{r} - \frac{g\cos\theta}{r}

It doesn’t matter that these second derivatives depend on the other stuff – I can still calculate them. Once I have those, I can create a numerical calculation to update the velocities and positions. Suppose I have everything know at time(1), then the stuff at time(2) would be:

\dot{r}_2 = \dot{r}_1 +\ddot{r}\Delta t
\dot{\theta}_2 = \dot{\theta}_1 +\ddot{\theta}\Delta t
r_2 = r_1+\dot{r}\Delta t
\theta_2 = \theta_1 +\dot{\theta}\Delta t
t_2 = t_1 +\Delta t

Now I’m all set to do a numerical calculation. Well, I still need the initial conditions. I could use this:

\dot{r}_0 = v_0
r_0 =0
\dot{\theta}_0 = 0
\theta_0 = \theta_0

But wait! There’s a problem. The calculation for \ddot{\theta} has a 1/r term. If r is zero the universe will explode. I can fix this by having the initial r a little bit bigger than zero. Problem solved.

Here is the program in Glowscript.

For the first run, I am going to calculate the x- and y-coordinates in each step and plot x vs y.  I know this should look like a parabola.

newplot (2).png

Boom.  That works.  Now for a plot of both r and theta as a function of time for a high launch angle.

newplot (3).png

Double boom.

Electric Field due to a Uniformly Charged Ring

Hold on to your pants. Let’s do this.

Suppose I have an electrically charged ring. The radius of this ring is R and the total charge is Q. The axis of the ring is on the x-axis. What is the value of the electric field along this x-axis?

Analytical Calculation

You can’t directly find the electric field due to a charge distribution like this. Instead, you have to break the object into a bunch of tiny pieces and use the superposition principle. The super position principle says that the total electric field at some point is the vector sum of the electric field due to individual point charges.

Of course the electric field due to a single point change can be found as:

\vec{E}=\frac{1}{4\pi \epsilon_0} \frac{q\hat{r}}{r^2}

So, if I break this ring into a bunch of tiny points I can find the electric field due to each of these points and add them up. Yes, the smaller the points the better the answer. In fact, if I take the limit in which the point size goes to zero I will turn this into an integral. Calculus for the win (CFTW).

Let’s take a look at one of these pieces on the ring of charge. Here is a view of the ring from the side.

I can find the little bit of electric field from this little bit of charge at the top of the ring. This electric field would be:

d\vec{E}=\frac{1}{4\pi\epsilon_0}\frac{dq\hat{r}}{r^2}

Of course the direction of r and thus the electric field will change directions as you go around this ring and add up tiny electric fields. But WAIT! We can cheat. OK, it’s not cheating–it’s just being smart. What if you also consider the tiny piece of charge at the bottom of the ring? This would also make a tiny electric field at this same location. It would have the same magnitude as the electric from the top piece since it’s the same charge and distance. However, the y-components of these two electric fields would cancel and leave only an x-component of the field.

In fact, for every tiny piece on the ring there is a corresponding piece on the opposite side of the ring. The only surviving components of electric field will be in the x-direction. So that’s all we need to calculate.

Looking at the diagram above, I can find the x-component of this tiny electric field by using the angle θ (which I don’t explicitly know). However, I can still write down this new x-component of the tiny electric field.

dE_x = \frac{1}{4 \pi \epsilon_0} \frac{dq}{r^2}\cos\theta

Yes, this is no longer a vector. It’s the x-component of the electric field such that it’s just a scalar. But there’s still a problem. I want to add up (thus integrate) all these tiny electric fields due to the tiny charges. However, I have these two variables that I don’t like. There is θ and r. I need to replace those.

For the r, it’s clear that I can use R and x and the pythagorean triangle to get:

r^2=R^2+x^2

I can also replace the θ by again using the right triangle to write:

\cos \theta = \frac{x}{\sqrt{R^2+x^2}}

Updating the tiny x-electric field:

dE_x = \frac{1}{4 \pi \epsilon_0}\frac{dq}{R^2+x^2}\frac{x}{\sqrt{R^2+x^2}}

This simplifies to:

dE_x = \frac{1}{4 \pi \epsilon_0}\frac{xdq}{(R^2+x^2)^{3/2}}

Now we need to get a better integration variable—we can’t integrate over dq. Or can we? Yes, we can. Take a look at the equation above. As you move around the circle, which of those variables change? The answer: none of them.

As I integrate over dq, I just get the sum of the dq’s—which would be Q. Boom. That’s it. Here is my final expression for the x-component of the electric field along the x-axis of this ring.

E_x =  \frac{1}{4 \pi \epsilon_0}\frac{xQ}{(R^2+x^2)^{3/2}}

Now for a couple of checks on this result. I will let you make sure these are ok.

  • Does this expression have the correct units for electric field?
  • What about the limit that x>>R? Does this expression look like the field due to a point charge?
  • What is the electric field in the center of the ring?

One last thing to think about. What if you want to find the electric field at some location that is NOT on the x-axis? Then the above derivation wouldn’t work. Too bad.

Numerical Calculation

Let’s do this again. However, this time I am going to create a numerical calculation instead of an analytical calculation. What’s the difference?

  • The analytical calculation takes the sum of charge pieces in the limit as the size of the pieces goes to zero.
  • The numerical calculation uses numerical values for a finite number of pieces to calculate the electric field.

That’s really the only difference.

OK, let’s just get into this. I’m going to give you a link to the code and then I will go over every single important part of it. Honestly, you aren’t going to understand this code until you play with it and probably break it.

Here is the code on trinket.io. Sorry—I can’t embed the code in this wordpress blog (I wish I could).

This is where I start.

k=9e9
Q=10e-9
R=0.01

I like to start with some constants and stuff. Here I am using k for the constant. I also had to pick a value for the total charge and the radius of the ring. Remember, you can’t do a numerical calculation without numbers.

Now let’s make the ring.

cring=ring(pos=vector(0,0,0),axis=vector(1,0,0), radius=R,thickness=R/10, color=color.yellow)

Some notes.

  • This isn’t really needed for the calculation—but it allows us to make a visualization of the thingy.
  • “ring” is a built in object in VPython (glowscript).
  • The important attributes of this object are the position (pos), the axis—which is a vector in the direction of the axis of the ring. Radius and thickness and color should make sense.

For the next part, I need to break this ring into pieces. If I use N pieces, then I can find the location of each of the tiny charges by using an angle to indicate the location. I also need to find the charge on each tiny piece. Here is the important code.

N=20
theta=0
dtheta=2*pi/N
dq=Q/N

The “dtheta” is the angular shift between one piece and the next. It’s like a clock. It’s a clock that only ticks N times and doesn’t really tell time. But each “tick” is another tiny charge. Maybe that’s a terrible analogy.

But how do we deal with all these tiny charges? What if N is equal to 50? I don’t want to make 50 variables for the 50 charges. I’m too lazy. So instead, I am going to make a list. Lists in python are super awesome.

I won’t go into all the details of lists (maybe I will make tutorial later), instead I will just show the code and explain it. Here’s where I make the tiny charge pieces.

points=[]

while theta<2*pi:
  points=points+[sphere(pos=R*vector(0,cos(theta),sin(theta)),radius=R/8)]
  theta=theta+dtheta

Some notes.

  • First, I made an empty list called “points”.
  • Next I went through all the angular positions around the ring. That’s what the while loop does.
  • Now I create a sphere at that angular position and add it to the points list.
  • Update theta and repeat.
  • In the end I have a list of points—the first one at points[0].

Next part—make the observation location. This is the spot at which I will calculate the electric field.

obs=sphere(pos=vector(.03,0.03,0), radius=R/10)
E=vector(0,0,0)

The “E” is the base electric field, it starts at the zero vector.

Now for the physics.

for p in points:
  r=obs.pos-p.pos
  dE=k*dq*norm(r)/mag(r)**2
  E=E+dE

print("E = ",E," N/C")

Here you can see the full power of a list. Once I make the list of tiny charges, it is very simple to go through the list one tiny charge at a time—using the “for loop”.

Essentially, this loop does the following:

  • Take a tiny charge piece.
  • Find the vector from this piece to the observation location
  • Find the tiny component of the electric field using the equation for a point charge.
  • Add this tiny electric field to the total electric field and then move on to the next piece.

Boom. That’s it. Print that sucker out. Maybe you should compare this electric field to the analytical solution. Oh wait. There’s a bunch of homework questions. Actually, I was going to do some of these but this post is already longer than I anticipated.

Homework

  • Pick a value of N = 10 and an observation location of x = 0.1 meters. How well does the analytical and numerical calculations agree? What if you change to N = 50? What about N = 100?
  • Create a graph that shows the magnitude of the electric field as a function of x (along the ring axis). In this graph include the analytical solution and plots for N = 10, 30, 50, 100.

Actually, I wanted to make that last graph. It would be great.

Oh wait! I forgot about the most important thing. What if I want to calculate the electric field at a location that is NOT on the x-axis? Analytically, this is pretty much impossible. But it’s pretty easy with a numerical calculation. Here’s what that would look like.

Oh, if you like videos—here is the video version of this post.

Analysis of a borked lab

It happens all the time. It even happens to you. There is a new lab you want to try out—or maybe you are just modifying a previous physics lab. You are trying to make things better. But when the class meets—things fall apart (sometimes literally).

Yes. This is what happened to me this week. And yes—it’s OK.

But let’s look at the lab and go over the problems so that I can make it even better for the future.

Finding the electric field due to a point charge

This is a lab for the algebra-based physics course. It’s always tough because many of the first things they cover in the lecture class don’t have lab activities with things you can measure. Oh sure—there is that electrically charged clear tape lab, but it will be a while before they get to circuits.

So, my idea was to have the students use python to calculate the electric field due to a point charge. This would give them a safe and friendly introduction to python so that we could use it later to get the electric field due to other things (line a dipole or a line charge). It would be great.

Here is the basic structure of the lab (based on this trinket.io stuff that I wrote – https://rhettallain_gmail_com.trinket.io/intro-to-electric-and-magnetic-fields#/introduction/vector-review

You can look at that stuff, but basically I give a workshop style presentation and have the students do the following:

  • Review vectors. Add two vectors on paper (not with python).
  • Find the displacement vector – given the vector for a point, find the vector from that point to another point (the vector r).
  • Find the unit vector and the magnitude of a vector (using python).
  • Next, find the electric field due to a point charge for the simple case with a charge at the origin and the observation point on the x-axis. Do this on paper.
  • Now do the same calculation with python.
  • Find the electric field at some location due to a charge not at the origin (in python).
  • Use python (or whatever) to make a graph of the electric field as a function of distance for a point charge. Graph paper is fine. If they wanted to, they could do the calculations by hand (or use python).
  • Finally, give a quick overview of the sphere() and arrow() object in glowscript.

So, that was the plan.

Lab problems

Here are the problems students had during this lab.

  • Computer problems. Yes—whenever using computers, someone is going to have a problem. In this case, it was partly my fault. There was one computer that was broken and some other ones weren’t updated. Honestly, the best option is for students to bring their own.
  • I can see that there are some students that just sort of “shut down” when they see computer code. They automatically assume it’s too complicated to grok.
  • Students working in big groups. I hate having 4 students use one computer. That’s just lame.
  • Too much lecture. The first time I did this, I spent too much time going over vectors with not enough breaks for students to practice. I partially fixed this for the second section of lab.
  • Some students were just lost on vectors.
  • Yes, the unit vector is a tough concept.
  • I’ve learned this before—but I guess I need to relearn. The visualization (sphere and arrow) are just too much for many students. That’s why I moved it to the end in my second section.

So, that’s it. I am going to rewrite the lab stuff on trinket.io. I am also going to change my material for the dipole stuff that they are doing next week. Hopefully it goes well. Let’s just see.

MythBusters Jr. Slinky Drop Stuff

Tonight’s episode (actually there are two episodes tonight) looks at the famous slinky drop problem. Let’s start with the first place I saw this—from Derek Muller (Veritasium) even though he didn’t invent this either.

That’s pretty awesome, right? Of course the first thing I want to do is to make a model of a falling slinky. Here is that first post.

Some important comments.

  • It’s important that the slinky itself has mass. You can’t use the normal assumption of a massless spring.
  • The best way to model a spring with mass is to have a bunch of smaller masses connected by massless springs.
  • When the slinky is dropped, the center of mass falls with a downward acceleration of -9.8 m/s^2.
  • However, since the slinky is contracting this makes the bottom of the slinky motionless.
Untitled 2

Here is an animation of my python model.

Slinky 3

Sorry—this code is older and I don’t have it on some online platform (I will try to update that soon). But here is the important plot. What if you look at the vertical position of the top, bottom and center of mass for this “slinky”? Here’s what that looks like.

Slinkydrop 2.png

The red curve is the bottom mass. Notice how it “hangs” there? Awesome.

But can you just put a mass (like a car) on the end of a spring and drop it? Yes, but it won’t look very cool. The key is the center of mass. You want the center of mass to fall such that the bottom mass stays in place. With a car and a spring, there is no top mass moving down faster than the acceleration due to gravity to make the bottom mass (the car) move up relative to the center of mass.

In the end, you need some type of mass at the top of the spring too. So, that could work. Two large masses separated by a spring. When you hang and then drop, the bottom mass will be stationary.

But wait! You can try this yourself. Get two masses and connect them with rubber bands (even though rubber bands aren’t ideal springs). Hold one mass and let the other hang below. Now drop.

Here’s what that looks like in slow motion. Sorry about the vertical video, when I recorded this I didn’t think I would post it.

Pretty awesome.

But wait! What if you want to make something like the slinky? In that case you can get a bunch of masses and connect them with rubber bands. It will be just like the python animation above, but in real life.

I should have recorded this in slow motion. Oh well.

Just for fun, here are some of my original notes in which I estimate what kind of spring you would need to do this drop thing with two cars.

All About Rhett: A Resume

Who is Rhett? Where is he from? What are his super powers? Why do you want to contact Rhett for your project? Let’s answer these questions.

Super Brief History

How about just the highlights? Yes.

  • Born in the past, not the future.
  • B.S. in physics from Benedictine University (although it was called I.B.C. back then).
  • M.S. in physics from the University of Alabama (including work at CERN).
  • Ph.D. in physics from NC State University.
  • Some other stuff here.
  • Associate Professor of Physics at Southeastern Louisiana University.

Stuff I Do: Blogging

Yes, I like to write. Blogging is the best (for me). It’s quick and free form. You can use a variety of tools (video, gif, graph, python). It’s just the best. THE BEST!

I should probably include some of my favorite posts—but there are just too many that I love. If you want a post on a particular topic (like video analysis)—just google “rhett allain video analysis” and that should get you what you need.

Here is my WIRED blog stuff.

Oh, I also blog here—but I guess you already know that.

Stuff I Do: MacGyver

I am currently the technical consultant for the CBS show MacGyver. It’s a super fun and awesome job. But what do I do? Essentially, I help out with the “MacHacks” in the show. I look over the hacks and see if they are at least plausibly based on some real thing. Other times, I make suggestions for hacks. Finally, I offer suggestions for some of the science-type stuff that MacGyver says.

Oh, I also make DIY home-versions of hacks. Here is an example.

As a bonus, here is an interview I did with CBS KPIX out of San Fransisco on my work with MacGyver.

Stuff I Do: MythBusters

I’ve actually been working on MythBusters much longer than I have worked for MacGyver. I worked on about the last 4 seasons of the original MythBusters and then I did the reboot with Jon Lung and Brian Louden. Finally, I worked on MythBusters Jr.

So, what do I do? Really, there are two things. First thing is to do some background check on future myths. For some myths, I do some estimations and calculations to see if there is a chance of a myth working. You don’t want something that is either too easy or too hard, it should be just right.

The second thing I do is the science explanations. When there is a short explanation about how something works, I help with that.

Stuff I Do: Books

Writing books isn’t as much fun as blogging, but there it is. Here are the books I have authored.

I could talk about books more—but I’m just going to leave it at that.

Stuff I Do: Python and Numerical Calculations

It’s not my fault. If you want to blame someone—how about Bruce Sherwood and Ruth Chabay. It is through their textbook, Matter and Interactions (Wiley), that I was introduced to python and numerical calculations. It’s awesome.

Once you start solving problems by coding, it sort of gets addictive. I’ve been creating python programs since about 2003 (just a guess). Later I started putting more and more of it in my classes—and here we are.

I’m not an expert coder, but I am an expert at implementing this stuff in introductory courses. That’s what I do. I have even held workshops for teachers and educators. It’s fun.

If you want to look at the stuff I’ve done, here are three versions (all online).

  • Physics Python for Mere Mortals. This is an online type book with embedded code. Designed for intro students with zero previous programming experience and some physics. This is what I use in my lecture and lab courses.
  • Introductory Physics with Python. This is more like a full book. It’s incomplete, but the idea is to teach physics using python. I want to work on this some more.
  • Numerical Calculations in Physics. I wrote these as tutorials for students in the calculus based physics course. It uses more vectors and stuff than the over stuff.

Again, I could list a BUNCH more stuff on python but I won’t.

Stuff I Do: Talks

I’ve done quite a few talks. Instead of listing all my talks (or my favorite talks), I’m going to list my favorite topics:

  • The physics of superheroes.
  • The physics of Star Wars and science fiction.
  • Video Analysis (real vs. fake videos).
  • The best physics models (python and stuff) and estimations.
  • Science communication (blogging, MythBusters, MacGyver).
  • The physics of video games.
  • Learning about physics learning (education stuff).
  • Physics demos.
  • Physics and python.

Other Videos

I’m not a huge video guy, but I do make videos. Here is one that turned out better than I expected. It’s a video made at WIRED.

Here is another one that I made (from my youtube channel).

Here is one more. I like to make videos that show physics solutions.

Wait. One more. This one is a more advanced video.

The End.

Contact me if you want to work on something. I’m always looking for extra jobs.

Intro to Chaos in Mechanics

This is really just for me so that I won’t forget.  I mean, I will forget—but then I can look back at this post and remember stuff.  Here’s to you Future Rhett.

What is a chaotic system?  Really, that’s the question—isn’t it?  There is the classic example of the double pendulumHere is some code for a double pendulum. And this is what it looks like.

Nov-11-2018 15-57-59.gif

But this isn’t the best system.  The problem is that there are two coordinates—the angle for the top bar and the angle for the bottom bar.  Sure, it’s cool—but what if you want to plot angle vs. time or something.  You have to plot both angles vs. time and that’s a bummer.

OK, how about a model of bounded population growth?  That’s just one dimensional, right?  Actually, it doesn’t even have to be population, it’s just an equation—something like this.

x_{n+1} = 4rx_n(1-x_n)

In this expression, r is some parameter—it really doesn’t matter what.  Let’s just model this expression for different values of r.  I’ll use a starting x value of 0.1 and r values of 0.7 and 0.9.   Here is the code.

GlowScript IDE 2018-11-11 17-55-15.png

Notice that when r = 0.7, the population reaches some stable value—but this is not true for r = 0.9.

Bifurcation Diagram

Now for another way to look at a chaotic systems—the bifurcation diagram.  Honestly, I didn’t really understand these things until I made one.  Here’s what we are going to do.

  • Start with some initial value of x (just pick something—I’m going to use 0.5).  Pick a value for r also.  Let’s just start at 0.1.
  • Run the model for 200 iterations and throw out that data.  This should allow us to look at the long term behavior for that particular value of r (throws out the transient behavior).
  • Now run the model for 100 additional iterations and save these.
  • Create a plot of these final x values vs. r.
  • Next increase the r value a little bit (I will increase it by 0.001)
  • Repeat until you get bored.

So if the model is stable after the initial stuff, then it will just keep plotting the same value of x after the first 200 iterations and you will just get a dot.  If it’s not stable after the first stuff, then you will get a bunch of dots with different x values.

OK, let’s do it.  Here is the code.  Oh, I made a function to iterate the model.  I probably should put more comments in there.

This is what it looks like.

GlowScript IDE 2018-11-12 09-08-29.png

Up to an r value of about 0.75, you only get one final x value.  After that, you get two different values . With r over 0.9, it gets crazy.

OK, that’s enough for now.  I just want to make sure future Rhett knows how to make a bifurcation diagram.