**Exercise 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.

- a) Write a program that defines a function returning the value x(x−1), then calculates the derivative of the function at the point x = 1 using the formula above with δ = 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? - b) Repeat the calculation for δ = 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 δ gets smaller, but then gets worse again. Plot your result as a function of δ . Why is this?

**Exercise 4.4: Calculating integrals**

Suppose we want to calculate the value of the integral

The integrand looks like a semicircle of radius 1:

and hence the value of the integral—the area under the curve—must be ½π = 1.57079632679 . . .

Alternatively, we can evaluate the integral on the computer by dividing the domain of integration into a large number N of slices of width h = 2/N each and then using the Riemann definition of the integral:

where

and .

We cannot in practice take the limit N → ∞, but we can make a reasonable approximation by just making N large.

- a) Write a program to evaluate the integral above with N = 100 and compare the result with the exact value. The two will not agree very well, because N = 100 is not a sufficiently large number of slices.
- b) Increase the value of N to get a more accurate value for the integral. If we require that the program runs in about one second or less, how accurate a value can you get?