20 Constructing derivatives
This chapter shows to use the computer to construct the derivative of any1 function. This is easy because the task of constructing derivatives is well suited to the computer.
We will demonstrate two methods:
Symbolic differentiation, which transforms an algebraic formula for a function into a corresponding algebraic formula for the derivative.
Finite-difference methods that use a “small”
, but not evanescent.
Chapter 23 covers the algorithms used by the computer to construct symbolic derivatives. Why teach you to do with paper and pencil the simpler sorts of problems that the computer does perfectly? One reason is that it helps you to anticipate what the results of the computer calculation will be, providing a mechanism to detect error in the way the computation was set up. Another reason is to enable you to follow textbook or classroom demonstrations of formulas which often come from working out a differentiation problem.
20.1 Why differentiate?
Before showing the easy computer-based methods for constructing the derivative of a function, it is good to provide some motivation: Why is differentiation so frequently in so many fields of study and application?
A primary reason lies in the laws of physics. Newton’s Second Law of Motion reads:
“The change of motion of an object is proportional to the force impressed; and is made in the direction of the straight line in which the force is impressed.”
Newton defined used position
Many relationships encountered in the everyday or technical worlds are more understandable if framed as derivatives. For instance,
- Electrical power is the rate of change with respect to time of electrical energy.
- Birth rate is one component of the rate of change with respect to time of population. (The others are the death rate and the rates immigration and emigration.)
- Interest, as in bank interest or credit card interest, is the rate of change with respect to time of assets.
- Inflation is the rate of change with respect to time of prices.
- Disease incidence is one component of the rate of change with respect to time of disease prevalence. (The other components are death or recovery from disease.)
- Force is the rate of change with respect to position of energy.
- Deficit (as in spending deficits) is the change with respect to time of debt.
Often, we know one member in such function-and-derivative pairs, but to need to calculate the other. Many modeling situations call for putting together different components of change to reveal how some other quantity of interest will change. For example, modeling the financial viability of retirement programs such as the US Social Security involves looking at the changing age structure of the population, the returns on investment, the changing cost of living, and so on. In Block V, we will use derivatives explicitly to construct models of systems, such as an outbreak of disease, with many changing parts.
Derivatives also play an important role in design. They play an important role in the construction and representation of smooth curves, such as a robot’s track or the body of a car. (See Chapter 48.) Control systems that work to stabilize a airplane’s flight or regulate the speed and spacing of cars are based on derivatives. The notion of “stability” itself is defined in terms of derivatives. (See Chapter 44.) Algorithms for optimizing design choices also often make use of derivatives. (See Chapter 49.)
Economics as a field makes considerable use of concepts of calculus—particularly first and second derivatives, the subjects of this Block—although the names used are peculiar to economics, for instance, “elasticity”, “marginal returns” and “diminishing marginal returns.”
20.2 Symbolic differentiation
The R/mosaic function D()
takes a formula for a function and produces the derivative. It uses the same sort of tilde expression used by makeFun()
or contour_plot()
or the other R/mosaic tools. For instance, run the code in Active R chunk 20.1 to see the function created by differentiating
If you prefer, you can use makeFun()
to define a function, then hand that function to D()
for differentiation as in Active R chunk 20.2:
When differentiating a function with two (or more) inputs, the function definition will have the names of the inputs on the right-hand side of the tilde expression, as in Active R chunk 20.3 where the two arguments to f()
are named x
and y
. But notice that in the use of D()
, the right-hand side of the tilde expression lists only the “with respect to” variable, in this case, x
.
Needless to say, D()
knows the rules for the derivatives of the pattern-book functions introduced in Section 19.3. For instance,
Try the other eight pattern-book functions. For seven of these, D()
produces a symbolic derivative. For the eighth, D()
resorts to a “finite-difference” derivative (introduced in Section 20.3). Which is this pattern-book function where D()
produces a finite-difference derivative?
20.3 Finite-difference derivatives
Whenever you have a formula amenable to the construction of a symbolic derivative, that is what you should use. Finite-difference derivatives are useful in those many situations where you don’t have such a formula. The calculation is simple but has a weakness that points out the advantages of the evanescent-
For a function
We will call the finite-difference approximation fd_sine()
and use makeFun()
to construct it:
Notice that fd_sine()
has a parameter, h
whose default value is being set to 0.01
. Whether 0.01 is “small” or not depends on the context.
To demonstrate that fd_sine()
is a good approximation to the symbolic derivative
Typically when using finite-difference derivatives, you do not have the symbolic derivative to compare it to as was done in Active R chunk 20.5. This would be the case, for example, if the function is a sound wave recorded in the form of an MP3 audio file. How would we know the finite-difference method is reliable in such a case.
Operationally, we can define “small
Look carefully at the vertical axis scale in the output from Active R chunk 20.6. The tick marks are at
In practical use, one employs the finite-difference method in those cases where one does not already know the exact derivative function.
In such situations, a practical way to determine what is a small h
, say, 0.0002 seconds. If the two candidates are a close match to one another, then you have confirmed that your choice of
It is tempting to think that the approximation gets better and better as h
is made even smaller. But that is not necessarily true for computer calculations. The reason is that quantities on the computer have only a limited precision: about 15 digits. To illustrate, let’s calculate a simple quantity,
Using the finite-difference derivatives, you can see this loss of precision if we make h
too small. # small in the finite-difference approximation to h = 0.000000000001
. You can see the loss of precision and back off to a bigger, more satisfactory value of
This process of using a small and an even smaller
20.4 Second and higher-order derivatives
Many applications call for differentiating a derivative or even differentiating the derivative of a derivative. In English, such phrases are hard to read. They are much simpler using mathematical notation.
a function the derivative of , the second derivative of , usually written even more concisely as .
There are third-order derivatives, fourth-order, and on up, although they are not often used.
To compute a second-order derivative
There is a shortcut for constructing high-order derivatives using D()
in a single step. On the right-hand side of the tilde expression, list the with-respect-to name repeatedly. For instance, look at the functions produced in the following active R chunks:
- The second derivative
:
- The third derivative
:
Physics students learn a formula for the position of an object in free fall dropped from a height D()
to find the object’s acceleration.
The second derivative of
The acceleration does not depend on
There are functions where the derivative cannot be meaningfully defined. Examples are the absolute-value function or the Heaviside function which we introduced when discussing piecewise functions in Section 9.4. Chapter 22 considers the pathological cases and shows how to spot them at a glance.↩︎