Physics 3730/6720 Assignment 2

Reading and references:

For each of the following exercises create the specified files with your answer(s). Submit your homework using the course submit utility.

Exercise 1. Histograms

Starting from the file ~p6720/examples/lcrs/lcrs.e_gax use awk, hist and gnuplot and make a histogram of the number of galaxies in recession-velocity bins of 5000 km/s, from 0 to 90,000 km/s. Again, do not copy the data file; use the absolute path to reference it. Use the hist -x option to set the range and the -s option to set the bin width and the -g option to get plottable output. Redirect the output from hist to a file. Call it n_vs_vr. You will use gnuplot to make a Postscript plot from this file. Write a gnuplot command file n_vs_vr.gpl that puts the plot in a file. Call your Postscript file n_vs_vr.ps. Using the appropriate "set" commands, label the x axis "v_r km/s" and the y axis "count". Give it a title "Broadband red galaxies". (To find the "set" command, use "help set".)

The faster a galaxy is receding, the farther away it is. You'd think that as you went farther away you'd be looking at a larger volume of space, so there should be more galaxies. If you did it right, your histogram should show that the opposite is true; that is, there is a peak and then beyond that the the number falls off rapidly with increasing velocity. Why?

Answer: It has to do with the flux limit of the survey. The survey omitted galaxies whose apparent brightness (as seen on Earth) fell below a certain threshold. At great distance a galaxy had to be very bright at the source in order to be included. But the number of very bright galaxies decreases rapidly with increasing brightness.

Submit only the files n_vs_vr and n_vs_vr.gpl.


Exercise 2. Quadratic equation

In this exercise you are to improve a program for computing the roots of a quadratic equation ax2 + bx + c = 0 and in the next exercise, use it to explore round-off errors. I have a crude single-precision version in ~p6720/examples/c++/quadratic.cc. Copy this program, but modify it as follows:

  1. Insert a test to cover the special cases a = 0 and a = b = 0. (Here it would be nice, but not required, to have your code write the solution to a linear equation.)
  2. Insert a test to quit if the roots are not real.
  3. Use single precision values throughout. In particular save the result of taking the square root in a single precision variable.
  4. Compute the roots using the standard formula
         x = [-b +/- sqrt(b^2 - 4*a*c)]/(2*a)
    
    and using the alternative formula
         x = 2*c/[-b -/+ sqrt(b^2 - 4*a*c)]
    
    You should compare the second formula (by hand algebra) with the standard one and notice that the root with the plus sign before the square root in one formula gives the same root as with the minus sign in the other. (Nothing to hand in.) By choosing the better formula carefully for each root you can avoid any round-off errors. Write all four answers, so you can compare them. Have your program label the two more reliable roots "robust" and the two less reliable roots "risky".
  5. In the second formula you might be dividing by zero. There are still two valid roots. So handle this special case as well.
  6. Write a Makefile so that you can compile your program using the single command
         make quadratic
    
Hand in your revision of the file quadratic.cc and your Makefile. We will test the compilation and run your program.

Exercise 3. Roundoff error

First answer these questions in an answer file roundoff.txt:

(a) In the conventional formula, when b is positive, which sign in front of the square root leads to a possible loss of significant digits? When it is negative, which?

(b) A large loss of significance occurs when you subtract two numbers that are almost equal. In each case the two numbers are b and sqrt(b^2 - 4*a*c). What simple mathematical condition can you put on a, b, and c that would cause the magnitudes of these two numbers to be very close?

(c) Then run your program for the cases

     a = 0.02, b = 2, c = 4
     a = 2e-4 , b = 3, c = 5
     a = 1e-2, b = 8, c = 1e-3
Put your results in a text file roundoff.txt. Identify which of the pair of results is the correct answer. Then explain the differences in the two sets of results in terms of round-off error. Explain also the size of the round-off error in the poorly evaluated roots.

NOTE: It is not sufficient to give only numerical results here. You must also give a clear EXPLANATION of those results. Your explanation should say why the risky formula gives such a bad result in the worst case, and why it gives such a good result in the best case.

Submit the file roundoff.txt.


This page is maintained by:
Carleton DeTar Mail Form
Last modified 28 August 2009