Lab 6 – Derivatives and Mutant Bacteria

advertisement
Lab 6 – Derivatives and Mutant Bacteria
Date: September 27, 2011
Assignment Due Date: October 4, 2011
Goal: In this lab you will further explore the concept of a derivative using R. You will use your
knowledge of how to define and plot functions in R. You will consider slopes between two points on
different curves and see how they relate to derivatives. You will learn how to create two plots in the
same figure and then plot functions along with their derivatives. Finally, you will relate this back to
biology by looking at a model of mutant bacteria growth and its derivative.
Plotting Functions and Average Slopes
We begin this lab by plotting a simple linear function
. Open a new script and type the
necessary code to plot this function from x = -5 to 5 with x incremented in steps of .01. Use type = “l” to
plot this as a line. Be sure to use a function definition when plotting. Check with someone around you
to make sure you have done this correctly. You may refer to previous labs if you need help with
commands. Hopefully at this point, however, you are becoming very familiar with these basic
commands.
What is the slope of this line? Pretend you do not know about slope-intercept form and you want to use
R to calculate the slope of this line. We know how to calculate the slope between any two points.
Recall the formula for the slope of a line.
The slope formula has been written in two forms above which are equivalent. In the first case we are
finding the slope between two points, a and b. In the second case we are also finding the slope between
two points, t+h and t. Therefore, in the second case the difference between the input values of the two
points is simply h.
We will use the second form to calculate our slope. In your code you must define t and h. Talk with
those around you about what t, h, and t+h represent.
Let’s first define these parameters as follows.
t=1
h=2
In order to help you understand which points we are looking at we will use R to plot them on the graph
you have already plotted with the following code.
points(c(t,t+h),c(f(t),f(t+h)),type=”o”,col=”red”)
Next add code to your script to define slope and print it to the screen. Work with those around you to
make sure you have this correct.
Rerun your code for three different values of h. What do you notice about the red line on your plot and
about the slope as you change h? Does the slope change? Why or why not?
Now change the function to
. (Just comment out the linear function above so you can use it
again in the next section.) You can use the same code you just wrote to now compute slopes between
points on this parabola. Again see what happens for different values of t and h.
Instantaneous Slope vs. Average Slope
When we talk about derivatives we want to calculate the slope at a point. However, we only know how
to calculate the slope at two points. Let’s see what happens as we move these points closer and closer
together by changing the value of h. In simple terms, this is what is meant by taking a limit as
.
We are letting the two points get closer and closer until our two points essentially become the same
point and we can calculate the derivative at that point.
Change your function back to
to begin with and then you will again use the quadratic
function. Let’s write a code that calculates the slope between two points for different values of h. One
way to do this is by calculating the slope inside a loop as follows.
for (i in 0:10){
h = 1/(10^i);
print(h)
#put your code to calculate the slope here. You can still define t outside the loop.
print(slope)
}
Fill out the tables below for each function.
,t=2
h
Slope: (f(t+h) – f(t))/h
1
.1
.001
What do you think the instantaneous slope is for this function at t=2?
,t=0
Slope: (f(t+h) – f(t))/h
h
1
.1
.001
What do you think the instantaneous slope is for this function at t=0?
,t=2
h
1
.1
.001
Slope: (f(t+h) – f(t))/h
What do you think the instantaneous slope is for this function at t=2?
,t=0
h
1
.1
.001
Slope: (f(t+h) – f(t))/h
What do you think the instantaneous slope is for this function at t=0?
As we have discussed in the past, R is not a symbolic algebra program. We have to give it a value for h
and we cannot let h = 0 since that would give us division by zero. Therefore, when we are calculating
instantaneous slopes they will always be approximate. We cannot actually take a limit in R but we can
let h be very, very tiny so we get an answer that is very close to the instantaneous slope of the function
at the specified value of t.
Plotting Derivatives
We have now looked at instantaneous slopes of specific points but what if we want to know the
instantaneous slopes at all the points along a curve. We will now write code to plot them side by side.
As we have talked about, if you use plot() more than once in your code it will erase the first plot and put
the second in its place. There is a way to avoid this by putting multiple plots in the same figure. Add this
line of code to the top of your script.
par(mfrow=c(1,2))
This allows you to have two plots side by side in the figure.
Ok we will now plot the function along with its derivative. You have already written code to plot the
function. Add main=”f(x)” into your plot command to give it a title.
We no longer need the line we wrote to put a red line on top of the two points we were plotting.
Comment it out. Now we will plot the derivative. In order to do this we need to calculate the
instantaneous slope for many points on the curve. Let’s define t as the same input vector we used to
plot our function.
t=x
We now need to pick an h. We will pick one that is very small so it approximates our instantaneous
slope well.
h = 1/(10^10)
Now use the code you have already used to define the slope earlier in the lab. Since t is now a vector,
your output will be a vector too. This output vector defines the derivative function as it tells us the
instantaneous rate of change for each point in the vector t(or x since they are the same). As we have
discussed, these values are only approximate since we can’t take limits in R. Therefore, we must round
our answer for these slopes to get a nice curve. We do this by using round() as follows. I have called my
slope vector “m” below. If you have called it something else than put that variable name in the place of
“m”.
m = round(m,digits=5)
This rounds the slope vector “m” to 5 digits. Now let’s plot it.
plot(x,m,type=”l”,main=”Derivative of f(x)”)
The derivative of the function
should be a horizontal line at -3. Why?
The derivative of the function
should be a linear function that crosses the x axis. What does
it mean when the value of the derivative is negative? What does it mean when the derivative is
positive?
Mutant Bacteria and Derivatives
In section 1.10 you saw an example of a discrete time dynamical system in which there were two types
of bacteria: wild type and mutant. This section goes through a derivation of a discrete time system for
p, the fraction of mutants in the total population. One solution for this system is given as follows,
where
.
Recall that s is the reproduction rate of mutants, r is the reproduction rate of the normal bacteria, and
p0 is the initial fraction of mutants in the total population.
Plot the function P(t) along with its derivative as we did before for simple linear and quadratic functions.
Don’t forget to round your vector that describes the derivative. Set p0 = .001, s = 2, and r = 1.5. Don’t
forget to label your graphs as we did before.
Think about why the derivative looks the way it does for the given function.
You will be using this script to plot P(t) and its derivative in your assignment so
save it!
Download