*Handouts:* 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!):
*

- Finish (some) leftover tasks from Session 1 (if any)
- Look at a round-off error demonstration with quadratic equations
- Discuss a special-case problem to help understand Landau-Paez 3.4 problem 3
- Debug and format (using "indent") sample codes
- Make some plots with gnuplot
- Assess different methods of calculating spherical Bessel functions (section 3.9 of Landau-Paez)
- Use the spherical Bessel function from the Gnu Scientific Library (GSL) in a code
- Are round-off errors random? (Advanced)

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 go to a directory on the shared disk (see Session 1 for creating one), download session02.zip from the 780 homepage and unzip it in your directory.

You should finish or correct the following subtasks from Session 1, which we will build on. If you are correcting something, check it with an instructor sometime during the class today.

**Dev-C++:**Make sure that you've*correctly*modified area.cpp to calculate the volume of a sphere and verified that it works.**Overflows, Underflows, and Machine Precision:**Complete part 2, modifying "precision.cpp" so that the result for double precision is correct (`10e-12`is*incorrect*).**Setting g++ Compile Options:**Make sure to do this.**Using the GSL Scientific Library:**Complete parts 1, 2, and 3.

This section assumes you have read the background notes for Session 2, which describe round-off errors and the quadratic equation.

- Review the discussion about round-off errors and the quadratic equation in Session 2 notes with your partner. Make sure you go over the pseudo-code for the test case and understand it.
- Look at the code quadratic_equation_1a.cpp in Dev-C++ 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.
Under Tools->Editor Options, change the tab size at the bottom right
to 2. Highlight everything between the
`main () {`line and the final`}`(not including these) and then hit Tab to indent. The convention we'll use is 2 spaces for each sub-level. - Now try compiling it in Dev-C++.
You should get a bunch of warnings and errors. Each one comes with
the line number of the error. Take each one in turn
and mark on the handout what you think the error is.
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").
*List the errors here:*

- Correct these errors and see that it compiles and runs.
Try a=1, b=2, c=0.1 and see that the roots differ.
*Which two are the most accurate?*(Hint: check the Session 2 notes.)

- 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!).
- 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 (there were also examples in Session 1).
- Follow the "Plotting Data from a File with Gnuplot" handout to duplicate the plot on the back. Try some of the extra commands, including output to a postscript file. Print out the file on Smith1011s and hand it in at the end.
*Is the plot qualitatively and quantitatively consistent with the analysis in the notes? Explain in a few sentences.*

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
10^{7}
small numbers a = 5x10^{-8} to 1 (so the exact answer is 1.5).
For the first three questions, *predict* the answer before running
the code.

*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.")

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

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

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

- Try to compile demo1a.cpp. 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!). How should you indent it (see demo1.cpp)?
*Challenge: Predict the results for 10*^{8}additions (instead of 10^{7}):

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

The next three tasks (Bessel 1-3) 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. Skim these sections before going further.

- Compile and run bessel.cpp.
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? What are the columns?*

- 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).
*Print out your plot.*

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

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

- 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?)*

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 z_{c} = 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?

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

furnstahl.1@osu.edu