780.20: 1094 Session 2

Handouts: Background notes for Session 2; Landau-Paez chapter 3 excerpt; gnuplot mini-manual; gnuplot example; printouts of several codes.

Your goals for this session (in order of priority; you won't finish them all!):

Please work in pairs and briefly answer questions asked here on this sheet. The instructors will bounce around 1094 and answer questions (don't be shy to ask about anything!).

You should make or change to a 780 sub-directory (see Session 1), download session02.tarz from the 780 homepage and unpack it ("tar xfvz session02.tarz").

Leftover Tasks from Session 1

It is not essential that you finish absolutely everything from Session 1, but you need to complete the following subtasks, which we will build on. Check them with an instructor sometime during the class today (even if you did them last time).

  1. Linux Environment: Make sure that you've correctly modified area.cpp to calculate the volume of a sphere and verified that it works.
  2. Overflows, Underflows, and Machine Precision: Complete part 2, modifying "precision.cpp" so that the result for double precision is correct.
  3. Using a Makefile: Successfully complete part 3.
  4. Using the GSL Scientific Library: Complete parts 1 and 2.

Round-Off Errors with Quadratic Equations

[Note: In the past this was presented in "lecture" mode. As an experiment, we'll have you simply read about it and run the demonstration codes.]

  1. Read the discussion about round-off errors and the quadratic equation in the background notes for Session 2. Make sure you go over the pseudo-code for the test case and understand it.
  2. Look at the code quadratic_equation_1a.cpp in an editor and/or on the handout. It represents the pseudo-code as it has just been typed in, mistakes and all. Note that it is not indented consistently. We can fix this with the "indent" tool. Just type "indent quadratic_equation_1a.cpp" at the command line and then look at the code again. It will have been indented automagically!
  3. Now try compiling with "make -f make_quadratic_equation_1a". You should get a bunch of warnings and errors. Each one comes with a number, which is the line number of the error. Take each one in turn and mark on the handout what you think the error is (you'll need to identify the line numbers in your editor; ask an instructor if you don't know how). Each warning is a clue to a mistake in the code (such as a typo)! [One hint: "setprecision" is defined in the header file "iomanip".] If you get stuck, quadratic_equation_1.cpp is the corrected version (without the "a" after the "1").
  4. Correct these errors and see that it compiles and runs. Try a=1, b=2, c=0.1 and see how the roots differ. Which are the best ones? (Hint: check the notes.)

  5. Now look at quadratic_equation_2.cpp, which is the second pass at the code. Check the comments for a description of changes. (This code is printed on the same handout as 1 and 1a.) Mark every C++ line on the printout that you don't understand completely (it may be most of them!).
  6. Compile and run quadratic_equation_2.cpp, and enter a=1, b=2, c=.1 again. The output is now more detailed and an output file named "quadratic_eq.dat" is created. We'll discuss outputting to a file more later; for now, this is an example you can follow to do it on your own.
  7. Follow the "Plotting Data from a File with Gnuplot" handout to duplicate the plot on the back. (You don't need to print the file.)
  8. Is the plot consistent with the analysis in the notes? Explain to your partner. [Note: Don't skip this!]

Summing in Different Orders (cf. Landau-Paez 3.4-3 Handout)

Here we consider another example of subtractive cancellation and how computer math is not the same as formal math. Imagine we need to sum the numbers 1/n from n=1 to n=N, where N is a large number. We're interested in whether it makes any difference whether we sum this as:
   1 + 1/2 + 1/3 + ... + 1/N (summing up)
or as:
   1/N + 1/(N-1) + ... + 1/2 + 1 (summing down)
In formal mathematics, of course, the answer are identical, but not on a computer!

To help think about the sum up vs. sum down problem, it's useful to think first about a simpler version. Suppose you want to numerically add 107 small numbers a = 5x10-8 to 1 (so the exact answer is 1.5).

  1. If you add 1 + a + a + ... in single precision, what do you expect to get for the total? (Hint: recall the discussion of "machine precision.")

  2. If, instead, you add a + a + ... + 1 in single precision, do you expect a different answer? Which will be closer to the exact answer?

  3. If you repeat the exercise in double precision, what do you expect will happen?

  4. Write a brief "pseudocode" for a program that would compare these two ways of summing. Then look at the file demo1a.cpp in an editor (also on a handout); does it look like an implementation of your pseudocode?

  5. In a terminal window, use the makefile make_demo1a to try and compile it ("make -f make_demo1a"). Identify and fix the errors, until it compiles and runs correctly (verify the results against the comments in the file). Correct your answers to 1,2,3 if necessary (and understand them!).
  6. Run indent on the file ("indent demo1a.cpp") and check that it is now nicely formatted.
  7. Challenge: Predict the results for 108 additions (instead of 107):

    Now change the code, and see if your prediction is correct (or that you can explain it after the fact).

Bessel 1: Making Another Plot with Gnuplot

The next four tasks (Bessel 1-4) build on the discussion in sections 3.6 to 3.9 of the Landau-Paez book. These sections are copied on one of our handouts. Read through the discussion before going further.

  1. Copy make_area to make_bessel and open make_bessel in an editor. (If you are in the session_02 subdirectory, you can use "cp ../session_01/make_area ." to create a new copy where you are or "cp ../session_01/make_area make_bessel" to make a copy directly; do you understand what all the "."'s mean? If not, ask!) Change all "area"'s to "bessel"'s. (Can you do this all in one step with the editor?) Run the makefile using "make -f make_bessel".
  2. If you run "bessel", you'll generate a data file "bessel.dat". By looking at the code (and chapter 3), figure out what the columns in the file mean. What Bessel function is being output?

  3. Make a plot of the third column as a function of the first column using gnuplot. (Use the example in the handout as a guide. You might also find the "GNUPLOT Manual and Tutorial" useful.) Try plotting BOTH the second and third columns vs. the first column on the same plot (i.e., two curves).

Bessel 2: Landau-Paez Section 3.9 Assessment (page 40 on handout)

  1. Modify the output statements in bessel.cpp to change the number of digits in the output (look back at other example codes for a hint).
  2. Modify the code bessel.cpp (give it another name) to do part 2 of 3.9.
  3. To answer part 3 of 3.9, make an appropriate plot of the error vs. x and interpret the graph.

Bessel 3: Using the GSL Scientific Library (continued)

Make sure you have completed the GSL task from Session 1 before doing this.

  1. Modify bessel.cpp to also calculate the spherical Bessel function using the GSL routine gsl_sf_bessel_jl for comparison. The GSL documentation gives the usage of this function as:
        Function: double gsl_sf_bessel_jl (int l, double x)
    "This routine computes the regular spherical Bessel function of order l, j_l(x), for l >= 0
    and x >= 0."
    How does the accuracy of the downward recursion routine in bessel.cpp compare to that of the GSL function? (How can you tell?)

Bessel 4: Spherical Bessel Functions in Mathematica

Mathematica doesn't have built-in spherical Bessel function routines but you can define them in terms of the more general Bessel functions.

  1. Using the Help Browser (with Master Index), find the name of the general Bessel function and how to use it.
  2. Define a function of x and l to calculate spherical Bessel functions, using:
    jl(x) = (pi/2x)1/2 Jl+1/2(x)
  3. How can you test that your definition is correct?

  4. Try to print 16 digits for l=10 and x=36.2. [Hint: Look up NumberForm and ScientificForm in the Help Browser.]
  5. How can you reveal the small x behavior of the spherical Bessel function?

Are Round-Off Errors Random? (Advanced)

In the book "Computational Physics", Landau and Paez say that the round-off errors in single-precision are distributed approximately randomly. Your task is to test whether this is true and what the distribution actually looks like. More specifically, if we generate a large set of zc = z (1 + epsilon) numbers from some calculation (e.g., by taking the square root of a bunch of numbers), how are the different epsilons distributed?

  1. Devise a scheme to test for the distribution of round-off errors.
  2. Carry out your scheme with a C++ program.
  3. Make appropriate plots to explore your results.

Mathematica Scavenger Hunt

Mathematica has A LOT of useful built-in functions, IF you can find them. The Help Browser (under the Help Menu) is your key; it's like the Google for Mathematica. Here we'll try to solve some problems to exercise your ability to use the Help Browser. Using the "Master Index" search is often the most efficient method.

  1. What is the atomic weight of uranium?

  2. How much is a boardfoot in MKS units?

  3. Is 15485863 a prime number? How many smaller primes are there?

780.20: 1094 Session 2. Last modified: 07:29 am, January 09, 2006.