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 h(x)=16x2 for values of x between 0 and 5.
Overlay the point (1,16) on the graph and make sure the point is visible.
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 (x1,y1). This equation is y−y1=m(x−x1). 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 y=m(x−x1)+y1.
Exercise 3. Create an interactive that plots the function f(x) = 16x2 and
a line that passes through the point (1,16) and a second point on the function
whose x-coordinate is set by the slider. Describe what happens as the movable
point approaches the fixed one.
#as the slider is brought closer to the fixed line the difference in x value decreases until you get to 1. At this point the delta x value is zero and because of that the graph gives us an error.
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, f′(x). Just like the number that gives the rate of change
of a function at a single point, f′(x) is termed the derivative of f(x).
In Sagemath, derivatives are computed using the function diff. For example, to
find the derivative of e3x with respect to x 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 x value using the usual function
evaluation notation fp(2) gives the value of the derivative at x=2.
Example 3. What is the derivative of f(x)=5x16 at x=1.5?
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 g(x).
(x^3 - 1)/(x^2 + 1)
In Example 4 we start with a function f(x)=x3+2x2−x+1 and use Sagemath to find the derivative f′(x), and then plot f(X) and f′(x) between x=−3 and x=2 with legend labels. Execute the two cells below to see what they produce.
f(x)=x^3+2*x^2-x+1## Our functionfp(x)=diff(f(x),x)## fp(x) is the derivativeshow("f(x)=",f(x))## show the formula for f(x) in a pretty formshow("f'(x)=",fp(x))## show the formula for f'(x) below it
## Now we plot them and add legendsp1=plot(f(x),(x,-3,2),color='blue',legend_label=f(x))## generate the plot of f(x) from -3 to 2, store in p1p2=plot(fp(x),(x,-3,2),color='red',legend_label=fp(x))## generate the plot of f'(x) from -3 to 2, store in p2show(p1+p2,axes_labels=["x","y/y'"])## combine the plots and add axes labels
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 f(x) and f′(x). 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 y=f(x) at a point a can be written down using the point slope form y−y0=m(x−x0) where in this case the point (x0,y0)=(a,f(a)). We can rewrite that to get the formula for the tangent line as y=f(a)+f′(a)(x−a).
Example 5. Use f(x)=x3+2x2−x+1 and draw a graph showing the function f(x) and the tangent line to the function at a=3/4. We will add a dot showing the point of tangency.
f(x)=x^3+2*x^2-x+1## Our functionfp(x)=diff(f(x),x)## the derivative, f'a=3/4m=fp(a)## slope of the tangent linet(x)=f(a)+m*(x-a)## formula for the tangent linep1=plot(f(x),(x,-2,2),color='blue')## generate the plot of f(x) from -2 to 2, store in p1p2=plot(t(x),(x,-2,2),color='red')## generate the plot of the tangent line from -2 to 2, store in p2p3=point((a,f(a)),size=40,color='black')## add a point, put it in p3show(p1+p2+p3,axes_labels=["x","y"])## combine the plots and add axes labels
Exercise 7. Modify Example 5 to produce a plot showing the tangent line to h(x)=ex+e−xex−e−x at x=1. Plot from x=−3 to x=3.