Often one has to compute derivative of a function while doing numerical computation. If the form of the function is simple enough ( say sine function, exponential etc ) one knows the derivative analytically. Then the computation of the derivative is evaluation of the derivative function. That is straight forward. Many times the function form is quite complicated. In that case, the computation of analytical derivative could be involved. The expression of the derivative may be long and it may not be convinient to code the expression of the derivative. In that case one resorts to numerical computation of the derivative. Some cases, the function may not be given in a closed form and is evaluated numerically. In that case computation of analytical derivative is not possible. Finally, you may have a function given as a table of independent variable and the value of the function. Here also one has to resort to numerical differentiation.
The technique of numerical differentiation is based on the Taylor series expansion of a function:
![]() |
(1) |
![]() |
(2) |
Note that the error in the value of the derivative depends on
and the second derivative of the function at
. If
one can eliminate the second derivative somehow, one will be able to
compute the derivative more accurately even for relatively large value
of
. This can be done if we use the Taylor series for two
values of x;
and
. I
leave it to you to show that the derivative then can be defined as
![]() |
(3) |
Problem 1: Using these two definitions of the derivative, write a program to compute the derivative of known functions ( sine, cosine, exponential, logarithm, Bessel functions, power series etc ). Show that the second definition is superior.
One can extend the method of derivative calculation to high order derivatives. But it must be noted that the accuracy of the numerical values of the higher derivatives degrade very fast. This is because every time one computes a derivative there is loss of accuracy due to truncation. For higher order derivatives the error accumulates fast and soon one starts getting into noise.
Problem 2: Calculate first, second and third derivative of exponential numerically using one of the definitions given above. Show that the accuracy is least for third order derivative.
Note: While computing third order derivative, you need second order derivative at two values of the argument, this in turn means first order derivative is needed at four ( three if you are careful ) values of the argument and the function at eight ( or five ) values of the argument.
Many times one has data given as a function of an argument at fixed value. One then has to calculate the derivative. There are three different methods of computing the derivatives: methods using forward and backward differences and one using medians. The median method is the best since the values of the function used are close to the argument at which one is computing the derivatives. Further, if the values are given at equally spaced points, one can use the data such that even or odd derivatives are eliminated.