Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
PHYS 127: COMPUTATIONAL PHYSICS HOMEWORK
Plotting experimental data In the on-line resources you will find a file called sunspots.txt, which contains the observed number of sunspots on the Sun for each month since January 1749. The file contains two columns of numbers, the first being the month and the second being the sunspot number. 1. Write a program that reads in the data and makes a graph of sunspots as a function of time. 2. Modify your program to display only the first 1000 data points on the graph. 3. Modify your program further to calculate and plot the running average of the data, de- fined by Yk = 1 2r r ∑ m=−r yk+m , where r = 5 in this case (and the yk are the sunspot numbers). Have the program plot both the original data and the running average on the same graph, again over the range covered by the first 1000 data points. Visualization of the solar system The innermost six planets of our solar system revolve around the Sun in roughly circular orbits that all lie approximately in the same (ecliptic) plane. Here are some basic parameters: Radius of object Radius of orbit Period of orbit Object (km) (millions of km) (days) Mercury 2440 57.9 88.0 Venus 6052 108.2 224.7 Earth 6371 149.6 365.3 Mars 3386 227.9 687.0 Jupiter 69173 778.5 4331.6 Saturn 57316 1433.4 10759.2 Sun 695500 – – Using the facilities provided by the visual package, create an animation of the solar system that shows the following: 1. The Sun and planets as spheres in their appropriate positions and with sizes proportional to their actual sizes. Because the radii of the planets are tiny compared to the distances between them, represent the planets by spheres with radii c1 times larger than their cor- rect proportionate values, so that you can see them clearly. Find a good value for c1 that 1 makes the planets visible. You’ll also need to find a good radius for the Sun. Choose any value that gives a clear visualization. (It doesn’t work to scale the radius of the Sun by the same factor you use for the planets, because it’ll come out looking much too large. So just use whatever works.) For added realism, you may also want to make your spheres different colors. For instance, Earth could be blue and the Sun could be yellow. 2. The motion of the planets as they move around the Sun (by making the spheres of the planets move). In the interests of alleviating boredom, construct your program so that time in your animation runs a factor of c2 faster than actual time. Find a good value of c2 that makes the motion of the orbits easily visible but not unreasonably fast. Make use of the rate function to make your animation run smoothly. Hint: You may find it useful to store the sphere variables representing the planets in an array of the kind described on page 115. The Mandelbrot set The Mandelbrot set, named after its discoverer, the French mathemati- cian Benoıˆt Mandelbrot, is a fractal, an infinitely ramified mathematical object that contains structure within structure within structure, as deep as we care to look. The definition of the Mandelbrot set is in terms of complex numbers as follows. Consider the equation z′ = z2 + c, where z is a complex number and c is a complex constant. For any given value of c this equa- tion turns an input number z into an output number z′. The definition of the Mandelbrot set involves the repeated iteration of this equation: we take an initial starting value of z and feed it into the equation to get a new value z′. Then we take that value and feed it in again to get another value, and so forth. The Mandelbrot set is the set of points in the complex plane that satisfies the following definition: For a given complex value of c, start with z = 0 and iterate repeatedly. If the magnitude |z| of the resulting value is ever greater than 2, then the point in the complex plane at position c is not in the Mandelbrot set, otherwise it is in the set. In order to use this definition one would, in principle, have to iterate infinitely many times to prove that a point is in the Mandelbrot set, since a point is in the set only if the iteration never passes |z| = 2 ever. In practice, however, one usually just performs some large number of iterations, say 100, and if |z| hasn’t exceeded 2 by that point then we call that good enough. Write a program to make an image of the Mandelbrot set by performing the iteration for all values of c = x+ iy on an N × N grid spanning the region where −2 ≤ x ≤ 2 and −2 ≤ y ≤ 2. Make a density plot in which grid points inside the Mandelbrot set are colored black and those outside are colored white. The Mandelbrot set has a very distinctive shape that looks something like a beetle with a long snout—you’ll know it when you see it. Hint: You will probably find it useful to start off with quite a coarse grid, i.e., with a small value of N—perhaps N = 100—so that your program runs quickly while you are testing it. Once you are sure it is working correctly, increase the value of N to produce a final high-quality image of the shape of the set. 2 If you are feeling enthusiastic, here is another variant of the same exercise that can produce amazing looking pictures. Instead of coloring points just black or white, color points according to the number of iterations of the equation before |z| becomes greater than 2 (or the maximum number of iterations if |z| never becomes greater than 2). If you use one of the more colorful color schemes Python provides for density plots, such as the “hot” or “jet” schemes, you can make some spectacular images this way. Another interesting variant is to color according to the logarithm of the number of iterations, which helps reveal some of the finer structure outside the set. Least-squares fitting and the photoelectric effect It’s a common situation in physics that an experiment produces data that lies roughly on a straight line, like the dots in this figure: x y The solid line here represents the underlying straight-line form, which we usually don’t know, and the points representing the measured data lie roughly along the line but don’t fall exactly on it, typically because of measurement error. The straight line can be represented in the familiar form y = mx + c and a frequent ques- tion is what the appropriate values of the slope m and intercept c are that correspond to the measured data. Since the data don’t fall perfectly on a straight line, there is no perfect answer to such a question, but we can find the straight line that gives the best compromise fit to the data. The standard technique for doing this is the method of least squares. Suppose we make some guess about the parameters m and c for the straight line. We then calculate the vertical distances between the data points and that line, as represented by the short vertical lines in the figure, then we calculate the sum of the squares of those distances, which we denote χ2. If we have N data points with coordinates (xi, yi), then χ2 is given by χ2 = N ∑ i=1 (mxi + c− yi)2. The least-squares fit of the straight line to the data is the straight line that minimizes this total squared distance from data to line. We find the minimum by differentiating with respect to 3 both m and c and setting the derivatives to zero, which gives m N ∑ i=1 x2i + c N ∑ i=1 xi − N ∑ i=1 xiyi = 0, m N ∑ i=1 xi + cN − N ∑ i=1 yi = 0. For convenience, let us define the following quantities: Ex = 1 N N ∑ i=1 xi, Ey = 1 N N ∑ i=1 yi, Exx = 1 N N ∑ i=1 x2i , Exy = 1 N N ∑ i=1 xiyi, in terms of which our equations can be written mExx + cEx = Exy , mEx + c = Ey . Solving these equations simultaneously for m and c now gives m = Exy − ExEy Exx − E2x , c = ExxEy − ExExy Exx − E2x . These are the equations for the least-squares fit of a straight line to N data points. They tell you the values of m and c for the line that best fits the given data. 1. In the on-line resources you will find a file called millikan.txt. The file contains two columns of numbers, giving the x and y coordinates of a set of data points. Write a program to read these data points and make a graph with one dot or circle for each point. 2. Add code to your program, before the part that makes the graph, to calculate the quanti- ties Ex, Ey, Exx, and Exy defined above, and from them calculate and print out the slope m and intercept c of the best-fit line. 3. Now write code that goes through each of the data points in turn and evaluates the quan- tity mxi + c using the values of m and c that you calculated. Store these values in a new array or list, and then graph this new array, as a solid line, on the same plot as the orig- inal data. You should end up with a plot of the data points plus a straight line that runs through them. 4. The data in the file millikan.txt are taken from a historic experiment by Robert Millikan that measured the photoelectric effect. When light of an appropriate wavelength is shone on the surface of a metal, the photons in the light can strike conduction electrons in the metal and, sometimes, eject them from the surface into the free space above. The energy of an ejected electron is equal to the energy of the photon that struck it minus a small amount φ called the work function of the surface, which represents the energy needed to remove an electron from the surface. The energy of a photon is hν, where h is Planck’s constant and ν is the frequency of the light, and we can measure the energy of an ejected 4 electron by measuring the voltage V that is just sufficient to stop the electron moving. Then the voltage, frequency, and work function are related by the equation V = h e ν− φ, where e is the charge on the electron. This equation was first given by Albert Einstein in 1905. The data in the file millikan.txt represent frequencies ν in hertz (first column) and volt- ages V in volts (second column) from photoelectric measurements of this kind. Using the equation above and the program you wrote, and given that the charge on the electron is 1.602× 10−19 C, calculate from Millikan’s experimental data a value for Planck’s con- stant. Compare your value with the accepted value of the constant, which you can find in books or on-line. You should get a result within a couple of percent of the accepted value. This calculation is essentially the same as the one that Millikan himself used to determine of the value of Planck’s constant, although, lacking a computer, he fitted his straight line to the data by eye. In part for this work, Millikan was awarded the Nobel prize in physics in 1923. 5