Numerical differentiation

You should already be familiar with the idea of analytical differentiation and be able to differentiate simple functions like y=xn and y=sin(x). If you don’t know how to do this, look it up now in any A level textbook or the relevant section of the Calculus Wikibook.

Sometimes functions are highly non-linear and a closed form for the derivative may be difficult to calculate. For example

y=xlnx

Alternatively, y can be defined as the solution to an equation, so we can not calculate a closed form for y=f(x) to which the traditional rules of differentiation may be applied. In such cases we may calculate a numerical approximation for the derivative using the following difference formulae.

Summary of mathematics used

Suppose y=f(x). Let the points x0,x1,x2,,xN be equally spaced over the interval [a,b], and let h=1N(xNx0)=xi+1xi. Now let yi=f(xi).

  • The forward difference approximation to the first derivative at x=xi is given by: yi+1yih
  • The backward difference approximation to the first derivative at x=xi is given by: yiyi1h
  • The central difference approximation to the first derivative at x=xi is given by: yi+1yi12h
  • An approximation to the second derivative at x=xi is given by: yi+12yi+yi1h2

Where h is assumed to be small, the smaller h is, the better the approximation becomes. The derivation of these forms is undertaken in the next exercise.