author:
'Adrian E. Feiguin' title: 'Computational Physics' ...
Ordinary differential equations
Let’s consider a simple 1st order equation:
To solve this equation with a computer we need to discretize the differences: we have to convert the differential equation into a “finite differences” equation. The simplest solution is Euler’s method.
Euler’s method
Supouse that at a point , the function has a value . We want to find the approximate value of in a point close to , , with small. We assume that , the rate of change of , is constant in this interval . Therefore we find: with . Then we re-write the differential equation in terms of discrete differences as: or and approximate the value of as We can generalize this formula to find the value of at as or in the general case:
This is a good approximation as long as is “small”. What is small? Depends on the problem, but it is basically defined by the “rate of change”, or “smoothness” of . has to behave smoothly and without rapid variations in the interval .
Notice that Euler’s method is equivalent to a 1st order Taylor expansion about the point . The “local error” calculating is then . If we use the method times to calculate consecutive points, the propagated “global” error will be . This error decreases linearly with decreasing step, so we need to halve the step size to reduce the error in half. The numerical work for each step consists of a single evaluation of .
Exercise 1.1: Newton’s law of cooling
If the temperature difference between an object and its surroundings is small, the rate of change of the temperature of the object is proportional to the temperature difference: where is the temperature of the body, is the temperature of the environment, and is a “cooling constant” that depends on the heat transfer mechanism, the contact area with the environment and the thermal properties of the body. The minus sign appears because if , the temperature must decrease.
Write a program to calculate the temperature of a body at a time , given the cooling constant and the temperature of the body at time . Plot the results for ; , using different intervals and compare with exact (analytical) results.
Let's try plotting the results. We first need to import the required libraries and methods
Next, we create numpy arrays to store the (x,y) values
We have to re write the loop to store the values in the arrays. Remember that numpy arrays start from 0.
We could have saved effort by defining
Alternatively, and in order to re use code in future problems, we could have created a function.
Actually, for this particularly simple case, calling a function may introduce unecessary overhead, but it is a an example that we will find useful for future applications. For a simple function like this we could have used a "lambda" function (more about lambda functions here).
Now, let's study the effects of different time steps on the convergence:
Challenge 1.1
To properly study convergence, one possibility it so look at the result at a given time, for different time steps. Modify the previous program to print the temperature at as a function of .