Finding and Graphing Tangent Lines to a Curve
In this example, we show how to find the equation of a line tangent to a given curve in Python. Suppose we have the graph of f(x) = e^((x-75)^2/200)/10sqrt(2pi) and we wish to find the equation of the line tangent to f at x=80. Recall the process described in an earlier video: list the steps to solve by hand, then determine the Python command(s) needed to perform these steps. To do this by hand, we need to:
1) Find the y-coordinate y0 by substituting x=80 into the original function (subs)
2) Find the derivative of f (diff)
3) Find the slope m of the line by substituting x=80 into the derivative (subs)
4) Print the resulting equation y = y0 + m(x - x0) (print)
We begin by defining x as a symbolic variable using the symbols command, then define our expression f. Now that we have the process above, we simply recall or look up the syntax for each of the commands. So y0 = f.subs(x,80), df = diff(f,x), m = df.subs(x,80), and we define tanline = y0 + m*(x-x0) and print it in a print command. We can convert our values to floating point using the evalf command. To verify graphically, we use the plot command to plot both the function and the tangent line on the same graph. Recall that each plot is a tuple (parentheses around it), and each x-range is a tuple within that plot. From the graph, we see our function is a bell curve, and we do notice our line is tangent to the curve at x=80.