Physics 212E Classical and Modern Physics Spring 2017 VPython Class 6: Phasors and Interference 1 Introduction We re going to set up a VPython program that will take three inputs: the number of slits, the slit spacing, and the wavelength of light, and produce an animation showing the interference pattern and phase diagram. This will lead to a pretty cool result, but it s a bit ambitious for a single class period. At the top of the program, just after the from vpython import * and from math import * lines, let s define our three parameters. And let s go one step further and tell ourselves that s what we re doing by putting a comment in the code. For example, # control parameters n = 3 d = 2.5e-6 # microns wavelength = 550e-9 # nanometers Anything written on a line after a # symbol is ignored by Python, which is used to add clarifying comments. Here we ve defined the number of slits N = 3, the slit spacing d = 2.5 10 6, and the wavelength λ = 550 10 9. We don t use the name lambda as a variable because this variable is already pre-defined for other purposes in Python. So, wavelength it is. 2 The Phasor Diagram, part I Once we specify the number of slits, we d like to make a phasor diagram with that many phasors, lined up head to tail. First, let s draw the x and y axes, which we can do with the curve object: xaxis = curve( pos=[(-1,0,0), (1,0,0)] ) yaxis = curve( pos=[(0,-1,0), (0,1,0)] ) Here we gave the curve object a list of positions, enclosed in square brackets. We have given curve objects a list of positions before, but we did it with the.append method. We d like to write code that will work no matter what value we put in for N. We can do this by creating a list of phasors. Lists are defined in Python by square brackets, so we begin by making an empty list called phasors: phasors=[] 1
We want to draw the vectors head to tail, so we need a vector variable to hold the location of the last vector s head, which will be the location of the next vector s tail. Let s call it tail and initially it should be the origin: tail = vector(0,0,0) Now we use a while loop to add a phasor to the list N times: i=0 while i < n: phasors.append(arrow(pos=tail, axis=vector(1/n,0,0), color=color.green)) tail = tail + vector(1/n,0,0) i = i + 1 Let s take a second to digest this. The variable i just counts how many times we ve gone through the loop. The append method grows our list of phasor objects by one each time. The phasors are green arrows pointing to the right, and the tail position of each new phasor is shifted to the right when we update the tail vector. Make sense? Run this and see if you get three arrows aligned like a central max phasor diagram. Now, try changing the number of slits to four. Does your program deal with it properly? 3 The Screen In our display window we re also going to set up the geometry of the real system, such as the slits and the screen, and also show the intensity. Here s some initial setup stuff: L = 1.5 # distance to screen ymax = 1 # height of screen scene2.center = vector(2,0,0) slits = box(pos=vector(2,0,0), size=vector(0.1,0.3,0.1), color=color.blue) screen = box(pos=vector(2+l,0,0), size=vector(0.1,2*ymax,0.01), color=color.red) The 2 just shifts the display to the right of our phasor diagram. We now want to add two curves to this display: (1) a horizontal line showing the direction of the central max and (2) a curve that will start out empty but will build up the intensity pattern on the screen: central_max = curve(pos=[(2,0,0),(2+l,0,0)]) intensity = curve() edge = L + 0.5*screen.size.x The last variable, edge holds the x coordinate for the right hand end of the red screen. You ll see why this is useful soon. 2
4 The Sweep Now we re going to sweep across the screen, from bottom to top, and see what the phasor diagram is doing and what the intensity is doing. First things first: let s set up the while loop that will control the sweep: y = -ymax # y is the coordinate on the screen while y < ymax: rate(5) # step 1: do the physics # step 2: update the phasor display # step 3: update the screen display y = y + 0.01 All the rest that you ll put into this program goes inside the while loop, between the rate(5) and the y=y+0.01 lines. 5 The Physics Okay, given y, L, d, and λ, calculate θ, r adj and φ adj. This should just take three lines of code. I called these quantities theta, delta_r, and delta_phi. Some useful things to know: math has trig functions like sin() and asin() (for arcsine), etc. These are in radians mode. math also has a built in variable pi = π 3.1416. 6 Update the Phasor Display Now is the trickiest part of the program. For a given y, we have a θ value that translates to a φ adj value. We want to the program to draw the appropriate phasor diagram with that φ adj. As we loop through the list of phasors, we ll need a vector variable keeping track of the location of the tail of the next vector, so let s define tail to be a vector, originally at the origin. We also aren t going to be making each phasor point in the (1/n, 0, 0) direction; rather they ll all be rotated relative to each other. So we need a direction vector, which should initially be (1/n, 0, 0) (this makes the first phasor point in the +x direction). After you ve defined your tail and direction vectors, we want to loop through all the phasor objects in the list phasors, which we can do with the syntax 3
for phasor in phasors: # stuff to do with each phasor This steps through each phasor object in phasors, momentarily calling each one phasor. We want to update the pos and axis attributes of the current object via phasor.pos = tail phasor.axis = direction This will do the right thing as long as we re sure tail points to where we want the current phasor s tail to be, and direction is pointing in the direction we want the current phasor to point. So we should update those vector variables to be ready for the next phasor object in the loop: tail = tail + direction direction = rotate(direction, angle=delta_phi) Do you see what this does? The head of the phasor that we just drew is located at tail+direction, so we assign that to be the new tail value for the next phasor. And each phasor is supposed to be rotated by φ adj relative to the previous, so here we rotate the direction vector. And that s it for updating the phasor diagram. Question: after this loop through the phasors, where do you think the tail vector is pointing? Remember that we update it after drawing each phasor. The answer to this question will come in handy below. Try running the program at this point and sort out any errors. While the screen display won t be doing anything, the phasor diagram should behave sensibly, sweeping through a range of φ adj values. 7 Update the Screen Display We re still inside the while loop, and we ve updated the phasor diagram for our new value of y. Now we ll update our screen display. And here comes the really cool part. We are going to plot the intensity of the interference pattern on the right hand side of the screen by building up the intensity curve object. What you need to know about intensity is that it is directly proportional to the square of the amplitude. If the amplitude doubles, the intensity quadruples, for example. The phasor diagram tells us the vector sum of all the phasors, and it s held in the variable tail, which we were updating always to be at the head of the last phasor we drew. Since intensity is the square of the amplitude, our signal intensity is just mag2(tail). We want this curve to poke out in the +x direction from the right edge of the screen, so we will add the intensity to the vector edge: 4
intensity.append( pos=( edge+mag2(tail), y, 0) ) That should do it. Try running this and see if you ve got all the pieces assembled. 8 Play Time The nice thing about this program is that you can now vary the slit spacing or the number of slits and everything will adjust accordingly. Which way should you change d to get more maxima on the screen? Try it. Change the number of slits to four. Do the minima happen where you would expect them to, based on the phasor diagram? Change the number of slits to 20. Do you begin to see why diffraction gratings give bright dots? Hand in a working version of the program with whatever number of slits and slit separation you would like. 5