Curves and Lines
The rate of change of a function at a point has an important geometric meaning that you will explore in this section. To do this, you’ll need to be able to plot points, which is done with the command point.
Example 1. To plot a point at the coordinates (1,2), use the command point([1,2])
. Notice that the coordinates are given as a list.
Example 2. To plot a larger red point at the coordinates (-3,5), use the command point([-3,5], color="red", size=40)
.
Exercise 1. Plot the function for values of x between 0 and 5. Overlay the point (1,16) on the graph and make sure the point is visible.
Exercise 2. Create an animation that zooms in on this point by reducing the range of . Use at least five different zoom levels. What do you observe?
You should have noticed that as you zoom in on the curve, it starts to look like a straight line. (Some points on certain curves will not do this, which is an important observation but not very common in mathematical biology.) The question is what straight line it is.
To answer this question, we want to plot lines with slopes corresponding to the average rates of change we computed in the previous lab. Since we have a point and a slope, this is most easily done using the point-slope equation for a line passing through the point . This equation is . However, since Sage’s plot command requires everything other than y to be on the right-hand side of the equation, we have to rearrange it into .
Exercise 3. Create an interactive that plots the function f(x) = and a line that passes through the point (1,16) and a second point on the function whose -coordinate is set by the slider. Describe what happens as the movable point approaches the fixed one.
Exercise 4. You can display a number on a plot using the text function. The syntax to display the value val at point pt
on a plot is text(val, pt)
. (As in other cases, you can give the coordinates of pt
as a list.) Modify your interactive so it computes and displays the slope of the line being plotted. HINT: Use + to overlay text on a plot.
So far, we’ve been working with derivatives at single points. However, we can compute the derivative of a function at all points on the function. We then obtain a new function, . Just like the number that gives the rate of change of a function at a single point, is termed the derivative of .
In Sagemath, derivatives are computed using the function diff
. For example, to find the derivative of with respect to and assign it to fp(x) enter fp(x)=diff(e^(3*x),x)
. As usual with an assignment, to print the result you will need to include an extra line where you print print(fp(x))
or show show(fp(x))
it. You can evaluate it for a particular value using the usual function evaluation notation fp(2)
gives the value of the derivative at .
Example 3. What is the derivative of at ?
Exercise 5. Use Sagemath to find the derivatives of each of the following functions:
Use show() to print out the formulas nicely. You may want to experiment with factor() and simplify() to see if you can get a nicer form for .
In Example 4 we start with a function and use Sagemath to find the derivative , and then plot and between and with legend labels. Execute the two cells below to see what they produce.
Example 4.
Exercise 6. Use the example above to create a function plot_f_fp(f,xmin,xmax)
that takes as input a function f(x) and the range of x values for the plots and produces a plot with both and . Verify that it works correctly by checking that when you invoke it with plot_f_fp(f,-3,2)
that it produces the same plot as in example 4, and that if you instead use
var('t'); H(t)=100-16*t^2; plot_f_fp(H,0,2.5)
you get a plot showing the height of a falling ball as on page 72 combined with a plot of the velocity of the ball which starts at 0 and goes linearly to -80 ft/sec.
We know that the tangent line to a curve at a point can be written down using the point slope form where in this case the point . We can rewrite that to get the formula for the tangent line as .
Example 5. Use and draw a graph showing the function f(x) and the tangent line to the function at . We will add a dot showing the point of tangency.
Exercise 7. Modify Example 5 to produce a plot showing the tangent line to at . Plot from to .
Exercise 8. Use diff
to create an animation showing the tangent line to a function at different points, like the one at http://en.wikipedia.org/wiki/File:Graph_of_sliding_derivative_line.gif. (You don't need to make the line change color.)