Convergence of Secant Slopes to the Derivative Function

derivApprox[g_,indvar_,a_,b_,intervals_]:=Module[{f,x,alocal,bloc,n,h,flist,secantslopes,p1,p2,p3,p4,yprime},ClearAll[n,h,f,yprime,flist,secantslopes,p1,p2,p3];f[x_]:=g/.indvar->x;yprime=(∂_x)f[x];alocal=a;bloc=b;n=intervals;h=((bloc-alocal)/n);flist=Table[{alocal+i h,f[alocal+i h]},{i,0,n}];secantslopes=Table[{alocal+i h,(1/h)(f[alocal+(i+1) h]-f[alocal+i h])},{i,0,n-1}];p1=Plot[f[x],{x,alocal,bloc},DisplayFunction->Identity];p2=ListLinePlot[flist,PlotStyle->{PointSize[0.025`],RGBColor[0,0,1]},DisplayFunction->Identity];p3=ListPlot[flist,PlotStyle->{RGBColor[1,0,0]},DisplayFunction->Identity];
Print[Show[p1,p2,p3,DisplayFunction->$DisplayFunction,AxesLabel->{indvar,{"y","sample points & secants"}}]];p4=ListPlot[secantslopes,PlotRange->All,PlotStyle->{PointSize[0.02`],RGBColor[1,0,0]},DisplayFunction->Identity];p5=Plot[yprime,{x,alocal,bloc},DisplayFunction->Identity];Print["     "];Print["The derivative of ",g/.x->indvar," is  ",yprime/.x->indvar];Print["     "];Show[p4,p5,DisplayFunction->$DisplayFunction,AxesLabel->{indvar,{"\!\(\*SuperscriptBox[\(y\), \(′\)]\)","and secant slopes"}}]]

Introduction

OBJECTIVE: To visualize the secant line between successive points on a curve, observe what happens as the distance between successive points becomes small, compare the graph of successive secant line slopes with the graph of the derivative function, and apply the definition of the derivative in computing it.

One of the central problems of calculus is measuring and quantifying slopes on the graphs of nonlinear functions. In this module, you will explore two methods for approximating these slopes and see how the approximations approach the derivative function in the limit. 

The first method is to extract a sample of evenly spaced points from the graph of a function y=f(x), calculate the slopes of the secant lines between the sample points, and graph the secant slope values together with the derivative function f'(x). You will see that as the number of sample points increases and the horizontal spacing between consecutive points decreases, the set of discrete secant-slope values gets closer to the derivative function. 

The second method for approximating the slopes on the graph of a function is to take every value of x in the domain of the function y=f(x) and calculate the slope of the secant line between the two points (x,f(x)) and (x+h, f(x+h)) for a fixed nonzero value of h. This gives a new function of x and h, namely, the function of secant slopes for any x in the domain of f. This secant-slope function is plotted together with the derivative f'(x). You will see that as the value of h gets closer to 0, the secant slope function gets closer to f'(x). Finally, you will see how to use Mathematica to take the limit of the secant-slope function as h approaches 0.

Before you begin this module, we recommend that you refer to "Tangents and Secants," a JAVA applet included in this supplement. This applet allows you to explore how the slopes of secant lines converge to the slope of the line tangent to a curve at a point.

Technology Guidelines

NOTE:  If you have just finished a module, restart Mathematica or Quit Kernel under the Evaluation pull-down menu before executing a new module.
TO OPEN CELLS, click on the arrow to the left of the cell or put your cursor on the right cell bracket and double click. To open all cells, highlight the bracket on the far right and select Grouping under the Cell pull-down menu and select Open All Subgroups.
TO STOP AN EXECUTION
Select the Evaluation pull-down menu and click on Abort Evaluation.
ORDER OF EXECUTION
Execute cells in the order given. Do not skip any Input cells within a given notebook.
SAVING NOTEBOOKS
You can save anytime to any directory you choose, and it is wise to save often. However, before you do your final save to reduce the size of your file, it is a good idea to delete all your output by selecting the Delete All Output selection under the Cell pull-down menu.
EXPERIENCING MAJOR PROBLEMS
Save if appropriate, then shut down Mathematica and start it up again.

Part I: Secant Slopes from a Discrete Set of Sample of Points

The function derivApprox[ ] extracts n evenly spaced sample points from a differentiable function and calculates the slopes of the secant lines between consecutive pairs of sample points. It then plots the function, the sample points, and the secant lines on one graph. A second graph is plotted to show the slopes of the secant lines in comparison with the derivative of the function. As the number of sample points increases, the secant slopes get closer to the derivative function. 

(Note: derivApprox[ ] was written specially for this module and is not a built-in Mathematica command.)

The arguments of derivApprox[f_, x_, a_, b_, n_] are: f, the function you wish to analyze; x, the independent variable; a, the beginning of the domain interval; b, the end of the domain interval; and n, the number of evenly spaced sample points. The following example illustrates how derivApprox[ ] works. All that you need to do is change the arguments.

derivApprox[x*Sin[x],x,-2*Pi,2*Pi,12]

You Try It: On Differentiable and Non-Differentiable Functions

1. Change the value of n (the last argument of derivApprox[ ]) and describe what you see. To do this systematically, you might want to repeatedly double the number of sample points. That is, let n=12, 24, 48, 96,. . . .

2. Try derivApprox[ ] on some differentiable functions that you pick. Be sure to use several sample sizes so that you can see how the secant slopes function converges on the derivative. Here is an example of a function that has a very interesting derivative. See if you can see what makes it special.

f[x_]=(E^x)

Plot[f[x],{x,-2,2},AxesLabel->{"x","y"}]

derivApprox[f[x],x,-2,2,24]

3. Try derivApprox[ ] on some functions that are not differentiable at one or more points. Here is an example.

f[x_]=Which[x≤2,2x,x>2,4x-(x^2)]

\[MathematicaIcon] About Mathematica

Plot[f[x],{x,0,4},AxesLabel->{"x","y"}]

derivApprox[f[x],x,0,4,12]

The derivApprox[ ] command gives us a formula for the derivative function that is printed above. Is it correct in this case? To answer this question you should consider what happens at x=2.

Part II: Secant Slopes at Every Point

Another approach to approximating the slopes on the graph of a function is to take every value of x in the domain of the function y=f(x) and calculate the slope of the secant line between the two points (x,f(x)) and (x+h, f(x+h)) for a fixed nonzero value of h. This gives a new function of x and h, namely, the function of secant slopes for any x in the domain of f. We form this secant-slopes function in the next cell.

msecant[x_,h_]:=((f[x+h]-f[x])/h);

\[MathematicaIcon] About Mathematica

Now, we graph the secant slopes for a specified h value together with the derivative function.

f[x_]=x*Sin[x]

fprime[x_]=D[f[x],x]

h=1;
Plot[{msecant[x,h],fprime[x]},{x,-2 π,2 π},AxesLabel->{"x","y' & (Subscript[m, sec])"},PlotStyle->{{RGBColor[1,0,0]},{RGBColor[0,0,1]}}]

The red curve gives the secant slope for each value of x, with h=1, and the blue curve gives the value of the derivative, that is, the slope of the tangent to the graph of f(x) for each value of x.

You Try It: On Differentiable and Non-Differentiable Functions

1. See what happens to the two graphs in the preceding cell when you decrease the value of h. Also, try some negative values of h. To be systematic, you should repeatedly cut h in half. For example, you could use h=-1,-0.5,-0.25, -0.125 for negative values of h. Describe what you observe.

2. Repeat the steps in Part II on some other differentiable functions that you pick. Be sure to decrease values of h so that you can see how the secant-slopes function, msecant[ ], converges on the derivative function as h approaches zero. Here is an example.

f[x_]=Sin[x]

fprime[x_]=D[f[x],x]

h=0.01;
Plot[{msecant[x,h],fprime[x]},{x,0,2 π},AxesLabel->{"x","y' & (Subscript[m, sec])"},PlotStyle->{{RGBColor[1,0,0]},{RGBColor[0,0,1]}}]

3. Repeat the above steps on some functions that are not differentiable at one or more points. Here is an example.

f[x_]=Which[x≤2,2x,x>2,4x-(x^2)]

Plot[f[x],{x,0,4},AxesLabel->{"x","y"}]

fprime[x_]=D[f[x],x]

h=1;
Plot[{msecant[x,h],fprime[x]},{x,-2 π,2 π},AxesLabel->{"x","y' & (Subscript[m, sec])"},PlotStyle->{{RGBColor[1,0,0],Thickness[0.01`]},{RGBColor[0,0,1]}}]

Note that we make the msecant[ ] graph a little thicker so that it is not hidden behind the graph of the derivative function.

Mathematica's derivative command, D[ ], gives us a formula for the derivative function above. Is it correct in this case? To answer this question, you should consider what happens at x=2.

Part III: Taking It to the Limit

The slope of the tangent to the graph of y=f(x) for each value of x is given by (Underscript[lim , h->0]) ((f(x+h)-f(x))/h) provided, of course, that the limit exists. This gives the derivative function f'(x). In the next group of cells, we use this definition to find the derivative function for a specific function, y=f(x).

Clear[f,h,x];

First, we pick a function.

f[x_]=a (x^2)+b x+c         (* a, b, and c are constants *)

Next, we calculate the secant slopes, that is, the difference-quotient.

((f[x+h]-f[x])/h)//Simplify

And we take the limit to form the derivative function.

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

You Try It: Finding Derivatives Using the Definition

Repeat the steps in Part III to find the derivatives of several functions that you pick. Here are some suggestions.

1. f(x)=(Sqrt[x])

2. f(x)=(1/(x^2))

3. f(x)=sin x

4. f(x)=A sin (a x) +B cos (b x)   where A, a, B, and b are constants

5. f(x)=tan x

6. f(x)=(e^x)

7. f(x)=(Ce^(a x))  where C and a are constants

\[MathematicaIcon] About Mathematica

The Which[ ] command can be used to form piecewise defined functions, as illustrated in the preceding cell. To learn more about the Which[ ] command, pull down the Help menu, select the Help Browser, and type Which. Go Back.

In the definition of msecant[ ], we use the delayed assignment with the symbol := (colon-equals). This is because we do not want the definition to store a secant slope definition for a specific f[x_]; rather, we want it to use the definition of f[x_] that is current when msecant[ ] is called. To learn more about the delayed assignments, see the "Overview of Mathematica" module that is included with this set of modules or pull down the Help menu, select the Help Browser, and type :=. 
Go Back.