Computing with Your Own
Rules for Differentiation

by
K. D.  Stroyan
University of Iowa

copyright 1997 by Academic Press, Inc. - All rights reserved.

Notebook Overview

Mathematica has a built in function called  D[.]  that will perform all the symbolic differentiations of calculus.  We explain the use of D[.] in the Chapter 7 NoteBook DfDx.  Before we use D[.], however, we will develop our own function "diff" to perform symbolic differentiation.  In doing this you will be able to see how powerful each of these rules is.  You will see what the various rules can do and what they can not do, by seeing the effect of adding new rules.

We will be able to perform all the symbolic differentiations in basic calculus by writing only 16 general symbolic function rules in our program.  Our "diff" program is built up in steps that correspond to the rules in Chapter 6 of the Main Text.  As you <Enter> cells, you are adding more rules to "diff," just as you yourself are developing more powerful differentiation procedures in the corresponding sections of the text.  For example, formulas that cannot be differentiated without the product rule also cannot be found with "diff" before you <Enter> that section of the program.  They can be computed with "diff" after you <Enter> it.  There are problems just like this in the text for you to compute or say why the rules are not powerful enough to do so.

References to the Text

This NoteBook goes with Chapter 6 of the text.

Step-by-step

Power Rule

The first rules we define allow Mathematica to differentiate powers.  The first rule below almost does this, but there are computer complications.  Note the underscores after the expressions supplied to the diff function.  The . after the n tells Mathematica that there doesn't need to be a number n given to the diff function.  This provides for the case when we are differentiating x^1, which we usually just write as x.  The second rule says the derivative of a constant c is zero, provided c doesn't depend on x.  (See Mathematica's manual for more explanation of the peculiar "?NumberQ" command. Essentially, it asks if c is a number and not a function of x.)

\(\(diff[x_\^n_. , x_] := n\ x\^\(n - 1\); \)\)diff[c_?NumberQ, x_] := 0;

Once you have Entered the rules above you can differentiate powers. Enter the following function in the next cell: (x^(1/5)) .  (<Enter> it now.)

diff[?? ,x]

Since the square root function can be expressed as a power, we can use the rules above to differentiate that as well.  See this by typing  (Sqrt[x]) in the next cell and entering.

diff[?? ,x]

The function  f[x] = 1/x  can also be expressed as a power.  Use diff[.] to differentiate it.

One odd thing about Mathematica is that it does not consider π and (Euler's) e (= E ≈ 2.71828...) to be `numbers.'  (π and e have many symbolic uses in Mathematica.)  Thus the rule for numbers above will not apply to π or e.  We must explicitly define the derivates of π and e to be 0 as follows. <Enter> the next rule now, so our power rule is complete.

diff[π, x_] := 0; diff[E, x_] := 0;

Practice Exercise

Use  diff[.]  to differentiate the functions

y = f[x] = (1/(x^2))

y=f[x]=e

y = f[x] = (x^π)

Other Variables

The variable name "x" in our program for "diff" can be changed to any convenient variable.  For example,

\(V\  = \ r^3; 
diff[V, r]\)

Powers vs Sums and Multiples

Notice that Mathematica is still unable to differentiate some simple expressions.  It still doesn't know that the derivative of a sum is the sum of the derivatives, because we haven't told it the superposition rule yet.  Enter the next computation to see what happens when Mathematica doesn't know how to compute an answer.

diff[3 x + 2, x]diff[((4 π)/3) (r^3), r]diff[E (x^π), x]

You know the superposition rule for differentiation, so what are the derivatives of the last two functions?  How do you apply the power rule and superposition to get YOUR answer?

Ans.:

Sine and Cosine Rules

The rules below will enable us to differentiate Sines and Cosines.  <Enter> them now.

diff[Sin[x_], x_] := Cos[x]; diff[Cos[x_], x_] := -Sin[x];

Practice Exercise

After <Enter>ing the sine and cosine rules, check them on a new variable by differentiating

y = f[ θ ] = Sin[ θ ]

and

y = f[ ω ] = Cos[ ω ]

Log and Exponential Rules

The rules below enable us to differentiate Logs and Exponentials.  <Enter> them.

diff[Exp[x_], x_] := Exp[x]; diff[Log[x_], x_] := (1/x);

Practice Exercise

After <Enter>ing the sine and cosine rules, check them on a new variable by differentiating

x = f[ y ] = Log[ y ]

and

y = f[x] = (e^x)

Superposition Rule

Now we define rules that will differentiate linear combinations of functions. We break this into two parts.  Given a function times a constant a, "diff" is defined simply as the constant times "diff" of the function.  Next, we define "diff" of a sum as the sum of the "diff"s.  Notice that g itself can be any function, even another sum of functions.  If g itself is a sum, Mathematica will simply apply the rule again.

<Enter> these rules now and continue.

diff[a_?NumberQ f_, x_] := a diff[f, x]; diff[f_ + g_, x_] := diff[f, x] + diff[g, x];

Now Mathematica is able to differentiate the simple sum we gave it above: 3 x + 2 .  <Enter> it..

diff[ ?? ??????, x ]

To see how powerful these rules are, enter the following function and <Enter> the cell.

y = Cos[x] + 7 Sin[x] - 0.5 (Sqrt[x]) + ((3 Log[x])/4)

\(y = \(?? ?;
diff[y, x]\)\)

Notice, however, there are still many expressions Mathematica still can't handle. Try a product

diff[ x Sin[x], x ]

Practice Exercise

1)  Insert cells and show that diff[.] can compute rather complicated expressions that you could do using only the rules above here in this NoteBook, but would just as soon let the computer do for you.  

2)  Give an example of your own construction of a function  y = f[x]  that YOU CAN differentiate, but that  diff[.] can not differentiate with only the rules above here in this NoteBook.

The Product Rule

Next we will define the product rule for differentiation.

diff[f_ g_, x_] := diff[f, x] g + f diff[g, x];

Now we can differentiate the product given above:  x Sin[x] .  Do it:

diff[ ?? ???, x ]

Again to emphasize how powerful this rule is, note that g above itself can be a product of two functions.  If it is, Mathematica  will simply use the same rule again. Enter the following function in the appropriate place to see:  3  Sin[x]  (Cos[x]  Exp[x])

diff[ ?? ????????, x ]

On the other hand, diff[.] does not yet have a rule to differentiate the composition of two functions.  Not even very simple compositions.  diff[.] can differentiate  3 x, <Enter> it:

diff[3 x, x]

diff[.] can differentiate Sin[u], <Enter> it:

diff[Sin[u], u]

But diff[.] can not differentiate the simple composition (knowing only the rules above here)

y = Sin[ 3 x ]

See for yourself

diff[ Sin[ 3 x],x]

Practice Exercise

1)  Insert cells and show that diff[.] can compute rather complicated expressions that you could do with only the rules so far, but would just as soon let the computer do for you.  

2)  Give an example of your own construction of a function  y = f[x]  that YOU CAN differentiate, but that  diff[.] can not differentiate with only the rules above here in this NoteBook.

The Chain Rule

The last rule we need is the Chain Rule.  Doing this is a little more complicated, but if we remember what the chain rule says, the rules below will be clear.

Suppose we wanted to differentiate the following function:

y = ((((x^2) + 2 x - 1))^3)

To use the chain rule we would first differentiate using the power rule and then multiply by the derivative of the function x^2 + 2 x - 1 to get:

(dy/dx) = 3 ((((x^2) + 2 x - 1))^2) (2 x + 2)

This suggests the following rule for differentiation.	<Enter> it.

\(\(diff[f_\^n_, x_] := n\ f\^\(n - 1\)\ diff[f, x]; \)\)

This is the Chain Rule applied to a function to a power.  Test it with the cell below.

diff[((((x^2) + 2 x - 1))^3), x]

Once the form of the above rule is clear, it should be clear how to finish defining the chain rule.  We simply modify the rules above for special functions of x to apply to functions of functions of x.  Below are the Sine, Cosine, Log, and Exponential Rules redefined for arbitrary functions f.

diff[Sin[f_], x_] := Cos[f] diff[f, x]; diff[Cos[f_], x_] := -Sin[f] diff[f, x]; diff[Exp[f_], x_] := Exp[f] diff[f, x]; diff[Log[f_], x_] := diff[f, x]/f;

Test the rules above on this expression:

((Exp[2 x] Sin[3 x])/Log)[x]= Exp[2 x] Sin[3 x] (((Log[x]))^(-1))

diff[?? ,x]

The Superposition Rule and Product Rule remain unaltered and Mathematica can use them all.  The function "diff" can now do the quotient rule as follows.

The Quotient Rule

There is not much need to use the Quotient Rule once we have the above rules working properly.  As another illustration of how powerful the above rules are, however, we will use the above rules to obtain the Quotient Rule in explicit form.

diff[(f/g), x]

This looks a little odd, but if we use the Together command we will see the Quotient Rule in the text.

Together[diff[(f/g), x]]

The Tangent Rule

You know that  Tan[x] = Sin[x]/Cos[x]  and Mathematica  knows that, too.  Unfortunately, it knows it too well.  Try to compute the derivative of tangent using the diff[] rules so far, <Enter>ing:

Sin[x]/Cos[x]

diff[Sin[x]/Cos[x], x]

Exercise

Use the quotient rule to find the derivative of tangent and then expand diff[.] to make a new rule like the rules above for Sin[x] or Log[x] and their associated chain rules as above. (In Chapter 5 we also showed by direct geometric estimates that the derivative of Tangent is Secant squared.  Note: It is also possible to tell Mathematica to turn the trig simplification rules off.  If you do this, the expression  Sin[x] * (Cos[x]^(-1)  will be reduced by diff[.] to a derivable expression.)

Combined Computation

With all the cells above <Enter>ed, you have written a general symbolic differentiation function that can do all the exercises that you are supposed to be able to do by the end of Chapter 6 on Symbolic Differentiation.  (Mathematica's built-in D[.] also knows derivatives for non-elementary functions like Bessel functions.)

Exercises

Exercise 6.2.4

Run this NoteBook and work through it line by line so that you can see how the addition of each of the Superposition Rule, Product Rule and Chain Rule makes symbolic differentiation more powerful.  For example, we can't differentiate  (Sqrt[x]) Cos[x]  without the Product Rule and neither can the computer.

Silly Fun

You could also use the ideas above defining diff[.] to give bogus "rules" for differentiation.  You could define  diff[ Tan[x],x] to be Logarithm, or diff[ f + g, x ] to be  f * g.  It's a little silly, but it might be fun to see what the computer does with some completely arbitrary meaningless rules.