Step-by-Step Differentiation

by George Beck

The built-in Mathematica operator D gives derivatives immediately without any intermediate steps. In this notebook the operators WalkD and RunD are defined to slow things down so that a beginner can learn how to take derivatives.

Proper formatting of output

From the menu, select Info, Preferences, Action... to produce a big box titled Action Preferences.

Look at the second box within Action Preferences titled Placing Print Output.

Click on the first little button so that there is no check mark for ``Place Print output as it is generated''.

Click on the second little button so that there is a check mark for ``Place each Print line in a separate cell''.

Click on the Apply button at the bottom.

Discussion

Differentiation rules

These are all the rules for taking derivatives.

PrintRules

Talking step-by-step differentiation

The defined function WalkD takes a derivative and tells you what rule it is using at each step. Expressions of the form d[y, x] mean dy/dx Mathematica uses the notation D[y, x] to take the derivative of y with respect to x without showing any intermediate steps.

Example 1

WalkD[x + Cos[x], x];

Example 2

WalkD[Sin[x + Cos[x]], x];

You can check with the built-in differentiation function D.

D[Sin[x + Cos[x]], x]

Quiet step-by-step differentiation

Using RunD instead of WalkD does the same thing but without telling you what rule is being used.

Example 3

Can you see which rules are beoing applied at each step? You can check with WalkD.

RunD[x ArcTan[Sqrt[x]], x];

Example 4

This triple chain is a symbolic example. Do you see the pattern at the end? Can you predict what the derivative of a chain of four functions will look like?

RunD[f[g[h[x]]], x];

Step-by-step second derivatives

Besides printing, WalkD and RunD also output the derivative. That means using them twice (in any combination) gives the first derivative followed by the second derivative. You can also do higher derivatives using triple combinations, and so on.

Example 5

RunD[WalkD[Cos[Sin[x]], x], x];

Again, you can check this with the built-in differentiation operator.

D[Cos[Sin[x]], {x, 2}]

Single-step differentiation

WalkD and RunD do one step after another until the derivative is completely worked out. You can do a single step of a differentiation by using the differentiation rules.

Instructions

This is the format for only a single step in taking the derivative of an expression. The slash-dot (/.) means the rule will be used on the expression.

expression /. rule

Example 6

If there is a sum first, use the linearity rule.

d[x^2 + 4 x^3, x] /. LinearityRule

Linearity is for sums and for terms with a constant multplier. Recall that in Mathematica % means the previous result.

% /. LinearityRule

Now use the power rule.

% /. PowerRule

Example 7

d[E^x + Tan[x], x] /. LinearityRule

SpecificRules gives the derivatives of individual functions that aren't combined in any way with other functions.

% /. SpecificRules

Example 8

d[Sin[x^2], x] /. ChainRule

% /. PowerRule

You can do two steps at a time.

d[Sin[x^2], x] /. ChainRule /. PowerRule

Example 9

The built-in operator D outputs derivatives immediately in one step.

D[x Log[x^2], x]

Here is the derivative done manually.

d[x Log[x^2], x] /. ProductRule

% /. SpecificRules /. ChainRule

% /. PowerRule

Example 10

You can use all the rules together. After the first step you can continue with Cmd-l until the derivative is finished. It is a good exercise to say what rule is being used at each step.

d[E^Tan[2^x], x] /. AllRules

% /. AllRules

% /. AllRules

% /. AllRules

Implementation

Clear @ d;

Usage messages

WalkD::usage = "WalkD[expression, x] prints each 
of the steps in finding the derivative of the expression 
with respect to x, using d to represent differentiation. 

It prints the differentiation rule that's used at each 
step. 

The function also outputs D[f, x].";

RunD::usage = "RunD[expression, x] prints each 
of the steps in finding the derivative of the expression 
with respect to x, using d to represent differentiation. 

The function also outputs D[f, x].";

Function definitions

SpecificRules =
	{
		d[x_, x_] :> 1, 
		d[f_[x_], x_] :> D[f[x], x], 
		d[a_^x_, x_] :> D[a^x, x] /; FreeQ[a, x]
	};

ConstantRule = d[c_, x_] :> 0 /; FreeQ[c, x];

LinearityRule = 
	{
		d[f_ + g_, x_] :> d[f, x] + d[g, x], 
		d[c_ f_, x_] :> c d[f, x] /; FreeQ[c, x]
	};

PowerRule = 
	{
		(* repeated intentionally: *)
		d[x_, x_] :> 1, 
		d[x_^a_, x_] :> a x^(a-1) /; FreeQ[a, x]
	};

ProductRule = d[f_ g_, x_] :> d[f, x] g + f d[g, x];

QuotientRule = d[Times[f_, Power[g_, -1]], x_] :> 
	(d[f, x] g - f d[g, x]) / g^2;

InverseFunctionRule = d[InverseFunction[f][x], x] :> 
	Derivative[1][f][InverseFunction[f][x]]^(-1);

ChainRule = 
	{
		d[f_^a_, x_] :> 
			a f^(a-1) d[f, x] /; FreeQ[a, x],
		
		d[a_^f_, x_] :> 
			Log[a] a^f d[f, x] /; FreeQ[a, x],

		d[f_[g_], x_] :> 
			(D[f[x], x] /. x -> g) d[g, x],
				
		d[f_^g_, x_] :> 
			f^g d[g Log[f], x]
	};

GeneralRules = Flatten @ 
	{
		ChainRule, 
		InverseFunctionRule,
		QuotientRule, 
		ProductRule, 
		PowerRule, 
		LinearityRule,
		ConstantRule  
	};

AllRules = Flatten @ {GeneralRules, SpecificRules};

PrintRules = TableForm @ 
{
	{""}, 
	{"AllRules      =  ", "SpecificRules + GeneralRules "}, 
	{ ""}, 
	{"GeneralRules  = ", "ConstantRule"}, 
	{"", "LinearityRule"}, 
	{"", "PowerRule" }, 
	{"", "ProductRule"},  
	{"", "QuotientRule "}, 
	{"", "InverseFunctionRule"}, 
	{"", "ChainRule "}, 
	{""} 
} ;

ListRules = 
{
	"SpecificRules ", 
	"ConstantRule", 
	"LinearityRule", 
	"PowerRule" , 
	"ProductRule",  
	 "QuotientRule ", 
	"InverseFunctionRule", 
	"ChainRule "
} ;

WalkD[f_, x_] := Module[
	{allrules, derivative},
	allrules = ToExpression[ListRules];
	derivative = d[f, x]; 
	Print[derivative, " ="]; 
	While[
		Not @ FreeQ[derivative, d], 
		oldderivative = derivative;
		k = 0;
		While[
			oldderivative == derivative,
			k++;
			derivative = derivative /. allrules[[k]]
		];
		Print[ListRules[[k]] ]; 
		Print["= ", derivative]; 
	];
	D[f, x]
]

RunD[f_, x_] := Module[
	{derivative},
	derivative = d[f, x]; 
	Print[derivative, " ="]; 
	While[
		Not @ FreeQ[derivative, d], 
		derivative = derivative /. AllRules;
		Print["= ", derivative]; 
	]; 
	D[f, x]
]