Google Classroom
GeoGebraGeoGebra Classroom

Differential Equations Day 7 -- Algebraic Newton's Law of Cooling & Programing Euler's Numerical Method

Newton's Law of Cooling Algebraic Solution

As we saw earlier, Newton's Law of Cooling states
The temperature of a body changes at a rate proportional to the difference in temperature between the body and its surroundings
We saw in an earlier lesson that the above principle can be expressed as the differential equation Last time we encountered this differential equation, we used Euler's Numerical Method to get an estimate for the solution of this differential equation with , an initial condition of Celsius, and an ambient temperature degrees Celsius. We will now see how to solve this differential equation algebraically as a linear differential equation. We will work with Newton's Law of Cooling in its general form, meaning we'll leave k and unspecified, and we will not have an initial condition. After we get the general solution of a Newton's Law of Cooling model, we'll then see how to work that into a real world application. Before we can study the differential equation as a linear differential equation, we must put the differential equation in standard (linear) form. Go back and review standard linear form if you need to. With the differential equation in standard (linear) form, we see that and . In other words, both p and g are constant functions. As such, this will proceed more or less like the example from the earlier lesson on linear differential equation. The difference here however, is that instead of specific numbers in the equation, here we leave the constants k and unspecified.
  1. 
  2. 
  3. In its general form, Newton's Law of Cooling has no initial condition, and so no specific solution is required.
In practice, in a Newton's Law of Cooling situation, you are usually given the initial temperature of the body, the temperature of the body at sometime in the future, and the temperature of the surroundings (). You are usually not provided k. It turns out that these three pieces of information are just enough information to determine k and c. See the example below.

Algebraic Example of Newton's Law of Cooling

Let's return to the example we saw earlier: A tub of ice cream is removed from the freezer at -18 degrees Celsius and brought into a room which is (positive) 32 degrees Celsius. The temperature of the ice cream, after one minute is observed to have warmed to -8 degrees Celsius. Find the specific algebraic solution that adheres to Newton's Law of Cooling, and which models this tub of ice cream. Use the model to predict the temperature two minutes after the tub of ice cream has been moved into the 32 degree air. SOLUTION: First of all, let's translate the above into mathematical notation. In this setup we're looking for a function which models the temperature of the ice cream, and adheres to Newton's Law of Cooling. That means that , and Unlike last time we encountered this question, we actually do not know k yet. It turns out that the fact that we know the value of T at two points in time (t=0 and t=1) is enough information to find k. Now, we need not repeat the steps we did above to find the algebraic solution to the equation in standard (linear) form. Referring to step 5 from above, we know that solves the differential equation. Incorporating the initial condition, , produces the following which can be solved for c, implying that c = - 50. Incorporating the temperature of the ice cream after 1 minute, , produces In other words We can solve this for k We therefore have all the information to assemble the final model of the tub of ice cream Plugging in produces the predicted temperature at minute 2, and it turns out to be 0 (check it yourself!). Let's examine this specific algebraic solution in Geogebra, and compare it to the Euler's Numerical Method estimate of the solution we produced in a previous class. Not bad! Notice that Euler's Method is systematically over estimating the algebraic solution because the shape of the slope field points to a solution which is concave down.

Review of Euler's Method

Now we switch our attention back to Numerical Methods. In particular, we'll take a closer look at Euler's Method and see how to implement it programmatically so that it doesn't represent as much work as doing the calculations by hand. For the purposes of this lesson, the most important thing to recall is the Euler's Method formulas for obtaining the coordinates of the next estimate point.

Overview of What We'll be Doing

In this activity we will see how to implement Euler's (Numerical) Method programmatically in both Geogebra and Octave (an open source version of MATLAB). First let's take a look at a non-linear and non-separable first order differential equation with initial condition. Feel free to use the Pen Tool to sketch an estimate of the specific solution satisfying the initial condition. Also, take a moment to reflect on why this is neither separable nor linear. I've loaded this differential equation into Geogebra below. Note that I have specified the right hand side of the equation as f(x,y)=sqrt(x^2+y^2), and then used f when creating the slope field with slopefield(f). This is slightly easier for a number of reasons, but mainly it makes implementation of Euler's Method easier below.

Implementing Euler's Condition in Geogebra Spreadsheet Mode

We'll implement Euler's Method in the Spreadsheet Mode of Geogebra. If you are using Geogebra Desktop, click the "View" drop down menu, and select Spreadsheet to make this mode visible. I've made it visible below.
The spreadsheet mode of Geogebra acts like a spreadsheet in Excel or Google Drive, except you can access and engage with mathematical objects in the Graphics and Algebra panes in each cell of the spreadsheet. In some sense, you can think of the Spreadsheet like a lot of input bars. For instance, in cells A1 and B1 I've loaded the x and y coordinates of Initial condition with =x(InitialCondition) and =y(InitialCondition)
Now we'll implement Euler's Method formulas in cells A2, B2. First we need a step size. We'll use h=0.1 for a step size. I created this by simply typing h=0.1 in the input bar. After doing this, in cell A2, I've calculated the x coordinate of the next estimate point with =A1+h And in in cell B2 I've calculated the y coordinate of the next estimate point with =B1+f(A1,B1)*h Note that because f was already declared, it is easier to code the next y coordinate. This is a general principle in any computer programming task: declare processes as much as possible so you don't have retype something over and over again. Lastly I've plotted the next estimate point by entering the following in cell C3 =(A2,B2) Try out calculating the next x and y coordinates, and the next estimate point by putting some code in cells A3, B3 and C3. Be sure to start with an equals sign! That said, don't spend too much time since we'll see how to copy the code from A2 and B2 programmatically and quickly in the next step.
Here's another look at the same applet with the spreadsheet editor closed.
Now comes the most powerful step. Rather than typing this equation in over and over again, GeoGebra lets us apply the formula in cells A2, B2 and C2 for as many steps as we want by using the "fill handle" (Google it to learn more!). In most spreadsheet software (including GeoGebra and Excel), the "fill handle" is in the lower right corner of a cell, and when you drag it down, it "fills" the same formula down as far as you drag it. I've applied the fill handle to calculate 10 total estimate points. In GeoGebra it's a small blue square in the lower right of a cell, or a set of highlighted cells. Try calculating 10 more rows by dragging the fill handle down on columns A, B, and C.
And again, here's a view of the same applet without the spreadsheet window open.
Not a bad fit! Now let's see how to do this in Octave. There's nothing special about Octave; any programming language can implement Euler's Method in a "for-loop". Click here: https://octave-online.net/bucket~LxkNgcm11TXPdGU8GNCtoR

Closing Thoughts

You should now be able to replicate this process in Geogebra Desktop. You will need to change f(x,y), the step size h, and also the number of steps in the spreadsheet mode, but this process should be extensible to a wide variety of use cases. That said, for your benefit, I've whipped up the following Euler's Method Calculator https://www.geogebra.org/m/d6cryyfx Looking ahead, when we encounter the Runge Kutta numerical method later this semester, we'll see that it is very similar to Euler's Method. The difference is that instead of a single formula for the next y-coordinate, we'll need a chain of several formulas to calculate the next y-coordinate. We'll discuss this later, but for now know that Runge Kutta makes use of some theory surrounding polynomial approximations to curve interpolation to get a better estimate of what the next y-coordinate should be rather than simply following the slope field in a straight line. To be continued.