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
Figure 8.3-1 Illustration of methods for estimating the derivative dy/dx,
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.
Figure 8.3-2 Measurements of a sine function containing uniformly distributed random errors between -0.025 and 0.025.
Figure 8.3-3 Comparison of backward difference and central difference methods for the data shown in Figure 8.3-2.
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
Table 8.3-1 Numerical differentiation functions
Related Matlab Homework Assignments
A few weeks ago, I wrote about calculating the integral of data in Excel. This week, I want to reverse direction and show how to calculate a derivative in Excel. Just like with numerical integration, there are two ways to perform this calculation in Excel:
- Derivatives of Tabular Data in a Worksheet
- Derivative of a Function using VBA (or Visual Basic for Applications)
For this post I’m going to focus on calculating derivatives of tabular data, with a post about calculating the same using VBA coming at a later date.
Differentiation of Tabular Data
This is the kind of derivative calculation that is typically performed on experimental data. It can be especially useful when you were not able to directly measure the quantity of interest, but were able to measure its integrand.
The classic example, of course, is position and velocity:
Say for instance, you performed some experiment where it was difficult to obtain the velocity directly. So instead, you measured the position at various times, t. You could import the data into Excel and calculate the velocity as the derivative of position with respect to time.
Finite Difference Method in Excel
The method used to perform this calculation in Excel is the finite difference method.
To use the finite difference method in Excel, we calculate the change in “y” between two data points and divide by the change in “x” between those same data points:
This is called a one-sided estimation, because it only accounts for the slope of the data on one side of the point of interest.
A better estimation would be to calculate the average slope at the point of interest by averaging the slope directly before and after that point.
So, if we wanted to find the slope at y2 (z), we could use this calculation:
Example: Calculate a Derivative in Excel
Let’s look at how to calculate a derivative in Excel with an example. We can use the position data that was calculated by integrating velocity data in the previous post and use it to calculate both the velocity and the acceleration. As a check, we will compare the calculated acceleration data to the initial acceleration data.
To make things easier for now, I’ve hidden the old acceleration and velocity data. We’ll look at how they compare at the end.
First, I calculate the velocity using the finite difference equation above. Since we need a y3 and a y1, I start the calculation in Cell E5 and fill it down.
Next, using the calculated velocity, I can calculate the acceleration using the same method. This time, the calculation is started in Row 6.
In theory, if we differentiate data obtained by integration then we should end up back at the original data. Of course, all numerical methods impart some kind of error into the data.
But how bad is the error? Let’s compare.
In this case, we can see some slight differences between the initial acceleration data and that obtained by differentiation. There are also some slight differences in the two velocity data sets. Fortunately, the error in numerical differentiation is not cumulative, unlike with numerical integration.
Tables of data are not an ideal way to examine this data, so let’s look at the plots:
It’s hard to see because the two lines are on top of each other, but for all practical purposes the velocities are identical.
How about the acceleration?
Here we can see that during periods of steadily increasing or constant acceleration, the two data sets are very similar. However, when there is a discontinuity in the acceleration data (i.e. at times 0.1, 0.45, 0.5, 0.7, and 0.75 sec) the acceleration obtained by differentiation (orange) does not match the original acceleration data (blue).
This is due to the equation that we used to perform the differentiation. Remember how we obtained the derivative at a point by averaging the slope on either side of that point? We’re seeing the results of that here.
If you’ve followed along with the instructions, then congratulations! You’ve just performed numerical differentiation using Excel. Of course, calculating a derivative in Excel isn’t that difficult once you know how to do it.
Have you used this method on some data? Tell me about it in the comments below.