780.20: 1094 Session 3

Handouts: Background notes for Session 3; Landau/Paez chapter 4 excerpts (passed out last time); Gnuplot fitting example; C++ formatting; printouts of codes

Your goals for today and next time (in order of priority):

Please work in pairs. The instructors will bounce around 1094 to ask and answer questions.

You should make or change to a 780 sub-directory, download session03.zip from the 780 homepage and unzip it.


Leftover Tasks from Session 2

Here are the priority tasks to finish from Session 2. Don't worry about other items; when you complete these, move on to Session 3.

  1. Summing in Different Orders. Make sure to finish the entire section. Be careful in part 5: There is one bug (a typo) that cannot be caught by the compiler.
  2. Bessel 1-3 (you'll finish this in PS#1).

Comparing Floating Point Numbers

A common task in a computational problem is to check whether two floating point numbers are the same. In C++, the comparison operator is == (not just one =), so it might seem that a simple if statement would do the job. Here's an example of why this fails in general:

  1. Look at the file number_comparison.cpp in Dec-C++. What does it do?




  2. Compile, link, and run the program. Why doesn't it give the answer you want? Add statements to print out x1 and x2 to check your response. What did you add?



  3. Suggest a better way to compare two numbers.




Numerical Derivatives: Pass 1

The Session 3 notes have a short introduction to numerical differentiation (see the Hjorth-Jensen notes online for more detail). These are among the simplest algorithms to use to derive and to verify the theoretical approximation and round-off errors (the errors for most real-world algorithms are noisy).

  1. Look at the file derivative_test_simple.cpp while reviewing the discussion in the notes. (Also look at the last section in the notes, which describes pointers to functions, which are used here.) What part(s) of the code do you not understand? What is being printed?



  2. Compile and run the code, generating the file derivative_test_simple.dat. Thee is a missing #include statement you need to add (compare with previous codes). Make a graph with gnuplot with two plot: the logarithm of the relative error for forward difference vs. the logarithm of h (which is Delta-x) and the analogous plot for central difference. Print the graph and attach
  3. Are the slopes in each region consistent with the analysis in the text? Which is the better algorithm? Explain. What value of h is optimal for each algorith?




Project with multiple files (including header file)

Many of the example programs started as "all-in-one" C programs from the Landau/Paez text. One of these was integ.c, which we converted to C++ and then split up into:

There is also a function in gauss.cpp we need. In a subsequent step, we modified the codes so that the function is passed as an argument to the integration functions.

The idea is that the integration routines should be isolated in a file by themselves. The main program just invokes these routines. The header file conveys the prototype information about the integration routines to the main program and to any other functions that might call the routines. (Note: Later we'll consider going further and defining an integration class.)

  1. Look at the original file (integ.c) and see how it was converted and split up into integ_test.cpp, integ_routines.cpp, and integ_routines.h (the cpp files are printed out for you). Also look in the ORIGINALS subdirectory to see the change to passing the function for the integrand. Why do these changes improve the code?



  2. Create a project integ_test and add the files listed above. (Follow the instructions from Session 1 "Using the GSL Scientific Library".)
  3. Compile, link, and run it to generate the integ.dat output file. Change integ_test.cpp to output relative (rather than absolute) errors. Note that only the files that have changed since the last compilation are recompiled each time!
  4. Use Gnuplot to reproduce Figure 4.3 on page 59 of the Landau-Paez handout. Briefly explain what you can learn from the plot. Consider all regions of the graphs.




  5. Bonus: Change the loop in integ_test.cpp so that the points on the log-log plot are evenly spaced. What did you change?




Finding the Approximation Error From a Log-Log Plot

From the plot in the previous section, we can estimate the approximation errors by eye. Now we want to actually fit lines to find the slope using Gnuplot. Use the handout as a guide.

  1. Modify the code so that it outputs the logarithm base 10 (log10) of N and the relative errors.
  2. Find the slopes of the trapezoid and Simpson's rule plots in the regions where they are linear.
  3. Are the results consistent with the analysis in the text? Can you fit the round-off error region?




Coding an Algorithm

This is primarily practice converting an algorithm to working code. Your goal is to add a new function to integ_routines.cpp, called three_eighth, which implements the 3/8 rule from Table 4.1.

  1. From the rule and the discussion in the text, write some pseudocode to explain how you would integrate a function with this rule.
  2. Implement your pseudocode in C++ by adding a new routine (called three_eighth) to integ_routines.cpp (be sure to add a prototype to integ_routines.h).
  3. Modify integ_test.cpp to output to a new file the results from the new routine, and add them to the previous plot. Explain the approximation error. [Warning: If you don't change the way that integ_test.cpp loops through the number of intervals, you will likely run into a subtle error that prevents you from getting the approximation predicted theoretically!]




GSL Scientific Library Yet Again

  1. Go to the web page with the GSL manual (it is linked from the 780.20 web page). Find an appropriate integration routine for the test integral we've been working on.
  2. Add another calculation to integ_test.cpp (with output to a file) using this GSL routine.
  3. Compare the accuracy of the GSL routine to the others on an error plot.





780.20: 1094 Session 3. Last modified: 07:13 am, January 10, 2007.
furnstahl.1@osu.edu