Accuracy and Speed

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:

{d f\over dx} = \lim_{\delta\to0} {f(x+\delta)-f(x)\over\delta}.

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.

  1. a)  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 δ = 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?
  2. 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

I = \int_{-1}^1 \sqrt{1-x^2} \> dx.

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:

I = \lim_{N\to\infty} \sum_{k=1}^N hy_k

where
y_k = \sqrt{1-x_k^2} and x_k = -1 + hk.

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

  1. 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.
  2. 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?