Local straightness and the derivative

Some programming.

\(\(\(
\)\(Needs["Graphics`Colors`"]

    \(Clear[plot, \ f, \ a, \ scale, \ plot1, \ TangentLine, \ plotboth, \ 
        scale];\)

    \(\(plot\  := \ 
Module[{}, \ 
Print["graph of y =  ", \ 
          f[x], \ " near "\ , \ {a, \ 
            f[a]}, \ ":"]; 
Plot[
          f[x], \ {x, \ a - scale, \ a + scale}, 
	
          PlotStyle\  -> {AbsoluteThickness[2], 
              Blue}, 
AxesOrigin\  -> \ {a, \ 
              f[a]}];
]\)\(
\)
    \)

    \(plot1\  := 

        Plot[f[x], \ {x, \ a - scale, \ a + scale}, 
	
          PlotStyle\  -> {AbsoluteThickness[2], 
              Blue}, 
AxesOrigin\  -> \ {a, \ 
              f[a]}, 
	DisplayFunction\  -> Identity];\)

    \(TangentLine\  := 

        Plot[flin[x], \ {x, \ a - scale, \ a + scale}, 
	
          PlotStyle\  -> {AbsoluteThickness[2], 
              Red}, 
AxesOrigin\  -> \ {a, \ 
              f[a]}, 
	DisplayFunction\  -> Identity];\)

    plotboth\  := \ 
      Module[{}, 
Print["graph of y =  ", \ 
          f[x], \ " near "\ , \ {a, \ 
            f[a]}, \ "with the tangent line at", {a, \ 
            f[a]}, \ ":"]; 
Show[{TangentLine, \ 
            plot1}, \ 
          DisplayFunction\  -> \ $DisplayFunction]\ ;
]

    \(fsec[a_, \ h_, \ x_]\  := \ 

        f[a]\  + \ \((x\  - \ a)\) \((f[a + h]\  - \ f[a])\)/h;\)

    \(SecantLine\  := 

        Plot[fsec[a, \ h, \ x], \ {x, \ a - scale, \ a + scale}, 
	
          PlotStyle\  -> {AbsoluteThickness[2], Green}, 
	
          DisplayFunction\  -> Identity];\)

    plotall\  := \ 
      Module[{}, 
Print["graph of y =  ", \ 
          f[x], \ " near "\ , \ {a, \ 
            f[a]}, \ "with the tangent line at", {a, \ 
            f[a]}, "and the secant line between x = "\ , \ 
          a, \ " and x = ", \ 
          a\  + \ h, ":"]; 
Show[{TangentLine, \ 
            plot1, \ SecantLine}, \ 
          DisplayFunction\  -> \ $DisplayFunction]\ ;
]\)\)\)

Define a function f[x].

Clear[f,  a];
f[x_]  :=  2  x  -  x^2 
a  =  1.6;
{a,  f[a]}
scale  =  2;
plot;

The graph of f[x] on different scales, near x = a:

\(\(\(
\)\(
\)\(\(scale\  = \ 2;\)

    \(plot;\)

    \(scale\  = \  .2;\)

    \(plot;\)

    \(scale\  = \  .02;\)

    \(plot;\)

    \(scale\  = \  .002;\)

    \(plot;\)\)\)\)

(Discussion Interlude.)

Numerical computation of the derivative f'[a] =  (Underscript[Limit, h  ->  0])((f[a + h]  -  f[a])/h), and definition of the tangent line.

\(For[n = 1, n ≤ 8, \(n++\), 
	h =  .1\^n; 
	
      Print[\(f[a + h] - f[a]\)\/h]]\)

The tangent line to the graph at x = a has slope f'[a].
Formula for the tangent line:       y = f[a] + m*(x-a).
Slope of tangent line  needs to be filled in here by hand!

\(\(\(m\  = \ \(-1.2\);\)\(
\)
    \)\)
flin[x_]  :=  Expand[f[a]  +   m * (x  -  a)]

Plot of function and tangent line together, on different scales, near 
x = a:

\(\(\(
\)\(\(scale\  = \ 2;\)

    \(plotboth;\)

    \(scale\  = \  .2;\)

    \(plotboth;\)

    \(scale\  = \  .02;\)

    \(plotboth;\)\)\)\)

Approximation to the tangent line by secant lines.

\(\(scale\  = \ 1;\)
\)
h  =   .7;
plotall;
h  =   .5;
plotall;
h  =   .3;
plotall;
h  =   .1;
plotall;
h  =   .05;
plotall;
h  =   .01;
plotall;

Exercise 1.

Change the defintion of the function f[x] to 
f[x_] = Sin[x] + 2x.  
Change the point a to  
a = 3.2.  
Then evaluate all the input (orange) cells of the notebook, up to the point where f'[a]  is obtained numerically.  
Fill in the appropriate value for the derivative in the cell defining the quantity m.  Then evaluate the remaining cells of the Notebook.

Exercise 2.

Treat f[x] = Sin[x^2]  at a = 1.43 similarly by evaluating the following cells:

Clear[f,  a, m];
f[x_]  :=  Sin[x^2];
a  =  1.43;
\(For[n = 1, n ≤ 8, \(n++\), 
	h =  .1\^n; 
	
      Print[\(f[a + h] - f[a]\)\/h]]\)

Fill in your best guess for the numerical value of the derivative f'[1.43] (and evaluate the cell!):

\(\(\(
\)\(m\  = \ \ \(-1.30571\)\)\)\)

Draw  the graph of the function, the tangent line, and several approximating secant lines by evaluating the following cell:

flin[x_]  :=  Expand[f[a]  +   m * (x  -  a)]
scale  =   .5;
h  =   .5;
plotall;
h  =   .2;
plotall;
h  =   .1;
plotall;
h  =   .05;
plotall;
h  =   .02;
plotall;
h  =   .01;
\(\(plotall;\)
\)