Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Solving Differential Equations with the Euler-Cromer Method
You will encounter differential equations that cannot be solved analytically. However, there are numerical methods that can be used to solve them with a computer. For many second-order, ordinary differential equations, a fairly simple approach called the Euler-Cromer Method is sufficient. This method was used frequently in Physics 231, but the name may not have been mentioned. (Note that it is referred to as the "last point approximation" or LPA in the refrence below.)
Notation
We will use shorthand notation for time derivatives where
and
In other words, the number of dots indicates the number of time derivatives. We’ll be finding the value of at discrete values of . Label these times with integer subscripts as
where is the initial time and is the time interval between the subsequent solutions. We will also label the values of the function at these time with integers as
Similarly, the derivatives at step are
and
The Problem
The form of the second-order differential equation to be solved is
or, in the more compact notation, it is
With the initial values of the variable () and its first derivative () are known at the initial time (), we want to find and at later times.
Euler Method
The simplest numerical method to solve differential equations is the Euler Method. The derivatives at step can be approximated as
and
Suppose that and are known at some time. Rearranging the equation for the first derivative, the approximate the solution after moving a step forward in time is
To find the solution at another step forward in time, we will need to know the first derivative at that later time. Rearranging the equation for the second derivative, the approximate first derivative after moving a step forward in time is
where we insert the function for the second derivative. These final two equations can be used iteratively to find the approximate solution at later times. Unfortunately, this method is not very accurate after a while. (For classical mechanics problems, this method often doesn't conserve energy.)
Euler-Cromer Method
A simple modification to the method described above greatly improves the accuracy of the numerical solution. First, calculate the value of the second derivative at the current time,
and use it to approximate first derivative after a step forward in time,
Next, the value of the varible at a later time is approximated by
where the first derivative at the later time is used. Note the slight difference between this approach and the Euler Method. The current value of (which may depend on the current values of , , and ) is used to calculate a new value of . However, the new value of is used to calculate the new value of .
Implementation in Python
As an example, we can numerically solve the differential equation
for and with initial conditions and at . In the program, the first and second derivatives of will be called and , respectively. (Thinks of each "" as a dot in the shorthand notation for derivatives.) The first part of the program prepares for the calculation. The pylab library is imported so that the results can be plotted. Constant(s), initial values of , , and , time step , and the final time are set. Three lists (, , and ) are started with the inital values of , , and .
The main part of the program is the loop that calculates and its first derivative () at time steps of using the Euler-Cromer method. The results for each step are appended to the lists. For this example, the calculation continues until the time reaches .
The results were stored in lists so that they can be used. After the calculations are complete, and can plotted versus as shown below.
It is important to check that the time step used was small enough to get accurate results. If is small enough, changing it slightly won't change the results significantly.
Note that this method can be used for ordinary, second-order differential equations, even if the function depends of something other than time. For example, it could be used to solve
for .
Reference
Alan Cromer, "Stable solutions using the Euler approximation," Am. J. Phys. 49, 455-459 (1981). (Note: As mentioned in the article, this method was discovered accidentally by a high school student.)