Physics 212E Spring 2004 Classical and Modern Physics Chowdary Computer Exercise #2 Launch Mathematica by clicking on the Start menu (lower left hand corner of the screen); from there go up to Science and Math Applications; go to the Mathematica 4.1 folder; then click on Mathematica 4.1. Mathematica will open up what is called a notebook. You type in the notebook, Mathematica evaluates your commands, and then outputs back into the notebook. Type the following into Mathematica: Plot@x^2, 8x,0,3<D (note those are curly brackets, not parenthesis) Then hit either: Enter on the numeric keypad or Shift+Enter (Shift and Enter simultaneously) on the regular keyboard. (If you just hit enter on the regular keyboard, you simple insert a carriage return without letting Mathematica know that you want it to carry out the command you have just given it.) This will evaluate the expression in the cell you just typed in. You should see the following appear on your screen: In[1]:= Plot@x^2, 8x,0,3<D 8 6 4 2 Out[1]= Graphics 0.5 1 1.5 2 2.5 3 (Your screen should also have blue brackets on the right, which don t show up in the image above. If you re unsure what I m talking about, check with your neighbor, then ask me as needed.) This graph shouldn t surprise you too much. Now, try Plot@A x^2, 8x,0,3<D and hit Enter (keypad) or Shift+Enter. You should see the following appear on your screen:
In[2]:= Plot@A x^2, 8x, 0, 3<D Plot::plnr : Ax 2 is not a machine size real number at x = 1.25`*^-7. Plot::plnr : Ax 2 is not a machine size real number at x = 0.12170097471874736`. Plot::plnr : Ax 2 is not a machine size real number at x = 0.2544263995781211`. General::stop : Further output of Plot::plnr will besuppressed during this calculation. 1 0.8 0.6 0.4 0.2 Out[2]= Graphics 0.2 0.4 0.6 0.8 1 What a mess! Why did this happen? After all, A*x^2 should have the same form as x^2, just scaled by the factor A. Of course, Mathematic has no idea what A is. So it just sits there and yells at you. If you want to get rid of this mess (I would), the easiest thing to do is to click on the right most bracket (the one that includes the entire outpu and then hit Delete. Now, try Plot@ 2 x^2, 8x,0,3<D and evaluate this expression. Do you get what you expected? Ok. Now let s use this to plot some wave functions.
2 2 y 1 y We know that the classical wave equation is =. A solution to the wave equation is the 2 2 2 x v t harmonic solution: y ( x, = Asin( kx ωt + δ). We can verify that this is a solution by direct substitution. For convenience, we ll set the phase δ = 0, which is fine to do with just one wave (it simply means we start our clock at a different point in time.) In class on Friday, we ll start to see situations where the phase (more specifically the phase difference between waves) has physical significance. So we ll work with y( x, = Asin( kx ω for now. We saw previously that we could plot x^2, a function of x, with little difficulty. It won t be too much harder to plot a function of two variables, but we ll build it up to it in small steps. Of course, we need to be careful with values for constants, since as we saw before, if we just left A, k, and ω as variables, Mathematica will have a fit. So for convenience, we ll let A = 1, k = 1, and ω =1. Exercise #1: What are the (standard) units for A, k, and ω? Exercise #2: What is the wavelength, frequency, and period of this wave? What is the wave speed? So we ll work with the harmonic wave y( x, sin( x meters and t were in seconds. =, where y could be in meters if x were in Our method will be to take snapshots of the wave at various instants of time, and put the snapshots together to get a moving picture of the wave motion. We ll start at time t = 0, and increment by steps of one tenth of a period. Exercise #3: What is one tenth of a period? Leave your answer in terms of π. Now, enter the following into your Mathematica notebook: Plot@Sin@x 0D, 8x,0,2 Pi<D and evaluate the cell. You should obtain the following (if the numbers in your In[ ]= and Out[ ]= don t match mine, that s fine). Note that Mathematica talks in radians, not degrees.
In[4]:= Plot@Sin@x 0D, 8x,0,2 Pi<D 1 0.5 1 2 3 4 5 6-0.5-1 Out[4]= Graphics You ve just taken a snapshot of the wave at time t = 0, and plotted it! Now, repeat this for one tenth of a period later: t = 2π*1/10 (we ll assume the units are in seconds). Enter the following (you can just type it in. If you want to copy and paste, the easy way to do that is to click on the right bracket that just contains the last line you typed in (probably at Out[4]), then copy and paste, and update as needed.): Plot@Sin@x 2 Pi 1ê10D, 8x,0,2 Pi<D and evaluate. Exercise #4: CAREFULLY sketch the plot on the axes above. Do the same thing for t = 2π*2/10 and sketch your plot on the axes above. Repeat one last time for t = 2π*3/10. What do you notice about the motion of the wave? Specifically, what direction is the wave moving? Does this match the rule of thumb that was given to you in class? Comment briefly. This approach rapidly gets tedious (I m already bored). Let s see if we can find some way to speed this up and make our lives easier and more interesting. We ll have Mathematica do the boring stuff for us (after all, computers were invented to do boring, repetitive calculations.) First, you MUST type in and evaluate the following, in order to turn the Animation package on: <<Graphics`Animation` the ` is right below the Esc key, and not next to the Enter key You might get some blue messages; just ignore them. Next, type the following: Animate@Plot@Sin@x td, 8x,0,2Pi<D, 8t,0,2 Pi,Piê 10<D
Much of the above line should look familiar to you, or be straightforward in context. The last bit: {t, 0, 2*Pi, Pi/10} tells the animate command to start at t = 0 and go up to t = 2*Pi in steps of Pi/10, essentially what you were doing by hand previously, except taking snapshots twice as fast. Evaluate this cell if you haven t already. You should get twenty graphs (you have twenty steps between t = 0 and t = 2*Pi if your step size is Pi/10). How can you animate these graphs? You double click on one of the graphs that was just output by Mathematica. That should cause one of the graphs to become animated. What is going on? Mathematica is just taking all the individual snapshots and displaying them on the same axes one right after the other, thus animating them. A neat trick is to double click on the middle bracket on the right (ask if you re not sure). Doing this collapses all the graphs into one, and when you double click on that single graph, you ll get the animation. Exercise #5: Consider the harmonic wave y ( x, = sin( x +. What direction do you expect this wave to move? Explicitly check your expectation using Mathematica. Was your expectation correct? Exercise #6: Now, consider the harmonic wave y ( x, = sin( x +. What direction do you expect this wave to move? Explicitly check your expectation using Mathematica. Was your expectation correct? Exercise #7: Finally, consider the harmonic wave y( x, = sin( x. What direction do you expect this wave to move? Explicitly check your expectation using Mathematica. Was your expectation correct? One of the most fascinating wave properties is the principle of superposition: waves that overlap in time and space add algebraically. Let s observe a striking and important example of superposition. You saw that y ( x, = sin( x + was a left moving wave, and y( x, = sin( x was a right moving wave. Let s see what happens when a right moving wave overlaps with the exact same wave moving to the left. Type the following: Animate@Plot@Sin@x td +Sin@x+tD, 8x,0,2Pi<, PlotRange 8 2,2<D, 8t,0,2 Pi,Pi ê30<d Much of the above syntax should look familiar to you. One difference is Pi/30, which simply takes snapshots more frequently. The PlotRange {-2,2} command is new. It is needed here since the plots automatically rescale as needed each time a new plot is made. If you don t force it to a particular scale using PlotRange, the animation effects get ruined (imagine taking snapshots with a different zoom each time). Try it without PlotRange {-2,2} (don t forget to get rid of the previous comma as well) if you want to see what I mean. Exercise #8: If you haven t already, evaluate the cell. Remember that this is the superposition of identical waves: one traveling to the right, and the other traveling to the left. What interesting behavior do you observe? Comment briefly.
I suggest you save this notebook in your public space; Mathematic has an extensive help menu and you can learn it relatively easily just by playing around. (If there s time, do the following in class. If not, you can play around later.) Speaking of playing around, now that you have the basics of plotting and animation down, you should try some of the other neat things that can happen with waves, especially with superposition of waves. If you go to the course web-site, and go the Course Calendar for today, you should be able to download a file called Waves212E.nb (do this by right clicking on the link, and then clicking Save target as.) I noticed that this was very slow; I ll have some disks with this file on it as well. Save this file either to the Desktop or to your public space. This file is heavily documented; you should be able to figure out most things from what you ve done today, and read the documentation for the rest. If you have questions, you should please ask me. Hope you think some of these wave phenomena are interesting. Though we are modeling them mathematically here, it turns out that ALL of the things you see are actually physical; most of them we ll experience on Friday!