Newton s Cooling Model in Matlab and the Cooling Project! James K. Peterson Department of Biological Sciences and Department of Mathematical Sciences Clemson University March 10, 2014 Outline Your Newton Cooling Project Your Project Typical Cooling Project Matlab Session
Abstract Newton s Cooling Model in Matlab and the Cooling Project! Your assignment will be to take the liquid of your choice and place it in a cup of some sort like a coffee cup. Measure the ambient temperature A of the room you are in. Microwave the contents until the liquid is close to boiling. Then take it out of the microwave and measure the initial temperature, T 0. Then measure the temperature every 30 seconds for the first five minutes and then every minute for the next 10 minutes and then every five minutes thereafter until the temperature of the liquid in the cup reaches A + 5 or so. You will not reach room temperature in any reasonable length of time, so stopping at A + 5 is just fine. We will then model the decay of the temperature of your liquid using Newton s Law of Cooling. So it we let T (t) be the temperature of your liquid at time t in minutes, we will assume that the cooling of the liquid satisfies Newton s Law of Cooling.
Hence, we know This has solution T (t) = k(t (t) A), T (0) = T 0. T (t) = A + Be kt. Applying the initial condition, we get T 0 = A + B. or B = T 0 A. Hence, the solution is T (t) = A + (T 0 A)e kt. The one parameter we don t know is the constant of proportionality, k. However, we do have all the data we collected. So, rewrite our solution as T (t) A = e kt. T 0 A Now take the logarithm of both sides to get ( ) T (t) A ln = kt. (1) T 0 A This tell us how to estimate the value of k. Let the variable U(t) be defined to be ( ) T (t) A U(t) = ln T 0 A Then, Equation 1 can be written as U(t) = kt. Thus, the variable U is linear in t; i.e. if we graph U versus t we should see a straight line with slope k. Our collected data consists of pairs of the form (time, temperature). Suppose we collected N such pairs. Label them as (t i, T i) for 1 i N (you might have 80 points say so for you N = 80). Compute the corresponding U(t i) U i points ( ) Ti A U i = ln T 0 A and plot them as a scatter plot in MatLab.
Note the first value, U 1 = ln T0 A T0 A = ln(1) = 0. So our line has U intercept 0. Hence, our line is of the form y = mt and we have to find a good candidate for the slope m. This slope will then be used in our cooling model. From your data, you now have a collection of pairs (t i, U i) for 1 i N. We can find a line which comes close in an optimal way to all of this data (although the line does not have to include all the data!) by using a bit of calculus. At each time, the line has the value mt i. The discrepancy or error between the actual U value and the line value is then U i mt i. We want the cumulative error between our line and the data, so we don t want errors to cancel. Hence, we could choose an error term like U i mt i or (U i mt i) 2. Both of these are fine, but the squared one is differentiable which will allow us to use calculus methods. Define the energy function E(m) where m is the slope to be E(m) = N (U i mt i) 2. This is a nice function and so let s find where this function has a minimum by setting its derivative is zero. Setting the derivative to zero, we find de N dm = 2 (U i mt i) ( t i) = 0. Simplifying a bit, we have N ( U it i = m ti 2 ).
Solving for the critical point m, we find m = Uiti t2 i Finally, it is easy to see d 2 E dm 2 = 2 N ti 2 > 0. for all choices of m. Hence, the value m which is the critical point must be the global minimum Hence, the optimal slope is m = Uiti t2 i Then we can see how good our model is by plotting the model solution T (t) = T (t) = A + (T 0 A)e m t on the same plot as the actual data. First some notes of formatting of the report in your choice of word processor. 1. Use a one inch margin on all sides. 2. Use 11 pt font. Now let s talk about the structure of the document. It consists of the following pieces: Introduction: here you discuss the Newton s Law of Cooling. You should do some research on this at the library or online to find out more about this law. Your findings about this will then go in this section. This is 6 Points.
More on your document s structure: The Mathematical Model: Here you state the mathematical form of Newton s Law of Cooling. This means that you need to type the appropriate mathematical equations. Take care to do this nicely so that the report has a nice visual look. Discuss the meaning of each symbol in the mathematical equations, of course. Derive the solution to the Newton cooling problem showing all the mathematics. This is to make sure you learn how to use mathematics in a word processing document. Derive the optimal slope value as we have done in the text: again, this is to make sure you can type mathematics. Derive the appropriate log plot that you will use to find the Newton Cooling Constant. This is 10 Points. The Experiment: Here you discuss that your experiment is going to try find out if your liquid obeys Newton s Law of Cooling. You need to explicitly go over all of the information that is relevant to the experiment. This includes the choice of liquid, the cup it was placed in, an analysis of the kind of cup it was (ceramic, Styrofoam etc.), how the temperature of the liquid was measured and so forth. Be very explicit here. The idea is that the reader would have enough information to run the experiment themselves. Include a picture of the cup and your temperature measurement device in this section. Don t use water; that is an automatic project score of 0! Take pictures of your experimental setup and embed them in the document. This is 10 Points.
The MatLab Analysis: Here you include 1. the MatLab code fragments that plot your original data using circles for the data points. 2. the MatLab code to create the appropriate log plot annotated carefully. 3. the plot of your log data using circles for the data points. 4. the MatLab code to find the optimal slope from log plot and your value of the slope This is 10 Points. The Model: Here you discuss the model you found from your data and show how it compares to the data you collected. The MatLab code to plot the model and the data simultaneously needs to be here as well as the actual plot. Your original data is again plotted with circles for the data while the model is plotted as a smooth curve. This is 8 Points. Conclusions: Here you discuss whether or not you feel Newton s Law of Cooling provides a good theoretical model for your collected data. Discuss also flaws you might have found in the experiment. This is 6 points. References: Any references you used need to be placed here.
Let s see how we can manage our experimental data. Save your experimental data in a file with two columns. Column one is for time and column two is for the temperature of your liquid. Create a new file in Matlab and call it Newton.dat or some other name. Place it in your class folder. For a typical liquid (orange juice, water, tomato juice etc), we might collect the data here 0. 0 205 0. 5 201 2. 0 201 5. 0 190 8. 0 178 1 0. 0 175 1 3. 0 167 1 5. 0 161 2 5. 0 141 3 0. 0 135 4 0. 0 127 5 0. 0 117 6 5. 0 108 8 0. 0 100 9 5. 0 95 1 1 0. 0 89 1 3 0. 0 83 1 6 5. 0 80 We have the ambient or room temperature is 76 degrees for our experiment. load the data and generate the plot of the data % l o a d i n t h e e x p e r i m e n t a l d a t a from t h e % f i l e Newton. dat as two columns >> Data = l o a d ( Newton. dat ) ; % use t h e f i r s t column as time >> time = Data ( :, 1 ) ; % use t h e second column as t e m p e r a t u r e >> Temperature = Data ( :, 2 ) ; % g e n e r a t e a p l o t o f t h e e x p e r i m e n t a l d a t a >> p l o t ( time, Temperature, o ) ; % s e t x l a b e l >> x l a b e l ( Time i n Minutes ) ; % s e t y l a b e l >> y l a b e l ( Temperature in Fahrenheit ) ; % s e t t i t l e >> t i t l e ( Newton Law Of C o o l i n g E x p e r i m e n t a l Data ) ;
We see the plot of the measured data here: From the theory we have discussed, ( we ) know we should graph the T A transformed data given by log T0 A where A is the room temperature, T and T0 are the temperature and initial temperature of the liquid, respectively. Here A = 76 and T0 = 205. Thus, T0 A = 129. Here is how we do this in Matlab: % for t h i s data, T 0 A = 205 76 = 129 >> A = 7 6 ; >> LogTemperature = l o g ( ( Temperature A) / ( 129. 0 ) ) ; >> p l o t ( time, LogTemperature, o ) ; >> x l a b e l ( Time i n Minutes ) ; >> y l a b e l ( Transformed V a r i a b l e l n ( (T A) /( T 0 A) ) ) ; >> t i t l e ( Transformed Data V e r s u s Time ) ;
You can see this transformed data here: Then, look for the best straight line through the plot making sure you have the line go through the point (0, 0) at the top. The optimal slope is then m = Uiti N. t2 i In MatLab, we can add up all the entries in the variable U and time using the sum command. The optimal slope calculation in MatLab is then >> mstar = sum ( LogTemperature. time ) /sum ( time. time ) mstar = 0.0215 where the variable LogTemperature plays the role of U. Note time and LogTemperature are vectors so we use.* to do component wise multiplications!
The syntax LogTemperature.*time means to multiply each component of LogTemperature and time separately to create the new column of values LogTemperature(1) time (1) LogTemperature(N). time (N) The command sum applied to this column then creates the sum Uiti like we need. A similar thing is happening with the term sum(time.*time). The model is thus which we enter in MatLab as u(t) = 76.0 + 129 e 0.0215t % compute t h e model >> u2 = 76.0 + 129.0 exp ( mstar time ) ; Then we plot both the experimental data and the model on the same plot, % p l o t t h e model and the measure d a t a on t h e same p l o t >> p l o t ( time, u2, r, time, Temperature, o ) ; >> x l a b e l ( Time I n Minutes ) ; >> y l a b e l ( Temperature in Fahrenheit ) ; >> t i t l e ( F i t o f Newton Law o f C o o l i n g Model To Data : Data = o ) ; Looks pretty good! We could also have added a legend like legend( Model, Data, Location, BestOutside ); but we didn t show that. You should do that though in your report so it is really easy to see what is going on.
Our final plot! Here is all the code without comments! >> Data = l o a d ( Newton. dat ) ; >> time = Data ( :, 1 ) ; >> Temperature = Data ( :, 2 ) ; >> p l o t ( time, Temperature, o ) ; >> x l a b e l ( Time i n Minutes ) ; >> y l a b e l ( Temperature in Fahrenheit ) ; >> t i t l e ( Newton Law Of C o o l i n g E x p e r i m e n t a l Data ) ; % for t h i s data, T 0 A = 205 76 = 129 >> A = 7 6 ; >> LogTemperature = l o g ( ( Temperature A) / ( 129. 0 ) ) ; >> p l o t ( time, LogTemperature, o ) ; >> x l a b e l ( Time i n Minutes ) ; >> y l a b e l ( Transformed V a r i a b l e l n ( (T A) /( T 0 A) ) ) ; >> t i t l e ( Transformed Data V e r s u s Time ) ; >> mstar = sum ( LogTemperature. time ) /sum ( time. time ) ; >> u2 = 76.0 + 129.0 exp ( mstar time ) ; >> p l o t ( time, u2, r, time, Temperature, o ) ; >> x l a b e l ( Time I n Minutes ) ; >> y l a b e l ( Temperature in Fahrenheit ) ; >> t i t l e ( F i t o f Newton Law o f C o o l i n g Model To Data : Data = o ) ;
So there you have it. You are ready to go off and collect some data and see if your liquid obeys Newton s cooling law. Have some fun with this and treat it like a real lab in biology. Document everything! I want you to learn how to type mathematics into word so look around and find the tools to do that. You ll have three plots total! Make sure you get them all. I enjoy interesting liquid combinations in this lab, so be inventive!