**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?