Calculus for Biologists Lab Math 1180-002 Spring 2012

advertisement
Calculus for Biologists Lab
Math 1180-002
Spring 2012
Lab #1 - Newton’s Law of Cooling
Report due date: Tuesday, January 17, 2012 at 9 a.m.
Goal: To explore the autonomous differential equation defined by Newton’s law of cooling. We will look at how temperature
oscillations changes in important parameters affect the behavior of the solution.
Before we begin, be sure to open a new script in R. Note that any lines of code will appear in this font, and any
commented code will be preceded by #. Also, make sure you keep track of your answers to the starred bold questions!
You will need to put them in your assignment.
Introduction
Newton’s law of cooling states that the rate at which an object cools is proportional to the difference between its
temperature (H) and the ambient temperature (A). This law is represented by the differential equation
dH
= α(A − H),
dt
(1)
where α is a rate constant (time−1 ) that describes the specific heat of the object. It determines how quickly the object
loses heat.
?
What are some things you can think of that lose heat rapidly? Slowly?
?
Newton’s Law of Cooling
Let’s take a look at what this derivative looks like. We’ll define α, A and the derivative.
alpha = 0.02
A = 30
dHdt = function(h) alpha*(A-h)
Now we can plot dHdt by defining a sequence of numbers to plug into the derivative.
H = seq(20,40,by=0.5)
## H values to plot
plot(H,dHdt(H),type="l",col=1,ann=F)
title(xlab="H", ylab="dH/dt")
?
When is H decreasing, zero and increasing?
Why does this make sense in terms of the object’s temperature?
?
Now that you have some understanding of the behavior of the object’s temperature, you can verify it by plotting the
solution H(t), which is given by
H(t) = A + (H(0) − A) e−αt .
(2)
First define times over which to evaluate H(t).
t = seq(0,300,length=500)
Notice that the option length is used in the seq command. This tells R that you want a vector of 500 numbers in order
from 0 to 300. When you actually care about the exact values chosen, you should use the by option.
For this example, we will specify that H(0) = 40◦ C, and then define and plot the solution.
H0 = 40
H = function(t) A + (H0-A)*exp(-alpha*t)
plot(t,H(t),type="l",ylim=c(30,40),col="black")
1 of 3
You should see a black curve that decays exponentially. Now let’s see what happens to the solution if we plug in different
α values. With each newly defined α, you will use lines to add a new curve to your already existing figure.
alpha = 0.05
lines(t,H(t),col="purple")
alpha = 0.1
lines(t,H(t),col="red")
You should now have three curves in your plot. Add a descriptive title, and save this figure as plot1 to include with
your assignment.
Note: You may use dev.copy(jpeg,"filename.jpg") or some other plot-saving method to do this.
Oscillations
We all know that there are some cases in which the environmental temperature may oscillate for one reason or another.
As an exercise, try to think of some examples of this. With R, we can easily impose periodic behavior on the differential
equations to see what happens. Let’s suppose that the ambient temperature is now a function of time A(t), oscillating
with a period T :
2πt
.
(3)
A(t) = 30 + cos
T
With this new expression for A, the structure of the law of cooling in Equation (1) doesn’t change very much at all:
dH
= α(A(t) − H).
dt
(4)
First, we will see exactly what A(t) does to dH
dt at different times. In the lines of code below, notice that I have specified
the variable period to represent T , because T already means something else in R.
rm(list=ls()) ## remove all variables
alpha = 0.02
## same as before
period = 4
## oscillation period of 4 minutes
A = function(t) 30 + cos(2*pi*t/period)
dHdt = function(h,t) alpha*(A(t)-h)
## new derivative
dH
Also, look carefully at the way I have defined dH
dt in the code. Because A is a function of time, dt is now a function of
both H and t. In R, we can define functions with any number of inputs. In this case, we have two of them. This way, we
can evaluate dH
dt at both a temperature and a time.
dHdt(40,0)
dHdt(20,10)
By running the two lines of code above, you should see that if the object’s temperature is 40◦ C when t = 0, then it will
cool. If its temperature is 20◦ C at t = 10, it will increase. Take a moment to think about why these conclusions are
valid. Seriously. Do it.
Now let’s see what the derivative looks like for a range of H values at times t = 0, 1 and 2.
par(mfrow=c(1,2))
H = seq(20,40,by=0.5)
## create side-by-side plots
plot(H,dHdt(H,0),type="l", col=1, ann=FALSE)
lines(H,dHdt(H,1),col=2)
lines(H,dHdt(H,2),col=3)
title(xlab="t", ylab="dH/dt")
What if we plotted the derivative for even more values of t? The lines below use a for loop to plot dHdt for t =
1, 2, 3, . . . , 20, changing the color each time.
2 of 3
plot(H,dHdt(H,0),type="l", col=1, ann=FALSE)
## start with t=0
for (j in 1:20){
lines(H,dHdt(H,j),col=j+1)
## plot dHdt for integers j=1,...,20
}
title(xlab="t", ylab="dH/dt")
Do you notice anything different between the two plots?
How do you explain the number of lines you see in the second plot?
?
?
Oscillatory Solution
Luckily for us, the modified differential equation in (4) has a solution, given by
+ 2απT sin 2πt
− α2 T 2 eαt
α2 T 2 cos 2πt
T
T
,
H(t) = 30 +
α2 T 2 + 4π 2
(5)
assuming this time that the initial condition is H(0) = 30. You will explore what variations in α do to this new solution.
rm(list=ls())
alpha = 0.02
period = 4
## arbitrary for now
## same as before
A = function(t) 30 + cos(2*pi*t/period)
H = function(t) 30 + ((alpha*period)^2*cos(2*pi*t/period) + 2*alpha*pi*T*sin(2*pi*t/period) (alpha* period)^2*exp(-alpha*t))/((alpha*period)^2 + 4*pi^2)
Now that we’ve set up our solution, we will use the vector values to store the different α we wish to test: α = 0.02, 0.5,
1 and 4.
values=c(0.02,0.5,1,4)
We will then create four plots, in a 2-by-2 grid, that correspond to each of these four values. The command
par(mfrow=c(2,2))
tells R to set up a figure that will have 2 rows and 2 columns of plots (for a total of four objects). Another for loop will
be used to plot both A(t) and H(t) four separate times.
t = seq(0,20,length=1000)
for (j in 1:length(values)){
alpha = values[j]
plot(t,A(t),type="l",col="black",ann=F)
lines(t,H(t),col="red")
title(xlab="t",ylab="A(t), H(t)",main=paste("alpha=",alpha))
}
You should have one black (A(t)) and one red (H(t)) in each panel. Save this figure as plot2 to include with your
assignment.
3 of 3
Download