LECTURE 6 NUMERICAL DIFFERENTIATION To find discrete approximations to differentiation (since computers can only deal with functional values at discrete points) Uses of numerical differentiation To represent the terms in o.d.e. s and p.d.e. s in a discrete manner Many error estimates include derivatives of a function. This function is typically not available, but values of the function at discrete points are. Notation Nodes are data points at which functional values are available or at which you wish to compute functional values At the nodes fx i f(x) x -2 - + +2 x i-2 x i- x i x i+ x i+2 node i-- i i+ i+2 p. 6.
Node index i indicates which node or point in space-time we are considering (here only one spatial or temporal direction) i 0 20 2 N=22 i=0,n i=, N For equi-spaced nodal points, h = x i + x i Taylor Series Expansion for f(x) About a Typical Node i fx = fx i x f x 2 + x i + -------------------f 2 x 2! i + x 3 -------------------f 3 3! x i x 4 + ------------------- f 4 x 5 x 4! i -------------------f 5 x 6 + + x 5! i + -------------------f 6 x 6! i + p. 6.2
For the present analysis we will consider only the first four terms of the T.S. expansion (may have to consider more) fx = fx i x f x 2 x i -------------------f 2 x 3 + + x 2! i + -------------------f 3 x 3! i + E where E = x 4 -------------------f 4 4! x i x 5 -------------------f 5 x 6 + x 5! i + -------------------f 6 x 6! i + E x 4 = -------------------f 4 x 4! i x E x 4 -------------------f 4 4! x i E Ox 4 If the Taylor series is convergent, each subsequent term in the error series should be becoming smaller. p. 6.3
The terms in the error series may be expressed Exactly as E We note that the value of is not known This single term exactly represents all the truncated terms in the Taylor series Approximately as x 4 = -------------------f 4 4! x 4 E -------------------f 4 4! x i This is the leading order truncated term in the series This approximation for the error can also be thought of as being derived from the exact single term representation of the error with the approximation f 4 f 4 x i In terms of an order of magnitude only as E Ox 4 This term is often carried simply to ensure that all terms of the correct order have been carried in the derivations. This error term is indicative of how the error relatively depends on the size of the interval! p. 6.4
Evaluate fx i + fx i x i + f x i + 2 = + x i + --------------------------- f 2 2! fx i + x i x i + 3 + --------------------------- f 3 x 3! i + Ox i + 4 2 3 + = + h + ----f + ----f 6 i + h 2 h 3 Oh 4 Evaluate fx i + 2 fx i x i + 2 f x i + 2 2 = + x i + --------------------------- f 2 2! fx i + 2 x i x i + 2 3 + --------------------------- f 3 x 3! i + Ox i + 2 4 + 2h 2h 2 2 --h 3 3 = + + + f 3 i + 2 4 Oh 4 p. 6.5
Evaluate fx i = fx i + x i f x i + fx i x i 2 ---------------------------f 2 2! x i x i 3 + --------------------------- f 3 x 3! i + Ox i 4 = h + h 2 2 ----f h 3 3 ----f 6 i + Oh 4 Similarly we can evaluate fx i 2 2h 2h 2 2 = + 2 4 --h 3 3 f 3 i + Oh 4 p. 6.6
Approximating Derivatives by Linearly Combining Functional Values at Nodes Forward first order accurate approximation to the first derivative Consider 2 nodes, i and i + + i i+ Combine the difference of the functional values at these two nodes 2 3 + = + h + ----f + ----f 6 i + h 2 h 3 Oh 4 h = h 2 2 h 3 3 + ----f ----f 6 i + Oh 4 + = ------------------ h h 2 3 --f ----f 6 i + Oh 3 h 2 p. 6.7
First derivative of f at node i is approximated as + = ------------------- + E where E h h --f 2 This is the first forward difference and the error is called first order in h (i.e. E Oh ) f(x) actual slope () + x i x i+ = x i +h Notes: There is a clear dependence of the error on h h approximate slope + - h The first forward difference approximation is exact for st degree polynomials p. 6.8
Backward first order accurate approximation to the first derivative Consider nodes i and i and define = hf i h 2 2 3 + ----f ----f 6 i + Oh 4 h 3 = h h 2 First backward difference of s then defined as: 2 ----f h 3 3 + ----f 6 i + Oh 4 = ------------------ + E h Error is again first order in h E = 2 --hf Oh p. 6.9
Central second order accurate approximation to the first derivative Consider nodes i, i and i + and examine + 2 3 = + h + ----f + ----f 6 i + Oh 4 f 2 3 i h + ----f ----f 6 i + Oh 4 + h 2 + h 3 = 2h Central difference approximation to the first derivative is h 3 3 + ----f 3 i + Oh 4 h 2 h 3 ------------------------- + E 2h + = Formula has an error which is second order in h E = h 2 3 ---- f 6 i Oh 2 p. 6.0
f actual slope () + i- - i i+ approximate slope + - - 2h x The smaller h, the smaller the error Error is obviously generally better for the central Oh 2 formula than the forward or backward Oh formulae! Expression is exact for 2nd degree polynomials due to the third derivative in the expression for E p. 6.
Strictly the order of the error is indicative of the rate of convergence as opposed to the absolute error log(e)=log( () - F.D. approx) st order 2nd order 2 log h p. 6.2
Forward first order accurate approximation to the second derivative Now consider nodes i, i + and i + 2 and the linear combination of functional values + 2 2 + + + 2 2 + + 2h 2h 2 2 --h 3 3 = + + + f 6 i + Oh 4 + 2 2 + Forward difference approximation to second derivative 8 2 2 h ----f --h 3 3 + + + f 6 i + Oh 4 + f i h 2 + h 2 2 h 3 3 = + Oh 4 + --------------------------------------- + E 2 + 2 2 + = h 2 Error first order in h 3 E = h = Oh p. 6.3
TABLE OF DIFFERENCE APPROXIMATIONS First Derivative Approximations Forward difference approximations: + 2 = ------------------ + E, E --hf h Backward difference approximations: + + 4 + 3 = ---------------------------------------------- + E, E 2h 3 --h2 3 2 + 9 + 2 + 8 + = -------------------------------------------------------------------- + E, E -- h 3 4 f 6h 4 i 2 3 = ------------------ + E, E h 2 --hf 2 i 3 4 + 2 = ----------------------------------------- + E, E 2h 3 --h2 3 8 + 9 2 2 3 = --------------------------------------------------------------------- + E, E 6h 4 --h3 4 p. 6.4
Central difference approximations: + = ------------------------- + E, E --h 2 3 f 2h 6 i + + 8 + 8 + 2 = --------------------------------------------------------------------- + E, E 2h 2 -----h 4 5 f 30 i Second Derivative Approximations Forward difference approximations + 2 + + 3 = -------------------------------------- + E, E h 2 2 h 2 + + 4 + 2 5 + + 2 = ------------------------------------------------------------------ + E, E -----h 2 4 f 2 3 h 2 p. 6.5
Backward difference approximations: 2 + 2 3 = ------------------------------------- + E, E h 2 h 2 2 5 + 4 2 3 = ------------------------------------------------------------ + E, E -----h 2 4 f 2 h 2 Central difference approximations: + 2 + = -------------------------------------- + E, E -----h 2 4 f 2 h 2 + + 6 + 30 + 6 2 = ------------------------------------------------------------------------------------------, 2h 2 + E E 2 2 -----h 4 6 f 90 i All the derivative approximations we have examined are linear combinations of functional values at nodes!! What is a general technique for finding the associated coefficients? p. 6.6