(C) Copyright 1994 D.C.M. Burbulla and  C.T.J. Dodson
       Notebook to support the book
     Self-Tutor for Computer  Calculus

Using Mathematica 2

by
     D. C. M. Burbulla and C. T. J. Dodson, University of Toronto
 Published by:    
    Prentice-Hall Canada  (English)   ISBN 0130152803     and 
     Prentice-Hall Japan  (Japanese) ISBN4-8101-8558-3 C3055 P2880E

The Derivative

Our new functions allow investigation of secants, tangents and normals.

Chap3.m

secant/: secant[f_, a_][p_] := 
         (one = Block[{x}, Plot[{f[x], (f[p] - f[a]) (x - a)/(p - a) + f[a]}, 
	 {x, a - 1.5, a + 1.5}, Axes -> None, AspectRatio -> Automatic,
	 DisplayFunction -> Identity]];
	 two = ListPlot[{{a, f[a]}, {p, f[p]}}, DisplayFunction -> Identity];
	 Show[one, two, DisplayFunction -> $DisplayFunction]) 
	 
secant::usage = "secant[f, a][p] plots f and the secant joining (a, f[a]) and (p, f[p])."


tangent/: tangent[f_, a_][x_] := Derivative[1][f][a]*(x - a) + f[a]


normal/: normal[f_, a_][x_] := (a - x)/Derivative[1][f][a] + f[a] 


tangentplot/: tangentplot[f_, a_, {b_, c_}] := 
              Plot[{f[x], tangent[f, a][x]}, {x, b, c}, 
	      AspectRatio -> Automatic]
	      

normalplot/: normalplot[f_, a_, {b_, c_}] := 
             Plot[{f[x], normal[f, a][x]}, {x, b, c}, AspectRatio -> Automatic] 


tangentfamily/: tangentfamily[f_, {a_, b_, stepsize_:0.1}] := 
                (one = Block[{x}, Plot[f[x], {x, a, b}, 
		AspectRatio -> Automatic, DisplayFunction -> Identity]];
		two = Block[{i, x}, Plot[Release[Table[tangent[f, i][x], 
		{i, a, b, stepsize}]], {x, a, b},
		DisplayFunction -> Identity]];
		Show[one, two, DisplayFunction -> $DisplayFunction])
		
tangentfamily::usage = "tangentfamily[f, {a, b, stepsize}] plots f and its tangents on 
the interval {a, b}, plotting one tangent per stepsize in the interval {a, b}. The default 
stepsize is 0.1."


normalfamily/: normalfamily[f_, {a_, b_, stepsize_:0.1}, {c_, d_}] := 
               (one = Block[{i, x}, Plot[Release[Table[normal[f, i][x],
	       {i, a, b, stepsize}]], {x, a, b}, PlotRange -> {c, d},
	       AspectRatio -> Automatic, DisplayFunction -> Identity]];
	       two = Block[{x}, Plot[f[x], {x, a, b},
	       DisplayFunction -> Identity]];
	       Show[one, two, DisplayFunction -> $DisplayFunction])
	       
normalfamily::usage = "normalfamily[f, {a, b, stepsize}, {c, d}] plots f and its normals 
on the interval {a, b}, with PlotRange set to {c, d}. One normal is plotted per stepsize 
in the interval {a, b}; the default stepsize is 0.1."


wire/: wire[x_] := (line = Block[{t}, Plot[70, {t, 0, 100}, 
                   DisplayFunction -> Identity]];
		   point = ListPlot[{{x, 70}}, DisplayFunction -> Identity];
		   circle = Block[{t}, ParametricPlot[ 
		   {x*Cos[t]/(2*Pi) + 25, x*Sin[t]/(2*Pi) + 25}, {t, 0, 2*Pi},
		   AspectRatio -> Automatic, DisplayFunction -> Identity]];
		   square = ListPlot[{{75 - (100 - x)/8, 25 - (100 - x)/8},
		   {75 - (100 - x)/8, 25 + (100 - x)/8},
		   {75 + (100 - x)/8, 25 + (100 - x)/8},
		   {75 + (100 - x)/8, 25 - (100 - x)/8},
		   {75 - (100 - x)/8, 25 - (100 - x)/8}},
		   PlotJoined -> True, DisplayFunction -> Identity];
		   Show[circle, square, line, point, Axes -> None,
		   PlotRange -> {{0, 100}, {0, 80}}, PlotLabel ->
		   N[x^2/(4*Pi) + (100 - x)^2/16] " = combined area of circle 
		   and square", DisplayFunction -> $DisplayFunction])
		   
		   
ladder/: ladder[t_] := ListPlot[{{10 t, 0}, {0, Sqrt[41^2 - (10 t)^2]}},
                       PlotRange -> {{0, 41}, {0, 41}},
		       PlotJoined -> True, AspectRatio -> Automatic]

Tangents and Normals

This section consists of some examples covering typical problems that involve tangents 
and normals. We shall make use of the defined functions in Chap3.m to simplify our 
calculations and to sketch graphs whenever possible. We shall make extensive use of 
the functions tangent and normal. 
We can remind ourselves of their syntax by querying Mathematica:

?tangent

?normal

Note that in the definition of normal no special consideration is given for the case f '(a) = 0,  
where, of course, the normal is a vertical line and the formula for normal no longer makes 
sense.

Example 1.  Find the tangents to the hyperbola xy = 1 passing through the point (-1, 1). 
We work as follows:

Clear[f]; f[x_] := 1/x

Solve[tangent[f, a][-1] == 1, a] (* (-1, 1) is on tangent*)

tangent[f, a][x] /. %

Simplify[%]

Plot[{%[[1]], %[[2]], f[x]}, {x, -2, 3}, 
PlotRange -> {-4, 6}];

Example 2.  Find all of the tangents to the graph of  y =  2 x3 + 13 x2 + 5 x + 9 passing through 
the origin. The point (0, 0) is not on the cubic; there could be as many as three tangents to the 
cubic passing through (0, 0).

Clear[f]; f[x_] := 2 x^3 + 13 x^2 + 5 x + 9

Solve[tangent[f, a][0] == 0, a]

tangent[f, a][x] /. % // Simplify

Plot[{%[[1]], %[[2]], %[[3]], f[x]}, {x, -3.5, 2}, 
PlotRange -> {-5, 70}];

The remaining examples will be slightly more complicated, using more of Mathematica's 
capabilities.

Example 3.  One thing we can do is graph lots of tangents. There is a defined function, 
tangentfamily, in Chap3.m, to do this:

?tangentfamily

We shall illustrate it on the parabola with equation  y =  x 2 .

Clear[f]; f[x_] := x^2

tangentfamily[f, {-3, 3}];

Show[%, AspectRatio -> Automatic, PlotRange -> {0, 9}];

Example 4.  In addition to tangentfamily, there is a similar defined function showing 
normals to a curve, called normalfamily:

?normalfamily

We can illustrate this function first on the parbola  y =  x 2 :

normalfamily[f, {-6, 6}, {-6, 6}];  (*  f as before  *)

Note the interesting pattern the normals create: they highlight a curve which is the envelope of 
all the normals. Let us plot the normals to  y  =  sin x  to see if an interesting pattern emerges 
again:

normalfamily[Sin, {-6, 6}, {-6, 6}];

We shall consider further the envelope of all the normals to a curve in Section 5.2.

Example 5.  Minimum distance in the plane is measured perpendicularly. Thus we can use 
normals to the find the closest pair of points, one from each curve, on two different curves. 
Consider the two parabolas   f  and  g  with equations

                       f(x)  =  x 2  +  2 x  +  2     and       g(x)  =  - x 2  +  6 x  -  4.

Clear[f]; f[x_] := x^2 + 2 x + 2; g[x_] := -x^2 + 6 x - 4

Plot[{f[x], g[x]}, {x, -2, 4}, AspectRatio -> Automatic];

Suppose the points  (a, f(a)) and  (b, g(b)) are the two points closest together. The first 
point must be on the normal to  g  at  x  =  b; the second point must be on the normal to 
 f  at  x  =  a.  This gives us a system of two equations to solve for  a  and  b. Try solving
 for the coincidence of the normals to the two curves.

Applications of the Derivative

As one example of using animations to illustrate a typical calculus problem, we consider the following question:
	
	A wire of length 100 cm is to be cut into two pieces. One piece is bent into a circle, the other into a square. How should the cut be made to maximize the sum of the areas of the square and the circle? How should it be done to minimize that sum? 
	
If the cut is made at   x  and   a(x)  is the total area of the enclosed by the two pieces as described, then:

a[x_] := x^2/(4 Pi) + (100 - x)^2/16

Plot[a[x], {x, 0, 100}];

You can see that the maximum total area is at the right end point x =100 (corresponding to bending the whole wire into a circle), and the minimum total area is at the critical point. We can find the critical point by setting a'(x) = 0:

Solve[a'[x] == 0, x]

% // N

To illustrate this problem more thoroughly we have defined a function called  wire :

wire[30];

wire[45];

wire[70];

wire[90];

<<Graphics`Animation`

Animate[wire[i], {i, 0, 100, 5}]