Lesson 2: Cooling and Euler's Method 2.1 Applied Problem. Heat transfer in a mass is very important for a number of objects such as cooling of electronic parts or the fabrication of large beams. Although this process can be very complicated, we can study it for simple cases. Consider a cooling cup of coffee, which is well stirred so that it has a uniform temperature. Here we will introduce an added complication that the surrounding temperature can vary with time. In this case the substitution z = ysur(t) - y(t) will not work as easily as in lesson one. In such cases we often replace the continuous model with a discrete model, which we can solve with the use of computers. The goal is to predict the temperature for any future time. 2.2 Differential Equation Model. We will consider two models for the temperature change: Newton s law of cooling and discrete time, and Newton s law of cooling and continuous time. The discrete model is based on an expression for the change in temperature over a small time interval. If the time interval is large, then one can expect the change in temperature to be large. If there is a large difference of the surrounding temperature, ysur, and the temperature of the coffee, y, then there will also be a big change in the temperature. A third factor will be the material of the coffee cup. If the cup is made from a metal, then the change in the temperature could be large. On the other hand, if the cup is made from
2 foam, then the change in the temperature will be less. These observations can be modeled as follows. Discrete Version of Newton s Law of Cooling. Let y(i) be the temperature at time i*dt where dt is a given time interval. y(i+1) - y(i) = c*(ysur y(i))*dt. We can solve for y(i+1) y(i+1) = y(i) + c*(ysur y(i))*dt = (1 c*dt)*y(i) + c*ysur*dt. If the surrounding temperature is ysur = 70 and the coffee is initially y(0) = 200, then the temperature will cool monotonically down to the room temperature. In our model this will happen if a = 1 c*dt > 0, and so, this is a restriction on the time step, dt. In order to estimate c, we must have a second observed temperature. Suppose we choose a time step equal to one minute, dt = 1, and have observed the temperature at time zero and one minute later to be y(0) = 200 and y(1) = 195. Now apply the model with i = 0 to get y(0+1) = (1 c*dt)*y(0) + c*ysur*dt 195 = (1 c *1)*200 + c*70*1. So, solve for c to get c = (195 200)/(70 200) = 5/130 = 1/26. The continuous version of this application was studied in the previous lesson. One hopes for small enough dt the solutions of the discrete and continuous models will be close.
3 Continuous Version of Newton s Law of Cooling. Let y(t) be the temperature at time t. y = c(ysur y) and y(0) = y 0. 2.3 Method of Solution. The general problem of solving or approximating the solution of the differential equation y' = g(t,y) can be approached by approximating the derivative. Since y'(t) is the derivative of y(t), it can be approximated by y'(t) (y(t + dt) - y(t))/dt where t is any time and dt represents a small change in time. Now let t = i*dt, replace y(t) by Y(i), and replace the differential equation by the following discrete equation (Y(i+1) - Y(i))/dt = g(i*dt, Y(i)). Next we solve for Y(i+1) in terms of the previous Y(i). We hope the error, Y(i) - y(i*dt), will be small, and it will decrease as dt goes to zero. Euler's Numerical Method for Approximating y' = g(t,y) and y(0) given. Y(i+1) = Y(i) + dt*g(i*dt,y(i)).
4 Below we have used this method for y' = (1/26)*(70 - y), y(0) = 200 and variable time steps dt = 50/n where n = 4, 8, 16 and 32 time steps. The last number in each column is an approximation of the solution at t = 50, y(50) = 70 + 130e (-50/26) = 89.0004 The error at time t = 50 is y(50) - Y(n+1) where the CAPITAL Y indicates the number computed via the Euler method. Note the error at this time is approximately proportional to dt. n = 4 n =8 n = 16 n = 32 200 200 200 200 n (dt = 50/n) error 137.5 168.75 184.375 192.1875 4 9.55137 105.0481 145.012 170.628 184.8445 8 4.58938 88.19804 126.9803 158.5333 177.9428 16 2.24524 79.44898 113.2831 147.8923 171.4558 32 1.11024 102.8785 138.5302 165.3587 94.97502 130.2934 159.628 88.97141 123.0466 154.2417 84.41097 116.6708 149.1791 Euler Error ~ dt = 50/n 111.0613 144.4208 106.1261 139.9484 10 101.784 135.7447 8 97.96381 131.7937 6 94.60278 128.0802 91.64571 124.5898 89.04406 121.3091 4 2 86.75511 118.2257 0 115.3275 4 8 16 32 112.6035 110.0432 n 107.6367 105.3749 103.249 101.2509 99.37282 97.60762 95.94851 94.3891 92.92341 91.5458 90.25098 89.03398 87.89011 error at t = 50 Series1
5 2.4 Matlab Implementation. In this section we use the Euler method for the problem where the surrounding temperature is 70 and the constant c in the right side is.10, which is larger than 1/26. The two line m-file for the function on the right side of the differential equation is stored as feul.m. function feul = feul(t,x) feul =.10*(70. - x); The m-file for the Euler method is as follows and is stored as eulerc.m. The time step, dt, is denoted by h and is defined by the final time, T, divided by the number of time steps, KK. The initial temperature is stored in y(1) and the initial time is stored in t(1). The for loop executes the Euler method, and this creates two row vectors, one for time and one for temperature. The output has the form of a graph, and notice the temperature more rapidly approaches the surrounding temperature. You can look at the numerical values by typing at the Matlab prompt [t' y']. %your name, your student number, lesson number clear; y(1) = 200.; T = 50; KK = 100 h = T/KK; t(1)= 0.; for k = 1:KK y(k+1) = y(k) + h*feul(t(k),y(k)); t(k+1) = t(k) + h; end plot(t,y) title('your name, your student number, lesson number') xlabel('time') ylabel('temperature')
6 200 your name, your student number, lesson number 180 160 temperature 140 120 100 80 60 0 5 10 15 20 25 30 35 40 45 50 time 2.5 Numerical Experiments. The next numerical experiments are done using Matlab. They illustrate numerical error as a function of the number of steps. This done with the equation for Newton's law of cooling with ysur = 70 and c =.061. Matlab has a number of solvers for ordinary differential equations, and the reader should execute the Matlab demonstration odedemo. In the following calculations the number of time steps was varied from n = KK = 4, 8 to 16. The three plots were put into the same graph, and the reader should observe the convergence as KK increases.
7 EDU» eulerc KK = 4 EDU» t4 = t; EDU» y4 = y; EDU» eulerc KK = 8 EDU» t8 = t; EDU» y8 = y; EDU» eulerc KK = 16 EDU» t16 = t; EDU» y16 = y; EDU» whitebg EDU» plot(t4,y4,t8,y8,t16,y16) Figure: Convergence of Numerical Solution
8 The next calculation keeps the c =.061 and y(0) = 200, but the surrounding temperature is allowed to increase from 70 by one degree every five minutes. This means ysur(t) = 70 + t/5. In order to incorporate this into the Matlab code, the m-file feul.m must be changed as follows replace the second line of feul.m by feul =.061*(70 + t/5 - x); save this file, and execute the m-file eulerc.m by typing eulerc at the Matlab prompt. In the following calculation we have increased the final time, T, in eulerc.m from 50 to 100. As time increases the temperature still approaches the surrounding temperature. 200 your name, your student number, lesson number 180 160 temperature 140 120 100 80 0 10 20 30 40 50 60 70 80 90 100 time
9 2.6 Additional Calculations. Consider the cooling cup of coffee with variable c =.061*(1+.S)*.5,.061*(1+.S)*1.0 and.061*(1+.s)*1.5 where S is your student number. Assume the surrounding temperature is ysur(t) = 70 + t/5 and y(0) = 200. (a). State the differential equation. (b). Modify the feul.m file. (c). Insert your name and student number into the eulerc.m file. Execute the eulerc.m file for each of the three values of c. (d). Print the feul.m and eulerc.m files as well as each of the three graphs.