10 Ordinary Differential Equations (ODEs)
19 Feb 2019Most physical phenomena are ultimately described by a relationship between changing quantities, resulting in differential equations. If such an equation only contains one independent variable (such as time) and hence only total derivatives (and no partial derivatives) we classify it as an ordinary differential equation (ODE). An ODE of order contains no derivatives higher than the -th derivative:
The dependent variable is a function of the independent variable and is the -th derivative with respect to . A linear ODE only contains first powers of or its derivatives. A non-linear ODE may contain higher powers. There often exist methods to solve linear ODEs analytically but this is impossible for most non-linear ODEs. Solutions of an ODE are fixed by the initial conditions1, e.g., and similar for all higher derivatives. For an ODE of order , exactly initial conditions are needed.
Using ODE integration algorithms (integrators) we can solve linear and non-linear ODEs of any order numerically. The basic idea is to start with the initial conditions and then propagate for a small step to numerically compute and all its derivatives. By repeating the process, one extrapolates from the initial condition to any "later" value of .
We will first study the simplest integrator, the Euler algorithm, and then look at a more robust class of integrators, the Runge-Kutta schemes.
Introductory example: The bouncing ball
We will introduce the basic ideas for solving ODEs by looking at a very simple physical example: the bouncing ball with Newton's equations of motion
where is the constant acceleration due to gravity2 and is the position of the ball as a function of time (its trajectory).
The forward Euler scheme for any first order ODE
is
In order to solve the original 2nd order equation of motion we make use of the fact that one -th order ODE can be written as coupled first order ODEs, namely
Solve each of the first order ODEs with the Euler algorithm:
In class we developed a simple simulation for free fall and for the bouncing ball.
- bounce_ball_numpy.ipynb: simulation and graphing with matplotlib
- bounce_ball_visual.ipynb: visualization with vpython inside the Jupyter notebook using vpython-jupyter
ODEs, standard form, and principles of integrators
The first part of this lesson introduces the formalism and the basic ideas behind integration algorithms. The Jupyter notebook 10-ODEs.ipynb contains the lecture notes.
We then revisit the Euler algorithm and introduce much better algorithms, namely the Runge-Kutta schemes and the Verlet integrator.
Accuracy and performance of Euler and Runge-Kutta integrators
The second part shows how to assess the accuracy of integration
algorithms and analyzes in more depth the simple
Euler integrator and the
Runge-Kutta methods,
namely rk2
and rk4
. As examples we use the simple 1D
harmonic oscillator with potential energy function , an anharmonic oscillator (), and a 6-th power oscillator ().
The Jupyter notebook 10-ODE_integrators.ipynb contains the lecture notes3. Skeleton code for in-class problem exercises can be found in the notebook 10-ODE_integrators_students.ipynb and in the module integrators.py.4
Verlet integrators
The third part introduces a different class of integrators: time reversible (and symplectic) integrators such as the velocity Verlet algorithm, which have worse accuracy than e.g. RK4 but long term stability.
- The notebook 10-ODE-integrators-verlet.ipynb implements the velocity Verlet algorithm and shows how to use it to calculate the orbit of the Earth around the sun.
- 10-ODE-visualize-planets.ipynb shows how to visualize planetary motion in 3D with vpython.
Resources
- Computational Modelling: Chapter 2
- Computational Physics: Chapter 9
- scipy.integrate for high performance integration algorithms for Python, in particular see scipy.integrate.odeint() and scipy.integrate.ode().
- Weisstein, Eric W. "Ordinary Differential Equation." From MathWorld — A Wolfram Web Resource.
- Numerical Recipes in C, WH Press, SA Teukolsky, WT Vetterling, BP Flannery. 2nd ed, 2002. Cambridge University Press. Chapter 16.
Footnotes
-
Solutions to ODEs can also be restricted by boundary conditions (values of the solution on the domain boundary) but this leads to difficult Eigenvalue problems and will not be considered in this lesson. ↩
-
The boundary condition at the floor is that the ball bounces back elastically, i.e., the velocity is reversed on collision. ↩
-
Notebook will be posted after class; in the meantime look at the student notebook. ↩
-
As usual,
git pull
the resources repository PHY494-resources to get a local copy of the notebook. Then copy the notebook and all other code into your work directory in order to complete the exercises. ↩