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:
On the computer we can’t actually take the limit as goes to zero, but we can get a reasonable approximation just by making 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 . 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 , and . You should see that the accuracy of the calculation initially gets better as 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.