This explanation of the derivative will start with the above definition. The
instantaneous rate of change of a function with respect to an independent
variable ought to remind the reader of a concept from algebra: slope. The rate
at which a function changes(rise) divided by the respective change in the
independent variable(run) is how we define the slope of a line. The derivative
is the slope of a special line known as the tangent line. A tangent line is a line
that only intersects a curve in one place. This is how we can call the derivative
an instaneous slope, it is the slope of the line that intersects a curve at one
specific point. This point is what the definition above notes as the point c.
Calculating the slope of a tangent line tends to be difficult due to that
single point. As shown in the above figure, calculating slope needs two points.
What would happen if the same equation was used to calculate the slope of a line
with only one point? First, what would it resemble geometrically? How many
different lines can you put through a single point?
If you have a single point
out in space, an infinite number of unique lines could pass through that point.
Now consider doing the computation. If x2 and x1 are the
same number then x2 - x1=0. This means that in our slope
equation, we would have to divide by zero - and we can't do that. Notice
figure 2 to see what happens when the computer tries to divide by zero. This
eliminates zero from the domain of the slope function since it will break the
function or yield an undefined value.
All is not lost. In calculus, the slope of this tangent line is found by taking
the derivative in closed form.
The derivative is defined mathematically as the
limit as h (lateral distance between point) approaches zero of the quantity
f(x+h) - f(x) divided by h. As stated above, h can never equal zero so a limit
is used to define what is happening as the function nears the undefined value. In
computational science, the slope of the tangent line is estimated with two
points. This method of estimating is known as the 'secant method'.
This method works by taking setting h to a small enough value to give the
accuracy needed and using the definition of a derivative as shown above. In
the figures below it should be obvious that as delta x(this represents h) is
allowed to get smaller and smaller, the secant line in question more closely
represents the tangent line at that point.
When writing an algorithm to determine the derivative keep round off error in mind. If you allow h to get too small then the division will produce a number plagued with error. First use a function whose derivative is known and then allow h to range from too big to too small. If you already know what the derivative should be then you can see when round off error occurs. When h is large, the error you get isn't the computer's fault (round off error) the points being used for the secant line are so far apart that they do not represent the tangent line.
A derivative can be taken of a derivative to produce a higher order derivative. To do this numerically, we will use the definition of the derivative again. If the first derivative represents the instantaneous rate of change in the function with respect to the independent variable, then the second derivative represents the instantaneous rate of change in the derivative with respect to the independent variable. So in the definition of the derivative, f' would replace f. Since f'(x+h) is needed to calculate f''(x), the first derivatives must be found and stored ahead of time in an array. There are ways around this - maybe you can develop one.