As we have seen the derivative of a function can be interpreted graphically as the slope of the function. This interpretation leads to methods for computing the derivative numerically. Numerical differentiation must be performed when we do not have the function represented as a formula that can be differentiated using the rules presented in Section 8.1. Two major types of applications require numerical differentiation. In the first type, data has been collected and must’ be analyzed afterward using post processing to find rates of change. In the second type, the rates must be estimated in real time as the measurements are made. This application occurs in control systems. For example, an aircraft autopilot needs to estimate the rate of change of pitch angle to control the aircraft properly. Numerical differentiation with post processing can use all of the data and need
not be particularly fast. However, real-time numerical differentiation requires a fast algorithm that can use only the data measured up to the current time. These two requirements place a heavier demand on real-time algorithms as compared to post processing algorithms.
Here we will introduce some simple algorithms for computing the derivative numerically. Consider Figure 8.3-1, which shows three data points that represent a function y(x). Recall that the definition of the derivative is
The success of numerical differentiation depends heavily-on two factors: the spacing of the data points and the scatter present in the data, due to measurement error. The greater the spacing, the more difficult it is to estimate the derivative. We assume here that the spacing between the measurements is regular; that is, X3 – x2 = X2 – X1 = !!ox.Suppose we want to estimate the derivative dy / dx at
the point X2. The correct answer is the slope of the straight line passing through the point (X2, Y2), but we do not have a second point on that line, so we cannot find its slope. Therefore, we must estimate the slope by using nearby data points. One estimate can be obtained from the straight line labeled A in the figure. Its slope is
This estimate of the derivative is called the backward difference estimate and is actually a better estimate of the derivative at X = XI + (Δx)/2, rather than at X = X2. Another estimate can be obtained from the straight line labeled B. Its slope is
This estimate is called the forward,difference estimate and is a better estimate of the derivative at X= X2 + (Δx)/2 rather than at X =X2. Examining’ the plot, you might think that the average of these two slopes would provide a better estimate of the derivative at X = xi because the average tends to cancel out the effects of measurement error. The average of mA .and mB is
This is.the slope of the line labeled C, which connects the first and third data points. This estimate of the derivative is called the central difference estimate.
The diff Function
MATLAB provides the diff function to use for computing derivative estimates. Its syntax is d = diff (x), where x is a vector of values, and the result is a vector d containing the differences between adjacent elements in x. That is, if x has n elements, d will have n – 1 elements, where d = [x(2) – x(1), x(3)-x(2), … ,x(n)-x(n-1)]. For example,i fx = [5, 7, 12, -20],
then diff (x) returns the vector [2, 5, -32].
Let us compare the backward difference and central difference methods by considering a sinusoidal signal that is measured 51 times during one half-period. The measurements are in error by a uniformly distributed error between -0.025 and 0.025. Figure 8.3-2 shows the data and the underlying sine curve. The following script file implements the two methods. The results are shown in Figure 8.3-3.
Clearly the central difference method does better in this example.
Many more-advanced numerical differentiation procedures have been developed; for example, a central difference method using four points instead of two is commonly used. Some algorithms .are suitable only for post processing, whereas others have been developed specifically for real-time applications. Post processing algorithms are covered in advanced texts dealing with data analysis. Real-time algorithms are covered in signal-processing texts.
MAlLAB provides the polyder function to compute the derivative of a polynomial. The derivative of is
Because polynomial derivatives can be obtained from the preceding formula, the polyder function is technically not a numerical differentiation operation. Its syntax has several forms. The basic form is
b = polyder (p)
where p is a vector whose elements are the coefficients of the polynomial arranged in descending powers; that is, p =[a1, a2,…..,an]. The output of polyder is the vector.b containing the coefficients of the derivative; that is, b = [b1. b2,…..bn-1]. The second syntax form is
b = polyder (p1, p2)
This form computes the derivatiye of the product of the two polynomials p 1 and p2. The third fonn is
[num, den) = polyder (p2,p1)
This form computes the derivative of the quotient p2 / P1. The vector of coefficients of the numerator of the derivative is given by num. The denominator is given by den.
Here are some examples of the use of polyder. Let P1 = 5x + 2 and P2 = 10x² +4x – 3. Then
These results can be obtained as follows:
The results are der2 = [20, 4), prod = [150, 80, -7), num = [50, 40, 23]. and den = [ 25, 20, 4 ) . The numerical differentiation functions are summarized in Table 8.3-1,
Table 8.3-1 Numerical differentiation functions