Physics 1251 Programming Lab!: Magnetic force (a.k.a. Mass Spectrometer)

You may recall the last programming exercise that involved a proton being accelerated through two charged plates. In this exercise we will add a magnetic field to the right hand side of the simulation. The proton only experiences this field when it passes into the right-hand side. This example is loosely based on the "mass spectrometer" shown in Figure 29.14 in Serway & Jewett and a similar "mass spectrometer" example from another textbook commonly used with Physics 1251 shown below. Click the aforementioned link or the diagram below to learn more about how a mass spectrometer setup like this works. The particle accelerated in the diagram is 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 molecule in the magnetic field tells us something about the mass of the molecule.

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 cod.

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? (Hint: The B field is into the page instead of out of the page as in the previous diagram.)

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$$ $$\sum F_y = F_{{\rm mag},y} = m a_y$$ $$\vec{F}_{\rm mag} = q \, \vec{v} \times \vec{B}$$

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$$ 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 $$

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$$ $$F_{{\rm mag},y} = -q v_x B$$

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$ 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})$$

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})$$ Which is equivalent to $$F_{{\rm mag},x} = q v_y B_o$$ $$F_{{\rm mag},y} = -q v_x B_o$$

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$$ $$\Delta v_y = a_y \Delta t$$

Use your expresions for $\Delta v_x$ and $\Delta v_y$ in bfield.pde. 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" (Sec. 29.2) and see if it matches up with the radius of the circle in the program. Notice that the gyroradius depends on the mass. Write out your calculation and your result in what you submit for the lab. Do more massive particles have a larger or smaller gyroradius than lighter particles?

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 bfield.js so that the picture is of a negatively charged particle.

Do this by changing

positive(x,y);
to
negative(x,y)

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 magneitc 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 through Section 29.2 and calculate the gyroradius. 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.