CoCalc Public FilesSAGEWS / 07-tangent.sagews

# Tangent Lines

In your experience with mathematics thus far, you have most likely come across the task of calculating the slope between two points. Whether you know it as "rise over run" or "change in y over change in x", the formula

\begin{align}\frac{y_2-y_1}{x_2-x_1}\end{align}

will give you the slope between points $(x_1, y_1)$ and $(x_2, y_2)$. So if, for instance, we were to calculate the slope between two points on $f(x) = x^3/2$, say (0, 0) and (2, 4), we would find that by the formula, (4 - 0)/(2 - 0) gives us a slope of 2, as pictured on the following graph. For a purpose that will later be revealed, however, what if we were to find the slope of the function at a single point -- at (1, 1/2)? Such is the nature of the tangent line problem that we are about to explore, and is one of the basic questions of calculus.

Rather than simply generating a static graph this time, we'll be doing a bit of manipulation. Evaluate the following code:

x1 = 0
x2 = 2

def f(x):
return (x^3)/2

y1 = f(x1)
y2 = f(x2)

slope = (y2-y1)/(x2-x1)

p = plot(f(x), x, 0, 3)
pt1 = point((x1, y1), rgbcolor=(1,0,0), pointsize=30)
pt2 = point((x2, y2), rgbcolor=(1,0,0), pointsize=30)

d = 3
l = line([(x1-d, y1-d*slope), (x2+d, y2+d*slope)], rgbcolor=(1,0,0))

t1 = text("(%s, %s)" % (float(x1), float(y1)), (x1+0.5, y1-0.8), rgbcolor=(1,0,0))
t2 = text("(%s, %s)" % (float(x2), float(y2)), (x2+0.5, y2-0.8), rgbcolor=(1,0,0))
t3 = text("slope: %s" % float(slope), ((x1+x2)/2-0.5, (y1+y2)/2), rgbcolor=(1,0,0))

g = p+pt1+pt2+l+t1+t2+t3
g.show(xmin=0, xmax=3, ymin=-1, ymax=9)


In the graph, the straight line that passes through the two points is called a secant line -- we can say that it is an approximation of the function's slope at the point (1, 1/2), albeit not a very good one. What we want is a line tangent to the function at (1, 1/2) -- one that has a slope equal to that of the function at (1, 1/2).

To attain a better approximation of the slope at that point, let's try decreasing the distance between the two points at either side of it. In the first cell of your worksheet, change $x_1$ to 0.5 and $x_2$ to 1.5 and evaluate. Your graph should now look something like the following image:

As you can see, the secant line in this graph still does not pass through the desired point of (1, 1/2), but at least we are getting closer. Now, try setting $x_1$ as 0.999 and $x_2$ as 1.001 and again evaluate the cell. Aside from looking cramped, what else can you observe about this graph? "Aha!" you surely say, it looks like the slope at (1, 1/2) is just about equal to 1.5000005. We could, of course, repeat this process in perpetuum, since when only a billionth of a unit separates the two points, there is little doubt that the calculated slope does, in fact, approximately equal the slope of $f(x) = x^3/2$ at (1, 1/2).

Let us skip that theoretical headache, however, and consider a much faster (and even more accurate!) method. Look again at the formula for calculating slopes, this time in a slightly altered form:

\begin{align}\frac{f(x+h)-f(x)}{h}\end{align}

Rather than ignoring the relationship between the two points, as in the first form, this formula relies upon it. The symbol $y_2$ is now f(x + h), and $y_1$ is f(x). The major difference here, though, is that $x_2$ has been replaced by the first x plus some change "h". The slope formula's premise still stands, however, as the new form does in fact calculate change in y over change in x.

So what can we do with this new formula, you ask? Well, what we would like to do is to make the change in x (designated by h) equal to zero in order to find the function's slope at a single point. Exchanging 0 for h does not work well, however, since that would make the formula simply (f(x + 0) - f(x))/0, which is not of much help (0/0 = ?). Let us consult SageMath, though, and see if there is a way to factor out h before setting it equal to zero. Evaluate following code:

def f(x):
return (x^3)/2

var('x, h')
(f(x + h) - f(x))/h


The result should look like this: 1/2*((h + x)^3 - x^3)/h. With h still in the denominator, though, replacing it with zero would not be a good idea. Luckily, SageMath has a way for us to pull all variables from the denominator to the numerator, which is just what we need. Evaluate the following:

((f(x + h) - f(x))/h).rational_simplify()

%md
The result should be 1/2*h^2 + 3/2*h*x + 3/2*x^2. Now that h is in the numerator, replacing it with zero will give us a formula for the function's slope at any value of x. To realize this result, add ".subs(h=0)" without the quotes to the end of rational_simplify() in the last entry and again evaluate the cell. And there you have it! By describing the change in x as a number 'h', we were able to simplify the formula for slope and replace h with zero in order to find that the slope of the function's tangent line at any x-coordinate is equal to $3x^2/2$. This resulting expression is known as the derivative of $f(x) = x^3/2$. You'll learn more about derivatives in the next section, but for now you should know that a function's derivative measures rate of change at a point.

To complete what we had originally set out to do, plug in a value of 1 for x, now, and you will see that the slope of the line tangent to the point (1, 1/2) on $f(x) = x^3/2$ is 3/2. This confirms our earlier experimentation with numerical analysis in addition to being an exact solution. If we were to write an equation for this tangent line in slope-intercept form, it would be

\begin{align}y=\frac{3x}{2}-1\end{align}

It's very important to remember that the equation for a tangent line can always be written in slope-intercept or point-slope form; if you find that the equation for a tangent line is $y = x^{4*x^2+e} + \sin{x}$ or some such extreme, something has gone (horribly) wrong. The slope of a tangent line will always be a constant.

You probably wouldn't be surprised to learn that this symbolic process involved a limit, specifically "the limit of (f(x + h) - f(x)))/h as h approaches zero." The slope that we found is also known as the derivative of f(x) at x=1. Look at how the tangent line lies against the graph of $f(x) = x^3/2$:

<img src="../IMAGES/tangent15.png">


The result should be 1/2*h^2 + 3/2*h*x + 3/2*x^2. Now that h is in the numerator, replacing it with zero will give us a formula for the function's slope at any value of x. To realize this result, add ".subs(h=0)" without the quotes to the end of rational_simplify() in the last entry and again evaluate the cell. And there you have it! By describing the change in x as a number 'h', we were able to simplify the formula for slope and replace h with zero in order to find that the slope of the function's tangent line at any x-coordinate is equal to $3x^2/2$. This resulting expression is known as the derivative of $f(x) = x^3/2$. You'll learn more about derivatives in the next section, but for now you should know that a function's derivative measures rate of change at a point.

To complete what we had originally set out to do, plug in a value of 1 for x, now, and you will see that the slope of the line tangent to the point (1, 1/2) on $f(x) = x^3/2$ is 3/2. This confirms our earlier experimentation with numerical analysis in addition to being an exact solution. If we were to write an equation for this tangent line in slope-intercept form, it would be

\begin{align}y=\frac{3x}{2}-1\end{align}

It's very important to remember that the equation for a tangent line can always be written in slope-intercept or point-slope form; if you find that the equation for a tangent line is $y = x^{4*x^2+e} + \sin{x}$ or some such extreme, something has gone (horribly) wrong. The slope of a tangent line will always be a constant.

You probably wouldn't be surprised to learn that this symbolic process involved a limit, specifically "the limit of (f(x + h) - f(x)))/h as h approaches zero." The slope that we found is also known as the derivative of f(x) at x=1. Look at how the tangent line lies against the graph of $f(x) = x^3/2$:

(plot(x^3/2, x, 0, 2)+plot(3*x/2-1, 0, 2, rgbcolor='red')+point((1, 1/2), \
rgbcolor='black', pointsize=30)).show(xmin=0, xmax=2, ymin=-1, ymax=4)


Now that we have an exact methodology for computing the slope of a tangent line, consider the function

\begin{align}g(x)=\frac{3}{x}\end{align}

What is the equation for the line tangent to (-2, g(-2))? The slope of the tangent line can be found using the function's derivative:

\begin{align}g'(x)=\lim_{h \to 0}\frac{g(x+h)-g(x)}{h}=\lim_{h \to 0}\frac{\frac{3}{x+h}-\frac{3}{x}}{h} =\lim_{h \to 0}\frac{\frac{3x}{x^2+xh}-\frac{3x+3h}{x^2+xh}}{h}\end{align}

\begin{align}=\lim_{h \to 0}{-\frac{3}{x^2+xh}}=-\frac{3}{x^2}\end{align}

Plug in -2 for x, then write an equation for the tangent line:

\begin{align}g'(-2)=-\frac{3}{(-2)^2}=-\frac{3}{4}\mbox{, so }y=-\frac{3}{4}x-3\end{align}

Here is another comparison:

(plot(3/x, x, -3, 0)+plot(-3*x/4-3, -3, 0, rgbcolor='red')+point((-2, -3/2), \
rgbcolor='black', pointsize=30)).show(xmin=-3, xmax=0, ymin=-10, ymax=0)

%md
## Practice Problems

Use the limit process to find the equation of the line tangent to the indicated point.

\begin{align}\mbox{1) }f(x)=4x\mbox{ at }x=2\end{align}

\begin{align}\mbox{2) }g(x)=-3x^2+2\mbox{ at }x=1\end{align}

\begin{align}\mbox{3) }x(t)=\frac{5}{t}+3\mbox{ at }t=5\end{align}

[Previous (Slant Asymptotes)](06-slantasymp.sagews)  <span style="float:right">[Next (The Definition of Derivative)](08-derivative.sagews)</span>


## Practice Problems

Use the limit process to find the equation of the line tangent to the indicated point.

\begin{align}\mbox{1) }f(x)=4x\mbox{ at }x=2\end{align}

\begin{align}\mbox{2) }g(x)=-3x^2+2\mbox{ at }x=1\end{align}

\begin{align}\mbox{3) }x(t)=\frac{5}{t}+3\mbox{ at }t=5\end{align}