1180:Lab3 James Moore January 22nd, 2012 Goals For This Week We’ll solve Newton’s Law of Cooling to determine the time of death for a body I found Monday afternoon. We’ll use R’s ode and splinefun commands to solve the ode and interpolate the temperature. Finally, we’ll compare the solution using ode and using Euler’s method. 1 Newton’s Law of Cooling Newton’s law of cooling is a differential equation that describes how a hot object cools to the ambient temperature. Let y represent the temperature of our object and let T be the ambient temperature. dy = k(y − T ) dt For example, suppose that the object is a dead body that I found Monday outside the math department. Then y is the temperature of the body, T is the outside air temperature and k is the rate of heat exchange between the body and the air. Let t0 represent the time of death. Then we know that y(t0 ) = 98.6F . If we know that the temperature of the body was 25.8F at 4pm on Monday, can we figure out what t0 is, i.e. when this person died? 2 Spline Functions First we need to reconstruct the temperature profile. This is the hourly temperature data I got from NOAA.gov. We’ll let t = 0 (not the same as t0 ) refer to 3pm Sunday. Enter this data into a ‘.R’ file. tdata<-seq(0,25) Tdata<-c(20,19,17,16,15,14,13,13,12,12,11,11,11,10,10,10,10,11,14,17,20,22,23, 24,23,22) What we need now is a function that gives the temperature at every value of t, not just every hour. To do this, we interpolate with a cubic spline. 1 T<-splinefun(tdata,Tdata) The splinefun function is a bit weird because it is a function that returns another function. The command above takes the data points and returns a function that represents a smooth curve. We can now plot the original points and the interpolated function together. Note how we can evaluate the temperature at non-interger values with the interpolating function. plot(tdata,Tdata,xlab=’time’,ylab=’population’,main=’Air Temperature’) t2<-seq(0,25,.1) lines(t2,T(t2),col=’red’) Save this plot. 3 Predicting the time of death II Now we’re going to try to find the time of death using ode. I suggest putting all of this in a .R file. First we’ll write in the function necessary for the differential equation. f<-function(t,y,k){ list(k*(T(t)-y)) } Then we’ll guess that the person tied at 3pm sunday (t = 0). We really only need to specify the solution then and at t = 25 so we’ll define tsolve1 to be a list containing only those values. tsolve1<-c(0,25) Next we run the ode with the appropriate initial conditions and with k = .13. result=ode(y=98.6,t=tsolve1,parms=.13,func=f) Finally, print out the result like this >result time 1 1 0 98.60000 2 25 20.95502 The resulting temperature is too low! That means we are overestimating how long the body has been freezing. Now we’ll change our guess to just one hour (meaning that t0 = 24 or at 3pm Monday). tsolve1<-c(24,25) 2 Running everthing again gives us. > result time 1 1 24 98.60000 2 25 89.30898 Now, it didn’t cool down enough, meaning that we underestimated the amount of time it has been cooling. Experiment with the value of t0 until the ending temperature is correct. Then provide the time of death. Now rerun the ode with more finely spaced time points going from t0 to 25. Plot the temperature of the body as well as the function T on the same plot. 4 Euler’s method Recall that last semester we solved ODEs ourselves using Euler’s method, I want you to do it again. For reference, look at Labs 3 (for loop) and 12 (Euler’s method) from last semester. You’ll be solving the following: y1 = 98.6 (1) t = seq(t0 , 25, h) yi = yi−1 + h ∗ f (ti−1 , yi−1 , .13) (2) (3) Remember the last line has to be evaluated inside a for loop. I recommend using a step size h = .1. The number of steps that you use (N ) should satisfy (N − 1) ∗ h = 25 − t0 . 5 Tasks for this week If you’ve gotten this far and followed all direction, you are almost done. For all plots, please include all the code you needed to make them. 1. Please provide a plot of the air temperature data (as points) with an interpolating curve plotted on top of it. (Should already be done.) 2. Please include your estimated time of death (provide your answer in the form Saturday 3:48pm, not t = 22). 3. Include a plot of the body temperature (using the correct time of death) and the interpolated air temperature. (Should already be done.) 4. Plot the body temperature (using the correct time once again) from ode, i.e. the same as above, along with the solution using Euler’s method. 3