Accuracy and Speed

Exercises taken from Computational Physics by Mark Newman Chapter 4

4.1 Factorial with ints and floats

Write a program to calculate and print the factorial of a number entered by the user. If you wish you can base your program on the user-defined function for factorial given in Section 2.6, but write your program so that it calculates the factorial using integer variables, not floating-point ones. Use your program to calculate the factorial of 200.  Now modify your program to use floating-point variables instead and again calculate the factorial of 200. What do you find? Explain.

4.3 Calculating derivatives

Suppose we have a function f(x) and we want to calculate its derivative at a point x. We can do that with pencil and paper if we know the mathematical form of the function, or we can do it on the computer by making use of the definition of the derivative:

{df \over{d x}} = \lim_{\delta \to 0} {f(x+\delta)-f(x)\over{\delta}}

On the computer we can’t actually take the limit as \delta goes to zero, but we can get a reasonable approximation just by making \delta small.
Write a program that defines a function f(x) returning the value x(x-1), then calculates the derivative of the function at the point x=1 using the formula above with \delta=10^{-2}. Calculate the true value of the same derivative analytically and compare with the answer your program gives. The two will not agree perfectly. Why not?
Repeat the calculation for \delta=10^{-4}, 10^{-6}, 10^{-8},10^{-10}, 10^{-12}, and 10^{-14}. You should see that the accuracy of the calculation initially gets better as \delta gets smaller, but then gets worse again. Why is this?
We will look at numerical derivatives in more detail in Section 5.10, where we will study techniques for dealing with these issues and maximizing the accuracy of our calculations.