780.20: 2082 Session 3
Handouts: Background notes for Session 3;
Landau/Paez chapter 4 excerpts;
Gnuplot fitting example;
Integrals with singularities;
Your goals for today and next time (in order of priority):
- Finish leftover tasks from Session 2
- Look at a quick demo about comparing floating-point numbers
- Use a makefile to compile a project with multiple .cpp files
and a header (.h) file
- Duplicate the results in Fig. 4.3 with a log-log plot,
then fit the slopes with Gnuplot
- Practice coding an algorithm:
Add a new subroutine with the 3/8 rule and analyze
- Add an appropriate integration routine from the GSL and analyze
- Modify the code to use a different function,
then evaluate an integral with a singularity
- Use Mathematica to check accuracy of your code
Please work in pairs (more or less).
Dick and Daniel will bounce around 2082 and answer questions (don't be
shy to ask about anything!).
You should make or change to a 780 sub-directory,
download session03.tarz from the 780 homepage and unpack it
("tar xfvz session03.tarz").
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.
- Summing in Different Orders. Finish the
entire section. Be careful in part 5: There is one bug that
cannot be caught by the compiler (check against the answer in the
comment to track it down).
- Bessel 1-3 Skip Bessel 4.
- Mathematica Scavenger Hunt.
Do the first one only.
Makefiles for multiple project files (including header file)
Last year, we took one of the "all-in-one" C programs from the Landau/Paez
text, integ.c, and converted it to C++: integ.cpp, and
split it up:
There is also a function in gauss.cpp and there is make_integ_test to compile
This year, we've also modified the codes so that the function is
passed as an argument to the integration functions.
- integ_test.cpp, which has the main program and the function
to be integrated
- integ_routines.cpp, which has the integration functions themselves
- integ_routines.h, which has function prototypes
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).
- 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 ORIGINALS to see the change to passing the function for
the integrand. Why do these changes improve the code?
Change integ_test.cpp to output relative (rather
than absolute) errors.
- Create the executable
integ_test using the makefile make_integ_test and run it to generate
the integ.dat output file.
Note that only the files that have changed
are recompiled by the makefile!
- Use Gnuplot to reproduce Figure 4.3 on page 59 of the Landau-Paez
handout. Explain what you
can learn from the plot to your partner (and then an instructor).
Consider all regions of the graphs.
- Can you change the loop in integ_test.cpp so that the points on
the log-log plot are evenly spaced?
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.
- Modify the code so that it outputs the logarithm base 10 (log10)
of N and the relative errors.
- Find the slopes of the trapezoid and Simpson's rule plots in
the regions where they are linear.
- 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.
- From the rule and the discussion in the text, write some
pseudocode to explain how you would integrate a function with this
- Implement your pseudocode in C++ by adding a new routine (called
three_eighth) to integ_routines.cpp (be sure to add a prototype to
- Modify integ_test.cpp to output to a new file the results from
the new routine, and add them to the previous plot. Can you 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!]
GSL Scientific Library Yet Again
- 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.
- Add another calculation to integ_test.cpp (with output to a file) using
this GSL routine.
- Compare the accuracy of the GSL routine to the others on an error plot.
More Complicated Integrals
- Choose one of the integrals with singularities from the
handout [Eq.(9) is a good choice!].
- Determine an "exact" answer analytically or using Mathematica
(see instructors for assistance).
- Modify your code so that you can analyze the integral with one
of the integration rules several ways, using an error plot to compare
(these may not all apply to a given integral):
- Brute force (unmodified)
- Subtracting the singularity
- Changing variables
780.20: 2082 Session 3.
Last modified: 09:35 pm, January 09, 2005.