Laboratory 5
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 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)).

Experiment 5.1

Find the derivative of f(x) using the limit definition of derivative and compare it with the result of applying the commands f'[x] and D[f[x],x]. Use the commands Expand, Together, and Simplify as necessary.

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

	2.  f(x)=(1/(1-(4/x))).
	
	3.  f(x)=(x/(Sqrt[1+(x^2)])).
	
	4.  f(x)=x sin((x^2)).
	
	5. f(x)=x+(Sqrt[x]).
	
	6. f(x)=(1/(x^2)).

5.2  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. Give an interval on which the error for the tangent line approximation is less than 0.5, and give a similar interval on which the error for the tangent line approximation is less that 0.05.


	1. f(x)=2sin(x)-cos(2x), near the point x=0. 
	
	2.  f(x)=(sin^2)(x)-((sin x)/x), near the point x=π. 

	3. f(x)=(e^(-x))cos πx, near the point x=1.

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.