EARS1160 DUE DATE: Tues Nov 29, 4pm

Computing and Numerical Methods

Prof G.A. Houseman

Practical 5: Numerical Differentiation

Aim

In this practical you will write a short FORTRAN program to compute the numerical value of the first derivative of a function. You will then apply this program to a test problem that originates in electrical resistivity theory, and examine the accuracy of the approximations that you use to evaluate the numerical derivatives. One of the important uses for numerical estimation of derivatives is in the solution of the equation F(x) = 0, using Newton iteration. In this exercise, however, we focus only on the method (and accuracy) of computing F'(x).

As an example function F we will use a FORTRAN FUNCTION WENNER(NBL,P,SS,IERR) that is contained in the file WENNER.F, written by Dr V. Lesur. You will need to copy this file from /home/users/library/EARS1160/prac2 into your working directory for this exercise.

The function WENNER completes a very specific task that arises in the analysis of electrical resistivity data: to compute the apparent resistivity a of a medium composed of uniform horizontal layers of differing resistivity, as seen by an array of electrodes set up in the Wenner configuration. Some background to this problem is given below, but all we really need to know for the purposes of this exercise is that for a set of parameters whose values are entered in the array P, the function returns the value of the apparent resistivity.

The header section of WENNER.F, includes a specification of how the function is to be called. Note that it is good programming practice to always include such a section at the head of any program unit that you write (so that others can use it - and so that you can use it after you have forgotten how you set it up). In this case WENNER.F must be called with 4 variables in the argument list: 3 of these are scalars (NBL, SS, IERR), one is an array (P). Note that you must always pass the information to a subroutine or function in exactly the order that is required by the definition of the subroutine or function, and precisely the same number and type of arguments.

NBL: (integer) the number of layers assumed by the resistivity calculation (max. = 5) P: (real array of dimension 2*NBL-1). The first NBL values of P contain the resistivity values i for layers 1 to NBL. The next NBL-1 values of P contain the layer thicknesses hi for layers 1 to NBL-1 (the NBLth layer is assumed to be of infinite thickness) SS: (real) the spacing a of the Wenner array (see Geophysical Background below). IERR: (integer) an error flag which should be set to zero before calling WENNER, but which returns a non-zero value if the function detects an illegal parameter. For the following three-layered problem:

Layer 1: 1 = 500 m and h1 =1 m

Layer 2: 2 = 100 m and h2 =7 m

Layer 3: 3 = 1000 m.

the apparent resistivity a depends on all 5 of the above parameters. The calculation of course depends also on the array spacing SS (and the number of layers NBL). In general we require the derivatives with respect to the parameters P that define layer resistivity and thickness.

Using the centred approximation to the first derivative:

Fx  x Fx  x Fx   0 0 0 2x

(a) write a FORTRAN program (using single precision real variables) that:

(i) reads from file the values required to define the model. (ii) reads the values i (to identify whether we are differentiating with respect to h1 or h2), a (electrode separation), and h (the initial increment used in calculating the finite difference) from standard input, (iii) computes the numerical derivative of the function WENNER with respect to hi , and (iv) prints h and the derivative (v) repeat steps (iii) and (iv) 30 times, each time decreasing h by factor of 2.

(b) Apply the program (and save the output) for each of the three electrode separation values:

a1= 50 m, a2 = 5 m, a3 = 0.5 m, to estimate  a h2 , using initial h = 3 m.

Verify program by observing that the answer for a1 = 50 m is close to -39.99

(c) Now modify the program so that all real variables are 64 bit (REAL*8 or DOUBLE PRECISION) and repeat (b).

(d) Examine the output from steps (b) and (c), and determine the best numerical estimates of

 a h2 for each value of a.

(e) Amend your program once more to read as input the best numerical estimate, and to add a 3rd column to the output which shows the current error (difference between current estimate and best estimate) and rerun the calculations. For each value of a how small should h be, if the derivative must be accurate to a relative error of 10-5 ?

(f) Write a brief report (including a program list, and output from all 6 calculations) summarizing and explaining the trends that are observed in the output data, and in particular the role of discretization error and round-off error in the calculated numerical derivatives. Geophysical Background

The apparent resistivity calculation used as an example in this exercise arises in electrical resistivity prospecting. In vertical resistivity sounding an electric current I is injected in the ground through a pair of current electrodes C1, C2 and the potential difference V(a) is measured between a pair of voltage electrodes P1, P2. A typical deployment of the four electrodes has each pair of electrodes separated by distance a (parameter SS in function WENNER):

C P P C 1 a 1 a 2 a 2

The apparent resistivity, defined by a = (2aV/I) is the resistivity that would be inferred if the ground below the array were a half-space with uniform properties. The ground of course is not a uniform half-space, so the apparent resistivity depends on the electrode spacing a. The variation of a with a is indicative of sub-surface vertical variation which is often approximated as a sequence of uniform horizontal layers. The properties of these layers i and hi can be estimated by finding the zero of a function made of the squared differences between measured apparent resistivities and theoretical apparent resistivities for the layered model. To find these values we require the derivatives of the apparent resistivity with respect to i and hi.