Derivatives as Limits
with Mathematica

by
K. D.  Stroyan
University of Iowa

copyright 2007 by Keith Stroyan - All rights reserved.

Notebook Overview

This NoteBook uses Mathematica to help you solve the preview exercises on derivatives from Chapter 1 of the main text.  The goal of the NoteBook is for you to solve the Exercises included in this NoteBook below.  The sample computations help you do this.

To solve these exercises, all we are asking you to do is copy a working computation and then modify it to perform a similar calculation for your example.  One important technical goal is for you to learn to copy a group of cells and modify it to perform a similar computation.

Additional information on numerical computation with Mathematica can be found in the NoteBook MmaNumerics, graphical computation in MmaGraphics in the folder ComputerIntro of Chapter 1.

References to the Text

This NoteBook goes with Section 1.4 of the text, Exercise 1.4.4

Step-by-step Slopes of y = f[x] = (x^3)

Here is the way to define a function and show it's plot: (<Enter> it.)

Clear[f,x];
f[x_]:=(x^3);
curve=Plot[f[x],{x,-1,4},AspectRatio->Automatic,PlotRange->{-1,4},AxesLabel->{"x","y"},AxesOrigin->{0,0}]
Print["y = f[x] = ",f[x]];

Two Points Near x = 1

We choose a point  (x,f[x])  and a nearby point (x+dx,f[x+dx]):
(<Enter> the next  set of commands)

x=1;
dx=(1/2);
m=((f[x+dx]-f[x])/dx);
points=Graphics[{AbsolutePointSize[5],{Point[{x,f[x]}],Point[{x+dx,f[x+dx]}]}}];
Show[curve,points]
Print["Slope between",{x,f[x]}," and ",{x+dx,f[x+dx]}," = ",m," ≈ ",N[m]];

Now make  dx  smaller by <Enter>ing the next Input cell:

dx=(1/4);
m=((f[x+dx]-f[x])/dx);
points=Graphics[{AbsolutePointSize[5],{Point[{x,f[x]}],Point[{x+dx,f[x+dx]}]}}];
Show[curve,points]
Print["Slope between",{x,f[x]}," and ",{x+dx,f[x+dx]}," = ",m," ≈ ",N[m]];

Now make  dx  even smaller and draw a line through the points:

dx=(1/8);
m=((f[x+dx]-f[x])/dx);
points=Graphics[{AbsolutePointSize[5],{Point[{x,f[x]}],Point[{x+dx,f[x+dx]}]}}];
Clear[y,z];
y[z_]:=f[x]+(((f[x+dx]-f[x]) (z-x))/dx);
line=Plot[y[z],{z,x-1,x+1},DisplayFunction->Identity];
Show[curve,points,line,DisplayFunction->$DisplayFunction]
Print["Slope between",{x,f[x]}," and ",{x+dx,f[x+dx]}," = ",m," ≈ ",N[m]];

Two Points Near x = 2/3

Now we choose a point  (x,f[x])  and a nearby point (x+dx,f[x+dx]): (<Enter> the next  set of commands)

x=(2/3);
dx=(1/2);
m=((f[x+dx]-f[x])/dx);
points=Graphics[{AbsolutePointSize[5],{Point[{x,f[x]}],Point[{x+dx,f[x+dx]}]}}];
Show[curve,points]
Print["Slope between",{x,f[x]}," and ",{x+dx,f[x+dx]}," = ",m,"≈ ",N[m]];

Now make  dx  smaller by <Enter>ing the next Input cell:

dx=(1/4);
m=((f[x+dx]-f[x])/dx);
points=Graphics[{AbsolutePointSize[5],{Point[{x,f[x]}],Point[{x+dx,f[x+dx]}]}}];
Show[curve,points]
Print["Slope between",{x,f[x]}," and ",{x+dx,f[x+dx]}," = ",m," ≈ ",N[m]];

Now make  dx  even smaller and draw a line through the points:

dx=(1/8);
m=((f[x+dx]-f[x])/dx);
points=Graphics[{AbsolutePointSize[5],{Point[{x,f[x]}],Point[{x+dx,f[x+dx]}]}}];
Clear[y,z];
y[z_]:=f[x]+(((f[x+dx]-f[x]) (z-x))/dx);
line=Plot[y[z],{z,x-1,x+1},DisplayFunction->Identity];
Show[curve,points,line,DisplayFunction->$DisplayFunction]
Print["Slope between",{x,f[x]}," and ",{x+dx,f[x+dx]}," = ",m," ≈ ",N[m]];

Exercise on Secants of f[x] = (x^2)

Copy the whole step-by-step computation of the slope of  y = (x^3) and modify it to work through the step-by-step computation of the slope of f[x_] := (x^2).

To copy a group of cells (you want the one starting with "Step-by-step Slopes of y = f[x] = (x^3)") , move the cursor to the line at the right of the window that marks the collection you wish to copy and click once to highlight it.  Leaving it highlighted, click Edit on the main Mathematica menu and click Copy inside that menu.

Next, move the mouse to the empty cell below here and click once to get an insert cursor (or horizontal line, either works).  Then click Paste from the Edit menu.  Do this cut and past now for the  three parts of the step-by-step example for  y = f[x] = (x^3).  Then edit the typing to compute the example with y = f[x] = (x^2) below here.

The Symbolic Expression for Limiting Slope

The slope of the segment between the points (x,f[x])  and (x+dx,f[x+dx]) tends to the limit  f'[x].  The graphical examples above for  y = f[x] = x^3 that we did and the ones you did for  y = f[x] = x^2 are clear, but the numerical results are not so clear.

Here is the numerical data for  y =(x^3) (copied from above):

Values of the secant slope at  x = 1

m=((f[x+dx]-f[x])/dx)
dx=(1/2)⇒m≈4.75
dx=(1/4)⇒m≈3.8125
dx=(1/8)⇒m≈3.39062

Values of the secant slope at  x = 2/3

m=((f[x+dx]-f[x])/dx)
dx=(1/2)⇒m≈2.58333
dx=(1/4)⇒m≈1.89583
dx=(1/8)⇒m≈1.59896

Limiting Slopes with the formula:

We learned in Chapter 1 that

Limit[((f[x+Δx]-f[x])/(Δx)),Δx->0]=f'[x]=3 (x^2)

This formula makes the limiting values of the data above clear.  Here is the computation using that result:

Values of the limit secant slope   f'[x] = Limit[ (f[x+dx] - f[x])/dx , dx ->0 ]

f'[x]=3 (x^2)
at  x=1
	f'[x]=f'[1]=3*(1^2)=3
at  x=1=2/3
	f'[x]=f'[2/3]=3*((((2/3)))^2)=((2^2)/3)=(4/3)≈1.33333
Compare these simple computations with the numerical results summarized directly above.

The Symbolic Limit

To see what this limit is yourself, you can simplify the algebra.  With Mathematica, this goes as follows:

Clear[f,x,dx];
f[x_]:=(x^3);
f[x+dx]-f[x]
deltaY=Expand[f[x+dx]-f[x]]
(deltaY/dx)
simpleQuotient=Simplify[(deltaY/dx)]

Limit[simpleQuotient,dx->0]

Analytical Convergence to the Derivative

Actually, Mathematica can do the limit computation itself without you helping to simplify the result.  Here's how:

Limit[((f[x+Δx]-f[x])/(Δx)),Δx->0]

Mathematica can even compute the derivative function directly.  Here's how:

f'[x]

Exercise on f[x] = (x^2)  =>  f'[x] = 2x

Use the mouse to copy the computation of the symbolic limit of the slope of  y = (x^3) and modify it to work through the computation of the limit of the slope of f[x] = (x^2).

Two Limit Animations for the Derivative

There are two ways to "see" the limit described in Chapter 1.  Each of these is animated below.  The first computer movie shows the secant line approaching the tangent and the second shows the whole graph of the derived function  f'[x]  being approximated by smaller and smaller difference quotients.

Animation of the Secant Limit

The next cell makes a sequence of plots for smaller and smaller values of the difference  dx.  After you <Enter> the computation, you can animate the result by clicking on the cell bar that contains all the graphs and then pressing the <command> and <y> keys at the same time (or choose "animate selected graphics" from the Graph menu.)  This movie shows the approximating lines converging to the tangent line of slope  f'[x].

Clear[f,x];
f[x_]:=(x^3);
Print["y = f[x] = ",f[x]];
x=(2/3);
Print["Slope ",N[f'[x]]," at x = ",N[x]];
curve=Plot[f[z],{z,x-2,x+2},AspectRatio->Automatic,PlotRange->{f[x]-2,f[x]+2},AxesLabel->{"x","y"},AxesOrigin->{0,0},DisplayFunction->Identity];
y[z_]:=f[x]+(((f[x+dx]-f[x]) (z-x))/dx);
Manipulate[dx=(2^(-n));m=((f[x+dx]-f[x])/dx);points=Graphics[{AbsolutePointSize[5],{Point[{x,f[x]}],Point[{x+dx,f[x+dx]}]},Text[N[m],{x+0.5`,f[x]}]}];line=Plot[y[z],{z,x-1,x+1},AspectRatio->1,PlotRange->{f[x]-1,f[x]+1},Axes->None];Show[curve,points,line],{n,1,6},SaveDefinitions->True]

Graphical Convergence to the Derivative Function

Convergence of

Limit[((f[x+Δx]-f[x])/(Δx)),Δx->0]

can be seen by graphing the function

(f[x+dx] - f[x])/dx  together with  f'[x]

Here's how:

Two plots:

Clear[f,x];
f[x_]:=(x^3);
Print["Quotients of  y = f[x] = ",f[x]," with derivative  f'[x] = ",f'[x]];
dx=(1/2);
Print["dx = ",dx];
Plot[{((f[x+dx]-f[x])/dx),f'[x]},{x,-1,2},AspectRatio->1,PlotRange->{-1,2},AxesLabel->{"x","y"}]

Here's another:

dx=(1/4);
Print["dx = ",dx];
Plot[{((f[x+dx]-f[x])/dx),f'[x]},{x,-1,2},AspectRatio->1,PlotRange->{-1,2},AxesLabel->{"x","y"}]

Animation of f[x+dx] - f[x])/dx  -> f'[x]

The next cell makes a sequence of plots for smaller and smaller values of the difference  dx.

Clear[f,x];
f[x_]:=(x^3);
Print["Quotients of  y = f[x] = ",f[x]," tend to  f'[x] = ",f'[x]];

Manipulate[dx=(2^(-n));Plot[{((f[x+dx]-f[x])/dx),f'[x]},{x,-1,2},AspectRatio->1,PlotRange->{-1,2},AxesLabel->{"x","y"},PlotLabel->{"dx = ",dx}],{n,1,6}]

You can animate the result by clicking on the cell bar that contains all the graphs and then pressing the <command> and <y> keys at the same time (or choose "animate selected graphics" from the Graph menu.)  This movie shows the approximating functions converging to the function  f'[x].

Exercise 1.4.4: Convergence of Derivatives

Copy the convergence animations for  y = (x^3)  above  and modify them to illustrate both of the convergences of the following derivatrives:
	
if  y = f[x] = (x^2),  then  (dy/dx) = f'[x]   =  2 x

if  y = f[x] = (x^4),  then  (dy/dx) = f'[x]  =  4 x^3

if  y = f[x] = (x^3)-(x^2)-5 x,  then  (dy/dx) = f'[x]  = 3 (x^2)-2 x-5

if  y = f[x] = Sin[x],  then  (dy/dx) = f'[x]  = Cos[x]

if  y = f[x] = Log[x],  then  (dy/dx) = f'[x]  =  (1/x)

Tangent Summary

Once we know the general formula for the derivative function,  f'[x], we can use it to plot the tangent line as in the following Input cell.  <Enter> it before you try to read the code that produces the curve, point, line and labels.

NOTE: We are comparing the CHANGE in f[ .]  given by  f[x+dx] - f[x] , as we change the input by  dx, to the linear function  dy = f'[x] dx.

Clear[x,f,y,dx,dy];
f[x_]:=(x^3)
x=(1/2);
y=f[x];
curve=Plot[f[x+dx]-f[x],{dx,-2,+2},AspectRatio->Automatic,PlotRange->{f[x]-2,f[x]+2},Ticks->None,AxesLabel->{"dx","dy"},AxesOrigin->{0,0}];
point=Graphics[{AbsolutePointSize[5],Point[{0,0}]}];
dy[dx_]:=f'[x] dx;
line=Plot[dy[dx],{dx,-1,+1}];
yAxis=Graphics[Line[{{-x,-y-1},{-x,-y+1}}]];
xAxis=Graphics[Line[{{-x-1,-y},{-x+1,-y}}]];
xlabel=Graphics[Text["x",{-x+1.2`,-y}]];
ylabel=Graphics[Text["y",{-x,-y+1.2`}]];
Show[curve,point,line,xAxis,yAxis,xlabel,ylabel]

Exercise on The Tangent Line

Copy the tangent summary plot above and modify it to plot the tangent line to  f[x] = Sin[x]  at  x = 1. (Recall from above that the derivative of sine is cosine.)

Nonlinear Lists (Optional computer solution to Exercise 1.4.1)

f[x_]:=(x^3);
xList={-1,0,1,1.5,2};
yList=f[xList]

pairs=Transpose[{xList,yList}]
plot1=ListPlot[pairs,Prolog->AbsolutePointSize[6],AxesLabel->{"x","y"},Ticks->None]
plot2=ListPlot[pairs,Prolog->AbsolutePointSize[6],AxesLabel->{"x","y"},Joined->True,Ticks->None]
Show[plot1,plot2]

curve=Plot[f[x],{x,-1,2},AxesLabel->{"x","y"},PlotRange->{-1.2`,8.2`},Ticks->None]
points=Graphics[{AbsolutePointSize[5],Point/@pairs}]
Show[curve,points]

Do[Print[((yList[[n+1]]-yList[[n]])/(xList[[n+1]]-xList[[n]]))],{n,1,Length[xList]-1}]