Calculus Lab #3:  Derivatives

Goals:	1.  Learn how to evaluate derivatives in Mathematica
	2.  See step-by-step derivatives
	3.  Solve rate change problems
	4.  Evaluate higher-order derivatives
	5.  Review for Exam #1

1.  Simple derivatives in Mathematica

Let's begin by learning how to evaluate derivatives using Mathematica.  The easiest way to do this is to use the Calculus options on the Basic Calculations palette.  For example, consider the function f(x) = sin(x).

Clear[x];
f[x_] = Sin[x] ;
Plot[f[x], {x, -2 π, 2  π}]

We take the derivative of this function with respect to x by using any of the following:

(∂_x) f[x] 
f'[x]

We can then ask Mathematica to evaluate this derivative at any value of X.

N[Cos[2]]
N[Cos[0]]
N[Cos[(π/2)]]

Task #1:  Select two even-numbered problems from section 2.2.  Define the functions, plot the functions, and then have Mathematica evaluate the derivatives of these functions.

2.  Derivative rules (step-by-step)

Using this notebook (which I downloaded from the internet somewhere), you can have Mathematica show the steps used to find the derivative of a function.  Here are the derivative rules that Mathematica knows:

PrintRules

Let's go through one example.  Consider the function f(x) = x + cos(x).
We know that to find this derivative, we need to use the linearity rule (adding derivatives).

In this syntax, we will:
	1.  Calculate the derivative
	2.  Fully simplify the derivative
	3.  Get the step-by-step derivative rules used. (WalkD)

(∂_x) (x + Cos[x])
FullSimplify[(∂_x) (x + 
            Cos[x])] 
WalkD[x + Cos[x], x];

Here are a few more examples:

(∂_x) ((2  x) Cos[x] - 
          2  Sin[x])
FullSimplify[(∂_x) (2  xCos[x] - 
            2  Sin[x])] 
WalkD[2  xCos[x] - 2  Sin[x], x];

(∂_x) (((5  x - 2)/((x^2) + 1)))
FullSimplify[(∂_x) (((5  x - 2)/((x^2) + 1)))] 
WalkD[((5  x - 2)/((x^2) + 1)), x];

(∂_x) ((((3  x - 2  (x^2)))^3))
FullSimplify[(∂_x) ((((3  x - 2  (x^2)))^3))] 
WalkD[(((3  x - 2  (x^2)))^3), x];

Task #2:  Select two even-numbered problems from section 2.3 and two even-numbered problems from section 2.4.  First, find the derivatives by hand.  Then check your answers using Mathematica.

3.  Don't Jump!

Ever since you started your calculus class, you've suffered from blinding headaches.  Nothing helps -- acupuncture, drugs, counseling, you've tried them all, but the headaches get worse and worse.  The pain is unbearable and you've decided to end it all.

You drive to the middle of a bridge and climb over the safety rail, 400 feet above the water.  With that pain, life is not worth living, so you fling your calculus book (which you carry everywhere) over the edge and jump out after it.  Your height in feet over the water after t seconds is given by the function h(t) = 400-16(t^2).

1.  Define the function in Mathematica.
2.  What is the domain of this function?  What is the range?
3.  Plot this function over its domain.
4.  How long will it be until you hit the water?
5.  You had lots of diving lessons when you were younger.  If you are traveling with a velocity less than 200 feet per second, you can survive the plunge.  Will you survive?
6.  Find the tangent line to this function at the point (3, 256)

SOLUTIONS

h[x_] = 400 - 16  (x^2)
"2. Domain = (0 ≤ x ≤ 5)"
"2. Range = (0≤ y ≤ 400)"
"3. Plot"
Plot[h[x], {x, 0, 5}, 
      PlotRange -> {0, 400}]
"4. I will hit the water in 5 seconds."
(∂_x) h[x]
"5. Instantaneous Velocity at t=5"
-32 (5)
"I'll survive!"
"6. Tangent line: y=-96x+256"
t[x_] = -96 x + 256

6.  Reflection

At the end of each Mathematica session, I want you to write a sentence or two describing what you learned (or just list important concepts from this lab).  You can later use these reflections as a reference for other Mathematica Labs.

Print out your answers to the lab questions and turn them in for credit.

Implementation (Don't bother opening this)

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]]