Lab 6 Derivatives and Mutant Bacteria Date: September 27, 20 Assignment Due Date: October 4, 20 Goal: In tis lab you will furter explore te concept of a derivative using R. You will use your knowledge of ow to define and plot functions in R. You will consider slopes between two points on different curves and see ow tey relate to derivatives. You will learn ow to create two plots in te same figure and ten plot functions along wit teir derivatives. Finally, you will relate tis back to biology by looking at a model of mutant bacteria growt and its derivative. Plotting Functions and Average Slopes We begin tis lab by plotting a simple linear function. Open a new script and type te necessary code to plot tis function from x = -5 to 5 wit x incremented in steps of.0. Use type = l to plot tis as a line. Be sure to use a function definition wen plotting. Ceck wit someone around you to make sure you ave done tis correctly. You may refer to previous labs if you need elp wit commands. Hopefully at tis point, owever, you are becoming very familiar wit tese basic commands. Wat is te slope of tis line? Pretend you do not know about slope-intercept form and you want to use R to calculate te slope of tis line. We know ow to calculate te slope between any two points. Recall te formula for te slope of a line. Te slope formula as been written in two forms above wic are equivalent. In te first case we are finding te slope between two points, a and b. In te second case we are also finding te slope between two points, t+ and t. Terefore, in te second case te difference between te input values of te two points is simply. We will use te second form to calculate our slope. In your code you must define t and. Talk wit tose around you about wat t,, and t+ represent. Let s first define tese parameters as follows. t = = 2 In order to elp you understand wic points we are looking at we will use R to plot tem on te grap you ave already plotted wit te following code. points(c(t,t+),c(f(t),f(t+)),type= o,col= red ) Next add code to your script to define slope and print it to te screen. Work wit tose around you to make sure you ave tis correct.
Rerun your code for tree different values of. Wat do you notice about te red line on your plot and about te slope as you cange? Does te slope cange? Wy or wy not? Now cange te function to. (Just comment out te linear function above so you can use it again in te next section.) You can use te same code you just wrote to now compute slopes between points on tis parabola. Again see wat appens for different values of t and. Instantaneous Slope vs. Average Slope Wen we talk about derivatives we want to calculate te slope at a point. However, we only know ow to calculate te slope at two points. Let s see wat appens as we move tese points closer and closer togeter by canging te value of. In simple terms, tis is wat is meant by taking a limit as. We are letting te two points get closer and closer until our two points essentially become te same point and we can calculate te derivative at tat point. Cange your function back to to begin wit and ten you will again use te quadratic function. Let s write a code tat calculates te slope between two points for different values of. One way to do tis is by calculating te slope inside a loop as follows. for (i in 0:0){ = /(0^i); print() #put your code to calculate te slope ere. You can still define t outside te loop. print(slope) } Fill out te tables below for eac function., t = 2 Wat do you tink te instantaneous slope is for tis function at t=2?
, t = 0 Wat do you tink te instantaneous slope is for tis function at t=0?, t = 2 Wat do you tink te instantaneous slope is for tis function at t=2?, t = 0 Wat do you tink te instantaneous slope is for tis function at t=0?
As we ave discussed in te past, R is not a symbolic algebra program. We ave to give it a value for and we cannot let = 0 since tat would give us division by zero. Terefore, wen we are calculating instantaneous slopes tey will always be approximate. We cannot actually take a limit in R but we can let be very, very tiny so we get an answer tat is very close to te instantaneous slope of te function at te specified value of t. Plotting Derivatives We ave now looked at instantaneous slopes of specific points but wat if we want to know te instantaneous slopes at all te points along a curve. We will now write code to plot tem side by side. As we ave talked about, if you use plot() more tan once in your code it will erase te first plot and put te second in its place. Tere is a way to avoid tis by putting multiple plots in te same figure. Add tis line of code to te top of your script. par(mfrow=c(,2)) Tis allows you to ave two plots side by side in te figure. Ok we will now plot te function along wit its derivative. You ave already written code to plot te function. Add main= f(x) into your plot command to give it a title. We no longer need te line we wrote to put a red line on top of te two points we were plotting. Comment it out. Now we will plot te derivative. In order to do tis we need to calculate te instantaneous slope for many points on te curve. Let s define t as te same input vector we used to plot our function. t = x We now need to pick an. We will pick one tat is very small so it approximates our instantaneous slope well. = /(0^0) Now use te code you ave already used to define te slope earlier in te lab. Since t is now a vector, your output will be a vector too. Tis output vector defines te derivative function as it tells us te instantaneous rate of cange for eac point in te vector t(or x since tey are te same). As we ave discussed, tese values are only approximate since we can t take limits in R. Terefore, we must round our answer for tese slopes to get a nice curve. We do tis by using round() as follows. I ave called my slope vector m below. If you ave called it someting else tan put tat variable name in te place of m. m = round(m,digits=5) Tis rounds te slope vector m to 5 digits. Now let s plot it. plot(x,m,type= l,main= Derivative of f(x) )
Te derivative of te function sould be a orizontal line at -3. Wy? Te derivative of te function sould be a linear function tat crosses te x axis. Wat does it mean wen te value of te derivative is negative? Wat does it mean wen te derivative is positive? Mutant Bacteria and Derivatives In section 0 you saw an example of a discrete time dynamical system in wic tere were two types of bacteria: wild type and mutant. Tis section goes troug a derivation of a discrete time system for p, te fraction of mutants in te total population. One solution for tis system is given as follows, were. Recall tat s is te reproduction rate of mutants, r is te reproduction rate of te normal bacteria, and p0 is te initial fraction of mutants in te total population. Plot te function P(t) along wit its derivative as we did before for simple linear and quadratic functions. Don t forget to round your vector tat describes te derivative. Set p0 =, s = 2, and r =.5. Don t forget to label your graps as we did before. Tink about wy te derivative looks te way it does for te given function. You will be using tis script to plot P(t) and its derivative in your assignment so save it!