Step-by-Step Differentiation

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 show each step so that a beginner can learn how to take derivatives. WalkD says what rule is used at each step, but RunD is in too much of a hurry for that.

Differentiation rules

These are all the rules for taking derivatives.

PrintRules

Step-by-step differentiation with each step explained

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 command 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 the result with the built-in differentiation function D.

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

Quiet step-by-step differentiation

RunD does the same thing as WalkD without telling you what rule is being used at each step.

Example 3

Can you see which rules are being 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

You can use WalkD and RunD twice (in any combination) to give the first derivative followed by the second derivative. You can also do the third derivative using triple combinations, and so on.

Example 5

RunD[WalkD[(E^((x^2))), x], x];

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

D[(E^((x^2))), {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.

How to apply a rule

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

In this example we substitute A for x.

(x^2) + (y^2) /. x -> A

The differentiation rules have the arrows already inside them.

LinearityRule

Example 6

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

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

LinearityRule is two rules in one, the first is for sums of functions and the second is for terms with a constant multiplier. 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 are not 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 one step at a time.

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 ⌘[l] until the derivative is finished, that is, until there is no more d in the expression. 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

wasSpellCheckOn = Head[General::spell1] =!= $Off;

Off[General::spell1]

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 is used at each \
step. The function outputs D[f, x].";

Function definitions

\(\(SpecificRules = 
        {d[x_, x_] :> 1, 
          d[f_[x_], x_] :> ∂\_x f[x], 
          d[a_\^x_, x_] :> ∂\_x a\^x /; FreeQ[a, x]};
      \)\)

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

The rule d[x_, x_] is repeated intentionally.

\(\(PowerRule = 
        {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[f_\/g_, x_] :> \(d[f, x]\ g - f\ d[g, x]\)\/g\^2;\)\)

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

\(\(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_] :> 
            \((∂\_x f[x] /. x -> g)\)\ d[g, x], 
          d[f_\^g_, x_] :> f\^g\ d[g\ Log[f], x]};\)\)

Drop the specific rules for generic cases.

Apply specific rules last when applying all at once.

Rule sets for applying the rules individually.

ListRules  =  ToExpression[RuleNames];

\(\(PrintRules\  := \ 
        CellPrint[
          Cell[BoxData[
              GridBox[Transpose[
                  {
	Prepend[
                      \({"GeneralRules ="<>#1, \ ##2} &\)\ @@\ 
                        Map[""<>\ # &, 
                          RuleNames], 
						\ "AllRules     = SpecificRules + GeneralRules"]}], 
	
                ColumnAlignments -> "", 
	
                RowSpacings -> {2, 0.8}]], \ "Output"]];\)\)

\(WalkD[f_, x_] := 
      Module[{derivative}, 
        derivative = d[f, x]; 
		displayStart[
          derivative]; 
		While[\(! \((FreeQ[derivative, 
                d])\)\), 
			oldderivative = derivative; 
          k = 0; 
			While[
            oldderivative == 
              derivative, 
				\(k++\); 
				derivative = 
              derivative /. 
                ListRules[[k]]]; 
          displayRuleUsed[
            RuleNames[[
              k]]]; 
			displayDerivative[
            derivative]]; 
		∂\_x f]\)

\(RunD[f_, x_] := 
      Module[{derivative}, 
		derivative = d[f, x]; 
		displayStart[
          derivative]; 
        While[\(\(! \((FreeQ[derivative, d])\)\)\), 
          derivative = derivative /. AllRules; 
			displayDerivative[
            derivative]]; ∂\_x f]\)

\(displayStart[expr_]\  := 
	
      StylePrint[expr, 
        "Output", 
			CellFrame -> 
          {{0, 0}, {0, 3}}, 
			Background\  -> 
          RGBColor[0.8, \ 1. , \ 0.8], 
			CellGroupingRules -> 
          "OutputGrouping", 
			CellMargins\  -> \ 
          {{Inherited, \ Inherited}, {0, Inherited}}, 
		CellEditDuplicate 
          -> False, 
		FontWeight -> Bold]\)

\(displayRuleUsed[expr_String]\  := 
	
      CellPrint[
        Cell[BoxData[expr], \ 
          "Output", 
			Background\  -> 
            RGBColor[0.8, \ 1. , \ 0.8], \ 
          FontColor\  -> \ 
            RGBColor[0.6, \ 0, \ 0.6], 
			FontSlant\  -> \ 
            "Italic", 
			\ \ CellFrame -> \ 
            False, 
				CellMargins -> 
            {{Inherited, Inherited}, {0, 0}}, 
          CellFrameMargins\  -> \ 
            {{100, \ Inherited}, {0, \ 0}}, 
		CellEditDuplicate -> 
            False, 
		FontWeight -> Bold]]\)

\(displayDerivative[expr_] := 
	
      CellPrint[
        Cell[BoxData[
            RowBox[{InterpretationBox["=", Sequence[]], "    ", 
                MakeBoxes[expr, \ StandardForm]\ }]], \ 
          "Output", 
			Background\  -> 
            RGBColor[0.8, \ 1. , \ 0.8], \ 
          Evaluatable\  -> False, 
				CellMargins -> 
            {{Inherited, Inherited}, {0, 0}}, 
          CellFrame -> False, 
		CellEditDuplicate -> 
            False, 
		FontWeight -> Bold]]\)

If[wasSpellCheckOn, On[General::spell1]]