CPSC/Math 402 Lab

Numerical Differentiation

The program derivative.cc uses three different formulas for approximating the derivative. Three functions (sin(x), log(x), and 1 + x + x3) are built into the program (note: the third function wasn't in the version handed out in class). The three formulas are:

The program lets you choose the function to use, the value of x, the starting value for h and the number of iterations. At each iteration h is divided by some number for the next iteration - you also enter that number (typically you would want to divide by 2 or 4 or 10 - for problem #27 on page 185 you would start at 0.1 and use 10 as the number to divide by). The program prints tables of results for each approximation forumla. The tables contain the iteration number, the estimate of f' given by the formula and the actual error in the estimate (which in this case can be computed because we actually know what f' is and don't really need to do numerical differentiation!). The first formula is the one problem #27 asks you to examine.

Save the program to your directory either by clicking on the link or using the Unix copy command to copy to your current directory:

      cp ~cpsc/public_html/Spring2012/CPSC402A/derivative.cc  .
The command to compile is:
      g++ derivative.cc
or
      g++ derivative.cc  -o diff
The first command puts the executable in a file named a.out while the second puts it in a file named diff (you can substitute any name here).

To run the program type:

        ./a.out
or
        ./diff
depending on what you named the executable.

Run the program as directed below and answer the questions in the book. Also observe the differences in the approximations! You can get a printout to use to answer the question. To do that use script. Recall that you just type script or script filename at the command prompt. Then all output from your commands is put in a logfile named typescript (if you didn't give a filename) or the filename you used. Type exit when you are done.