Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 722

Rates of Change

The average speed of a moving object over some time interval is the distance between positions divided by the time it took to get from one position to the next. More generally, the average rate of change of a function of time is the change in the value of the function divided by the length of the time interval.

# Code for weird_function(), which will be used in the following exercise: def weird_function(): pieces = [sin(x), cos(x), arctan(x), ln(x), sqrt(x), exp(x)] f(x)=prod([choice(pieces) for i in srange(5)]) + prod([choice(pieces) for i in srange(5)]) return f

Exercise  1.{\bf Exercise\; 1.} Evaluate the cell in your turn-in sheet that contains the function weird_function(){\bf weird\_function()} and run the command h(x)=weird_function(){\bf h(x)=weird\_function()} to create a random function called h(x)h(x).

h(x)=5*x+2

Exercise  2.{\bf Exercise \; 2.} Compute the average rate of change of h(x)h(x) between x=3.5x = 3.5 and x=5.5.x = 5.5.

(h(5.5)-h(3.5))/2
5.00000000000000

We know the average rate of change of the function between x=3.5x = 3.5 and x=5.5x = 5.5. Now, we want its rate of change at x=3.5x = 3.5.

Exercise  3.{\bf Exercise \;3.} Create a list of average rates of change for h(x)h(x) for Δx\Delta x values of 1, 0.1, 0.01, and 0.001. (You can do this by hand or use a loop.) Use the result to estimate the function’s rate of change at x=3.5x = 3.5. Why do you think the number you gave is the rate of change at that value of xx?

dx_list=[1,0.1,0.01,0.001] ave_change_list=[] for dx in dx_list: ave_change=(h(3.5+dx)-h(3.5))/dx ave_change_list.append(ave_change) ave_change_list
[5.00000000000000, 5.00000000000000, 4.99999999999972, 4.99999999999901]

The rate of change of a function at a point is called its instantaneous rate of change at that point.

Exercise  4.{\bf Exercise\; 4.} Estimate the instantaneous rate of change at another point (as you did in Exercise 3).

x0=2. dx_list=[1,0.1,0.01,0.001,0.0001,0.00001,0.000001,0.0000001] ave_change_list=[] for dx in dx_list: ave_change=(h(x0+dx)-h(x0))/dx ave_change_list.append(ave_change) ave_change_list
[5.00000000000000, 5.00000000000000, 4.99999999999989, 4.99999999999901, 5.00000000000611, 4.99999999998835, 5.00000000158707, 4.99999998737621]

At this point, you might be wondering why we bother with different values of Δx\Delta x at all. Why not just pick a small value of Δx\Delta x and always use that to compute rates of change? The answer won’t be mathematically precise, but real-world measurements always have some error anyway. The following series of exercises will explore this question.

Exercise  5.{\bf Exercise\; 5.} Find the average rates of change for the function f(t)=t33t2+4t+1f(t) = t^3-3t^2+4t+1 at t=1t = 1 using 1, 0.1, 0.01, and 0.001 as your values of Δt\Delta t. Then, estimate the function’s rate of change at t=1t = 1. Feel free to use more Δt\Delta t values if you want.

var('t') f(t)=t^3-3*t^2+4*t^1 t0=1. dt_list=[1,0.1,0.01,0.001,0.0001,0.00001] ave_change_list=[] for dt in dt_list: ave_change=(f(t0+dt)-f(t0))/dt ave_change_list.append(ave_change) ave_change_list
t [2.00000000000000, 1.01000000000000, 1.00010000000004, 1.00000100000042, 1.00000000999856, 1.00000000009537]

Exercise  6.{\bf Exercise\; 6.} Repeat this procedure for g(t)=t34t2+6t+2g(t) = t^3-4t^2+6t+2. What is this function’s rate of change at t=1t = 1?

var('t') g(t)=t^3-4*t^2+6*t+2 t0=1. dt_list=[1,0.1,0.01,0.001,0.0001,0.00001] ave_change_list=[] for dt in dt_list: ave_change=(f(t0+dt)-f(t0))/dt ave_change_list.append(ave_change) ave_change_list
t [2.00000000000000, 1.01000000000000, 1.00010000000004, 1.00000100000042, 1.00000000999856, 1.00000000009537]

Exercise  7.{\bf Exercise\; 7.} Drawing on your results, briefly explain why limits are necessary when computing derivatives. Why don’t we just pick a small value of Δt\Delta t and always use that?

#limits are necessary because the approximation goes on to infinity and using a very, very small number/big decimal isn't ethical when a certain limit can be drawn at a certain good approximation.

Symbolic Calculations in Sage

One of Sage’s most powerful features is its ability to do symbolic computations – calculations with variables rather than numbers. Such calculations require you to use a particular kind of variable called a symbolic variable. Symbolic variables can be manipulated algebraically without having numerical values. The variable x is a built-in symbolic variable.

(x^5)/(x^3)
x^2

Exercise  8.{\bf Exercise\; 8.} Compute x3x7x^3x^7.

x^3*x^7
x^10

Exercise  9.{\bf Exercise\; 9.} Find the type of xx.

type(x)
<type 'sage.symbolic.expression.Expression'>

The variable xx is a built-in symbolic variable. To use other symbolic variables, like NN or PP, you’ll need to create them. This is called declaring the variables and is done using the var function. This function takes a string of variable names, separated by commas, as input and generates the appropriate symbolic variables.

var("a")
a
(a^3)/a
a^2

Note that Sage assumes that a0a \neq 0. Symbolic variables don’t have to be single letters.

var("sheep, rabbits")
(sheep, rabbits)
2*sheep+3*rabbits
3*rabbits + 2*sheep

Once a symbolic variable has been declared, you can use it in subsequent calculations without re-declaring it until you close your worksheet.

a*a
a^2

The command factor allows you to factor polynomials.

factor(a^2 + 5*a + 6)
(a + 3)*(a + 2)

Exercise  10.{\bf Exercise\; 10.} Factor x2+7x+6x^2 + 7x + 6.

factor(x^2+7*x+6)
(x + 6)*(x + 1)

Exercise  11.{\bf Exercise\; 11.} Factor k25k+6.k^2 - 5k + 6.

var('k') factor(k^2-5*k+6)
k (k - 2)*(k - 3)

Plotting uses symbolic variables, so if you want to plot a function of a variable other than xx, you have to declare it before using it.

var("p") plot(log(p), (p,0,4))
p

Exercise  12.{\bf Exercise\; 12.} Plot the function f(n)=2nf(n) = 2^n using nn as the independent variable, for values of nn between -30 and 30. You don’t need to define the function before plotting it.

var('n') plot(2^n,(n,-30,30))
n

Once you assign a numeric value to a symbolic variable, it’s no longer a symbolic variable. Instead, it’s now a number. A common plotting error results from assigning a value to some variable, usually xx, and later using that variable in a plot. The tell-tale symptom of this problem is a graph consisting of one or more horizontal lines.

x=2 plot(x^3, (x,-3,3), color="red")

Exercise  13.{\bf Exercise \;13.} Why is the horizontal line y=8 rather than some other value of y?

#It's at 8 because the graph is zoomed in and when plugging the only/declared x-value into the equation which is 2 or aka (2^3), you get a set declared y-value of 8 aka the y-intercept.

To fix this problem, just declare xx to be a symbolic variable again.

var("x") plot(x^3, (x,-3,3), color="red")
x

-------------------Second Half of Lab 4--------------------------.

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.

Example1.{\bf 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.

Example2.{\bf Example 2.} To plot a larger red point at the coordinates (-3,5), use the command point([-3,5], color="red", size=40).

Exercise  14.{\bf Exercise\; 14.} Plot the function h(x)=16x2h(x) = 16x^2 for values of x between 0 and 5. Overlay the point (1,16) on the graph and make sure the point is visible.

pl=plot(16*x^2, (x,0,5)) p2=point([1,16],color="red",size=40) show(pl+p2,axes_labels=["x","y"])

Exercise  15.{\bf Exercise\; 15.} Create an animation that zooms in on this point by reducing the range of xx. Use at least five different zoom levels. What do you observe?

Frames=[] dx=2 for ii in srange(0,12): p=plot(16*x^2,(x,1-dx,1+dx),axes_labels=["x","y"])+point([1,16],color="red",size=40) Frames.append(p) dx=dx/2. Ani=animate(Frames) show(Ani)

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 earlier. 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)(x_1,y_1). This equation is yy1=m(xx1)y-y_1 = m(x-x_1). 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(xx1)+y1y = m(x - x_1) + y_1.

Exercise  16.{\bf Exercise \;16.} Create an interactive that plots the function f(x) = 16x216x^2 and a line that passes through the point (1,16) and a second point on the function whose xx-coordinate is set by the slider. Describe what happens as the movable point approaches the fixed one.

f(x)=16*x^2 @interact def plot_secant_line(x1=(0,2,1/50)): p1=plot(f(x),(x,0,5),axes_labels=["x","y"]) p2=point([1,16],color="red",size=40) x0=1 y0=(x0) y1=f(x1) m=(y1-y0)/(x1-x0) L(x)=y0+m*(x-x0) p3=point([x1,y1],color="blue",size=40) p4=plot(L(x),(x,0,5),color="green") show(p1+p2+p3+p4)
Interact: please open in CoCalc

Exercise  17.{\bf Exercise \;17.} 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.

f(x)=16*x^2 @interact def plot_secant_line(x1=(0,2,1/50.)): p1=plot(f(x),(x,0,5),axes_labels=["x","y"]) p2=point([1,16],color="red",size=40) x0=1 y0=f(x) y1=f(x1) m=(y1-y0)/(x1-x0) L(x)=y0+m*(x-x0) p3=point([x1,y1],color="blue",size=40) p4=plot(L(x0),(x,0,5),color="green") p5=text("slope=",(0.3,100))+text(m,(1,100)) show(p1+p2+p3+p4+p5)
Interact: please open in CoCalc

Exercise18.{\bf Exercise 18.} Modify your interactive to also compute and display the derivative of the function at the point (1,16) (NOT the point moved by the slider). What do you notice about the derivative of the function and the slope of the plotted line?

f(x)=16*x^2 df(x)=32*x @interact def plot_secant_line(x1=(0,2,1/50.)): p1=plot(f(x),(x,0,5),axes_labels=["x","y"]) p2=point([1,16],color="red",size=40) x0=1 y0=f(x0) y1=f(x1) m=(y1-y0)/(x1-x0) L(x)=y0+m*(x-x0) p3=point([x1,y1],color="blue",size=40) p4=plot(L(x),(x,0,5),color="green") p5=text("slope=",(0.3,100))+text(m,(1,100)) p6=text("f'(1)=",(0.5,150))+text(df(1),(.75,150)) show(p1+p2+p3+p4+p5+p6)
Interact: please open in CoCalc

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)f'(x). Just like the number that gives the rate of change of a function at a single point, f(x)f'(x) is termed the derivative of f(x)f(x).

In Sagemath, derivatives are computed using the function diff. For example, to find the derivative of e3xe^{3x} with respect to xx, enter diff(e^(3*x),x). The diff function outputs a symbolic expression into which you can substitute numbers. If you would prefer to have a function as the answer, use fp(x)=diff(e^(3*x),x).

Example  3.{\bf Example \;3.} What is the derivative of f(x)=5x16f(x) = 5x^{16} at x=1.5x = 1.5?

df = diff(5*x^16, x) df
80*x^15
df.subs(x=1.5)
35031.5112304688
fp(x)=df fp(1.5)
35031.5112304688

Exercise  19.{\bf Exercise\; 19.} 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.)

list_frame=[] for x1 in srange (0,30,.5): df=diff(tan(x),x) m=df(x1) L(x)=m*(x-x1)+tan(x1) p= plot(tan(x), (x,0,30), ymin=-5, ymax=5)+point((x1,tan(x1)), size=40)+ plot(L(x), (x,x1-5,x1+5)) list_frame.append(p) ani=animate(list_frame) show(ani)