This Exercise Set has been submitted for peer review, but it has not yet been accepted for publication in the PICUP collection.

Magnetic deflection (a.k.a. Magnetic Spectrometer)

Developed by Chris Orban

This exercise illustrates a charged particle being accelerated through two charged plates, after which point it moves into a magnetic field where it will experience a magnetic force (sometimes called the Lorentz force). The student will modify the code to include the magnetic force, which is a fun thing to do because running the code will prove that the magnetic force produces circular trajectories without using any calculus! This exercise will use a programming language called [p5.js]( that is very similar to C and C++ programming. (Note: If you are familiar with C or C++ the main difference you will see is that there is no main() function and instead the draw() function serves this role.) **Importantly, this exercise can be completed using any computer or chromebook without downloading any software!** This exercise is designed for an algebra-based physics class at the college or high school level. It may also be useful for calculus-based physics for non-majors (e.g. engineering & science majors). This exercise is part of a series of exercises developed by Prof. Chris Orban. There are pre-and-post assessment questions associated with this exercise (not available here) that are being used in an educational research study. If interested to collaborate on that study please e-mail Prof. Chris Orban ( The first paper from this study [is available at this link](, the second paper which discusses the electromagnetism exercises [is available at this link](
Subject Area Electricity & Magnetism
Levels High School and First Year
Available Implementation Javascript
Learning Objectives
1. Students will gain intuition for how charged particles behave as they travel through a uniform magnetic field 2. Students will modify a code to incorporate the acceleration due to the magnetic force on a moving charged particle 3. Students will use this code demonstrate that charged particles in a uniform magnetic field follow a circular trajectory (a task that otherwise requires a page of calculus to prove) 4. Students will calculate the gyroradius for charged particles in a uniform magnetic field and show that the radius of the circular trajectory from the code does match well with this calculation. This is an example of what physicists do when they "verify" a code. 5. Students will check that the speed of the charged particle moving in the uniform magnetic field stays (approximately) the same. In principle the speed should stay exactly the same, but numerical errors can affect this. This is another example of what physicists do when they "verify" a code.
Time to Complete 60 min
You may recall [an earlier programming exercise]( that involved a [particle being accelerated through two charged plates]( In this exercise we will add a magnetic field to the right hand side of the simulation. The particle only experiences this field when it passes into the right-hand side. This exercise is loosely based on the idea of a [mass spectrometer]( The particle accelerated into the magnetic field is always positive. Specifically it is some unknown molecule that has lost one electron and we are trying to measure its mass in order to identify it. Since the molecule has lost an electron it has a +1 charge. The mass of the molecule is essentially the same even if it missing an electron because electrons make up a very, very small fraction of the mass of atoms. As you will learn about in this lab, the trajectory of the particle in the magnetic field tells us something about the mass of the particle (if we already know q, v and B, for example). Step 1. Check out the modified version of the earlier programming lab by clicking on this link. As you can see there is a magnetic field pointing into the page on the right hand side. The positively charged particle does *not* react to this magnetic field simply because no one has told the program what to do when a magnetic field is present! Your task will be to tell the program what to do in this case. Step 2. Open up the bfield code in an editor Click on this link to open the bfield code in a p5.js editor Important! Create an account with the editor or sign in to an existing account. Then click "Duplicate" so you can have your own version of the code. Note: You cannot use your code from earlier exercises because those exercises don't have the capability to draw the magnetic field, and there are a few other minor formatting differences. Step 3. Before doing any coding, what is the direction of the magnetic force on the positive charge when it enters the magnetic field as shown below? (Note: The B field is into the page on the right.) ![](images/MagneticSpectrometer/no_deflect2.png "") Step 4. What happens later on, after the charge has been deflected? What should the overall trajectory qualitatively look like and why? Write down what your expectation is in what you turn in for this lab. It's ok if your expectation turns out to be wrong. You won't lose any points. Step 5. (You don't really have to do anything for this step except to try and understand the conclusion.) On the right hand side of the simulation, the only force the charge feels is the magnetic force. Since this is the only force this means that if we can figure out the x and y components of the magnetic force then we can figure out the x and y acceleration: $$\sum F_x = F_{{\rm mag},x} = m a_x \nonumber $$ $$\sum F_y = F_{{\rm mag},y} = m a_y \nonumber $$ $$\vec{F}_{\rm mag} = q \, \vec{v} \times \vec{B} \nonumber $$ In Physics 1251 we often write that the magnitude of the magnetic force is this: $$ F_{\rm mag} = | \vec{F}_{\rm mag}| = q |\vec{v}| \, |\vec{B}| \sin \theta = q v B \sin \theta \nonumber $$ where $\theta$ is the angle between the velocity and the magnetic field. In this case, the angle between the velocity vector and the magnetic field is always 90 degrees (think about why this is true) which means that the magnitude of the magnetic force is just this: $$F_{\rm mag} = q v \, B = q \sqrt{v_x^2 + v_y^2} \, B \nonumber $$ This equation is great except that what we really need is $F_{{\rm mag},x}$ and $F_{{\rm mag},y}$. Ultimately, we will find that the following is true: $$F_{{\rm mag},x} = q v_y B \nonumber $$ $$F_{{\rm mag},y} = -q v_x B \nonumber $$ For example, notice that $$F_{\rm mag} = \sqrt{F_{{\rm mag},x}^2 + F_{{\rm mag},y}^2} = q \sqrt{v_x^2 + (-v_y)^2} \, B = q \sqrt{v_x^2 + v_y^2} \, B = q v B \nonumber $$ as expected. Here is the formal derivation of $F_{{\rm mag},x}$ and $F_{{\rm mag},y}$ : If you look carefully at the picture, you'll realize that $\vec{B} = B_o \hat{z}$. The velocity is always $\vec{v} = v_x \hat{x} + v_y \hat{y}$. The cross product is therefore, $$\vec{v} \times \vec{B} = (v_x \hat{x} + v_y \hat{y}) \times (B_o \hat{z}) = v_x B_o (\hat{x} \times \hat{z}) + v_y B_o (\hat{y} \times \hat{z}) \nonumber $$ According to the right hand rule, $\hat{x} \times \hat{z} = -\hat{y}$ and $\hat{y} \times \hat{z} = \hat{x}$. This means that $\vec{F}_{\rm mag}$ is $$\vec{F}_{\rm mag} = q \vec{v} \times \vec{B} = q v_x B_o (-\hat{y}) + q v_y B_o (\hat{x}) \nonumber $$ Which is equivalent to $$F_{{\rm mag},x} = q v_y B_o \nonumber $$ $$F_{{\rm mag},y} = -q v_x B_o \nonumber $$
Step 6. Now that I have given you $F_{{\rm mag},x}$ and $F_{{\rm mag},y}$ you can solve for $a_x$ and $a_y$. Go ahead and solve for these accelerations and write down your result for $a_x$ and $a_y$ in what you submit for the lab. Now for the coding part. Using the accelerations you just calculated you can figure out the change in velocity over the time interval $\Delta t$. $$\Delta v_x = a_x \Delta t \nonumber $$ $$\Delta v_y = a_y \Delta t \nonumber $$ Use your expressions for $\Delta v_x$ and $\Delta v_y$ in your code. Use these expressions inside of the if statement that has the code for when the particle is inside the magnetic field. I'm talking about this section of the code:
if ( x > x_plate_right ) 
  deltaVx = 0.0; // change in vx from magnetic field 
  deltaVy = 0.0; // change in vy from magnetic field
When finished your code should behave like this Step 7. When the charged particle swings back and hits the plate again it should have the same speed as when it started because a constant magnetic field can change the direction of the velocity but it can't change the magnitude of the velocity. Check to see if this is true of your simulation (diferences +/- 10% are ok). You may want to look at the simulation without the magnetic deflection to figure out the speed of the particle right as it leaves the plates. Note: you might not see perfect agreement between these two numbers because of numerical errors (perhaps try making dt smaller to see if this helps). Step 8. Calculate the "gyroradius" and see if it matches up with the radius of the circle in the program. Notice that the gyroradius depends on the mass and that the particle in the simulation is actually a Deuteron (q = 1, mass = 2) Write out your calculation and your result in what you submit for the lab. Also answer this question: Would hydrogen (q = 1, mass = 1) have a larger or smaller gyroradius than a Deuteron? Why? Step 9. Change the charge from +1.0 to -1.0 and change the electric field from positive to negative and see if the particle moves in the magnetic field the way you expect (which way do you expect?!). Just for fun, edit the code so that the picture is of a negatively charged particle. Do this by changing
And make sure to change the electric field from positive to negative, or else the negatively charged particle may not make it through! What happens?!

How to get full credit on this assignment!!!

1. State which direction that the magnetic force will exert on the particle (Step 3) No calculation necessary. When the particle enters the region with the magnetic field, will the magnetic force on the particle be up, down, left, right, into or out of the page? The direction of the magnetic force needs to be correct for you to get full credit on this question 2. Guess what the trajectory of the particle will look like in the magnetic field (Step 4) No calculations necessary. Just guess qualitatively what will happen to the particle when it enters the magnetic field. Will it go straight, up, down, in a circle, etc.? It's ok if you guess wrong. As long as you think about it and give your best guess. Justify your answer (with words) for full credit 3. Write down what you should use for $a_x$ and $a_y$ in the the comments accompanying your code (Step 6) Look carefully at the end of Step 5 and use this information to figure out what you should use for $a_x$ and $a_y$ (hint: divide by mass). Write down $a_x$ and $a_y$ in the comments and implement this in the code (don't forget $\Delta t$!). 4. Check if the velocity at the end of the simulation is about the same as it would be if there were no magnetic field (Step 7) In the comments for the code write down the final velocity of the particle in the simulation with the magnetic field. Then either set B = 0 or go back to your earlier simulation and check the final velocity is and write this down in your comments. It's ok if these are off by 10-20%. More than that could indicate a problem. 5. Look up "gyroradius" in any physics textbook and calculate the gyroradius for this problem. Then measure the gyroradius from the simulation! Write down your calculated gyroradius and the measured gyroradius. The two should be consistent within 10-20%. More than that could indicate a problem. 6. Which direction would a negatively charged particle move in the magnetic field? Change the charge from +1 to -1, change the E field to a negative value and see what happens when the particle enters the magnetic field. Write down what happened.

Download Options

Credits and Licensing

The instructor materials are ©2017 Chris Orban.

The exercises are released under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 license

Creative Commons Attribution-NonCommercial-ShareAlike 4.0 license