Laboratory 5 - Part 2
Tangent Lines and Derivatives

Introduction

In this lab we learn how to use Mathematica to calculate the derivative, both from the definition as well as from built-in commands. We then learn how to calculate the tangent line approximation to functions, and then generalize to quadratic and cubic function approximations.

5.1 Recall: Using Mathematica to Calculate the Derivative

For a function f(x), the derivative of f(x) at x=a, denoted by f'(a), is defined as

f'(a)=(lim_(h->0))(( f(a+h)-f(a) )/h)

if this limit exists. The quantity ((f(a+h)-f(a))/h) is called the difference quotient. For a fixed value of h, the difference quotient is the slope of the secant line obtained by connecting the two points (a,f(a)) and (a+h,f(a+h)). Let us calculate the derivative of the function f(x)=(x^4) at the point x=a directly from the definition. Begin by defining the function f(x).

f[x_]:=x^4

The expression for the difference quotient is ((f(a+h)-f(a))/h); call this d q.

dq=(f[a+h]-f[a])/ h

There is a Mathematica command Expand which expands powers and products. We shall apply it here to the difference quotient, and call the result e d q.

edq=Expand[dq]

Note that Mathematica performed the division by h as part of the expansion. The derivative of f(x) is the limit as h->0 of the difference quotient; we then use Mathematica to evaluate the limit.

Limit[edq,h->0]

We conclude that the derivative of f(x) at x=a is f'(a)=4(a^3).

Mathematica has a built-in command to calculate the derivative of a function. To find f'(x), given f(x), we simply execute the command:

f'[x]

As an alternative, we can execute the command D[f[x],x] to obtain the derivative of f with respect to x.

D[f[x],x]

Note that both of these answers agree with the result we obtained directly from the definition, which gave f'(a)=4(a^3).

As another example, let us find the derivative of f(x)=(x/(x-2)).

f[x_]:=x/(x-2)

f'[x]

Although Mathematica has calculated the derivative, it has presented the result in an unusual form. However, the commands Together or Simplify can be used to rewrite the result. The command Together puts terms in a sum over a common denominator, and factors the result. We implement it as follows:

Together[f'[x]]

On the other hand, the Simplify command tries some simple algebraic manipulations, and returns the simplest form that it finds. To implement it, execute the following command:

Simplify[f'[x]]

We conclude that the derivative of f(x)=(x/(x-2)) is f'(x)=-(2/(((-2+x))^2)).

5.2  Recall: Using the Derivative to Approximate a Function

We can use the derivative to construct approximations of functions. As an example, consider the function f(x)=-2sin x+(cos^2)((x/2)) on the interval [-π,π].

Clear[f,x]
f[x_]:=-2  Sin[x]+(Cos[x/2])^2

The graph of f(x) is given below.

Plot[f[x], {x,-Pi,Pi}]

Suppose that we want a simple approximation of this function near the point x=0. The simplest approximation we could give would be the constant function approximation (Subscript[f, 0])(x)=1, because f(0)=1. Unfortunately, this is not a very useful approximation, which we see when we compare the graphs.

f0[x_]:=1

Plot[{f[x],f0[x]},{x,-π,π},PlotStyle->{{},RGBColor[1,0,0]}]

A better approximation to our curve would be the tangent line to f(x). In particular, to find a good approximation to the function near the point x=0, we shall use the tangent line that passes through the point (0,f(0))=(0,1). To find the slope of the tangent line, we calculate the derivative at x=0

f'[0]

Since the tangent line has slope -2 and passes through the point (0,1), it has the equation y=1-2x. Define the approximation (Subscript[f, 1])(x) by (Subscript[f, 1])(x)=1-2x.

f1[x_]:=1-2x

Now plot f(x) and the approximation (Subscript[f, 1])(x) on the same graph.

Plot[{f[x],f1[x]},{x,-π,π},PlotRange->{-2,3},PlotStyle->{{},RGBColor[1,0,0]}]

The graph shows that the tangent line to the graph of the function at x=0 is a much better approximation to the original curve in the neighborhood of x=0 than the constant function approximation.
	To estimate how good the approximation actually is, we could calculate the error in the approximation, say |f(x)-(Subscript[f, 1])(x)|. A plot of the error function appears below.

Plot[Abs[f[x]-f1[x]],{x,-π,π}]

We would like to know for what values of x the error |f(x)-(Subscript[f, 1])(x)| is less than say 0.5. Examining the graph, we make the guess that this happens for -1≤x≤1, although we are not entirely sure about what happenns at x=-1 because the graph increases so rapidly at that point. To be sure, let us calculate the error for x=-1.0

Abs[f[-1.0]-f1[-1.0]]

In fact, the error when x=-1.0 is greater than 0.5; however, if we try x=-0.9instead, we find that

Abs[f[-0.9]-f1[-0.9]]

We conclude then that the error is less than 0.5 if -0.9≤x≤0.9. 
	We can also ask for what values of x the error is less than 0.05. To determine this, we again graph the error, but on a smaller interval, say [-1,1].

Plot[Abs[f[x]-f1[x]],{x,-1,1}]

Our first guess is that the error is less than 0.05 for -0.3≤x≤0.3. As in the case above, this guess for the lower limit might be incorrect because of the rate at which the error increases. To be certain, we calculate the error for x=-0.3

Abs[f[-0.3]-f1[-0.3]]

This information, together with the graph lets us conclude that the error is less than 0.05 for -0.3≤x≤0.3.

Experiment 5.2

For each of the following, find the tangent line approximation to the function at the indicated point. Show the function and the tangent line on the same graph. 

	1. f(x)=sin(x), near x=0.
	2. f(x)=(e^x), near x=0.
	3. f(x)=tan(x), near x=0.

5.3 Using Higher Derivatives to Approximate Functions

In the previous example we saw how we could use the derivative to obtain an approximation to the function f(x)=-2sin x+(cos^2)((x/2)) near the point x=0. In particular, we constructed the function (Subscript[f, 1])(x)=1-2x which has the property that f(0)=(Subscript[f, 1])(0) and f'(0)=(((Subscript[f, 1])))'(0). A better approximation might be a function (Subscript[f, 2])(x) chosen so that  f(0)=(Subscript[f, 2])(0), and f'(0)=(((Subscript[f, 2])))'(0) which also satisfies the condition f''(0)=(((Subscript[f, 2])))''(0). There are many functions that could satisfy these three conditions, including the original function f(x) itself. We want to choose a form for (Subscript[f, 2])(x) that allows it to satisfy our three conditions, but also is simple to understand and to compute. For this reason, we look for a function (Subscript[f, 2])(x) as a quadratic function, so that (Subscript[f, 2])(x)=a (x^2)+b x+c.To determine the values of the coefficients a, b, and c, we proceed as follows.
	First we define the function f(x)=-2sin x+(cos^2)((x/2)) and the function (Subscript[f, 2])(x)=a (x^2)+b x+c.

f[x_]:=-2Sin[x]+(((Cos[(x/2)]))^2)
f2[x_]:=a (x^2)+b x +c

Now we calculate f(0) and (Subscript[f, 2])(0).

f[0]

f2[0]

Thus, if we want f(0)=(Subscript[f, 2])(0), we must set c=1. Next we calculate f'(0) and (((Subscript[f, 2])))'(0).

f'[0]

f2'[0]

As a consequence, we set b=-2. Finally, we calculate f''(0) and (((Subscript[f, 2])))''(0).

f''[0]

f2''[0]

Thus, we set 2a=-1/2, or a=-1/4. Redefine (Subscript[f, 2])(x) with these values of a, b, and c.

f2[x_]:=1-2x-(1/4)(x^2)

Plot f(x) and the approximation (Subscript[f, 2])(x) on the same graph to obtain the following.

Plot[{f[x],f2[x]},{x,-π,π},PlotStyle->{{},RGBColor[1,0,0]}]

From the graph, we see that (Subscript[f, 2])(x) is a better approximation to f(x) than (Subscript[f, 1])(x) was. Let us plot the error |f(x)-(Subscript[f, 2])(x)|.

Plot[Abs[f[x]-f2[x]],{x,-π,π}]

From the graph, we see that the error is less than 0.5 if -1.1≤x≤1.1; this interval is larger than the interval we obtained for the approximation (Subscript[f, 1])(x). Zoom in on the error graph to find the following.

Plot[Abs[f[x]-f2[x]],{x,-1,1}]

Thus, our error is less than 0.05 if -0.4≤x≤0.4, which again is larger than the corresponding interval for (Subscript[f, 1])(x).

If a quadratic approximation chosen to agree with the function, and its first two derivatives is a good approximation, one might think that a cubic approximation chosen to agree with the function and its first three derivatives would be even better. Let us check to see if this is the case; we begin by defining the function (Subscript[f, 3])(x)=a (x^3)+b (x^2)+c x+d.

f3[x_]:=a (x^3)+b (x^2)+c x+d

Because we want f(0)=(Subscript[f, 3])(0), we compute

f[0]

f3[0]

and so we set d=1. Similarly

f'[0]

f3'[0]

so we set c=-2 and because

f''[0]

f3''[0]

we require 2b=-1/2, or b=-1/4. Finally we check

f'''[0]

f3'''[0]

so we set 6a=2, or a=1/3. This gives us (Subscript[f, 3])(x)=1-2x-(1/4)(x^2)+(1/3)(x^3)

f3[x_]:=1-2x-(1/4)(x^2)+(1/3)(x^3)

Plot[{f[x],f3[x]},{x,-π,π},PlotStyle->{{},RGBColor[1,0,0]}]

Experiment 5.3

1. Find the quadratic approximation to f(x)=sin(x) near the point x=0. Show the function and the approximation on the same graph. 

	2. Find the cubic approximations to f(x)=(e^x), near the point x=0. Show the function and the approximation on the same graph. 
	
	3. Find the quadratic and cubic approximations to f(x)=tan x near the point x=0. Show the function and the approximations on the same graph.

Experiment 5.4

1. Find an algebraic formula for the tangent line approximation to y=f(x) near the point (0,f(0)) in terms of f(0), and f'(0). 

	2. Find an algebraic formula for the quadratic approximation to y=f(x) near the point (0,f(0)) in terms of f(0), f'(0) and f''(0).
	
	3. Find an algebraic formula for the cubic approximation to y=f(x) near the point (0,f(0)) in terms of f(0), f'(0) f''(0), and f'''(0).

Credits

These laboratories were created by Raouf Boules, Geoff Goodson, Ohoe Kim and Mike O'Leary for use in the Calculus courses of Towson University. Commercial use is prohibited without permission of the authors. Non-commercial use is permitted, provided this credit section is retained.