You are on page 1of 11

Computational Physics II

Lecture 2: basic methods for solving ODEs


Jan Eysermans
Benem
erita Universidad Aut
onoma de Puebla (BUAP)
janeysermans@gmail.com http://bit.ly/2bVatcv

First semester 2016-2017

Overview
Physical models results often in differential equations which needed to be solved
(e.g. Newton, Maxwell, Schroedinger)
Analytical solutions available only in very simple cases
We will cover in this chapter basic numerical solutions to some general problems:
- only focus on ODEs (Ordinary Differential Equations: one variable and linear)
- PDEs (Partial Differential Equations: multiple variables and linear) will be
treated later
The numerical solutions will be compared with analytical models in some simple
examples error estimation
We will use C++ to compute the ODEs, and Python to show the result (i.e. the
plot)

Jan Eysermans (BUAP)

Analytic Geometry

First semester 2016-2017

2 / 11

A note on floating-point range and precision


A floating point number (e.g. me = 9.10938 1031 ) is stored based on its
mantissa (9.10938) and exponent (31).
Both mantissa and exponent are limited due to memory limitations and depends
on whether the float or double type is used.
For the exponent, we have the following limitations:
- Float: 2127 1038 too tight for physical constants!
- Double: 21023 10308
For the mantissa, the precision is given by
- Float: 7 decimal digits
- Double: 16 decimal digits
It is recommended to use double for the calculation and simulation of physical
phenomena.

Jan Eysermans (BUAP)

Analytic Geometry

First semester 2016-2017

3 / 11

Definition of physical constants


Physical constants needed to be defined only once:
const double m e = 9.10938215e-31; // electron mass in kg
const double c = 299792458; // speed of light in m/s
It is convenient to store all constants and frequently used functions in a library.
Make a new file consts.hpp and store the following physical constants:
G, g, me , mp , q, c, k, 0 , 0
Try to use a clear and consistent naming convention such as capital letters spaced
with underscores (e.g. PI, E, M E, Q).
In order to use these constants in our program, include them in the source code:
#include "consts.hpp"
Files with extension .hpp are called header files and contain declarations of
frequently used constants, functions, etc.

Jan Eysermans (BUAP)

Analytic Geometry

First semester 2016-2017

4 / 11

The equations of motion


In general, the equations of motion is given by the coupled first-order ODEs:
dv
= a(r, v),
dt

dr
= v.
dt

The acceleration a can depend on the position r and the velocity v. In case of
Newtonian mechanics, the acceleration of a body with mass m is given by:

a(r, v) =

1
Ft (r, v),
m

where Ft is the total force acting on the the body with mass m.
For complicated forces, an analytical formula is not available and a numerical
method is imposed to solve the problem. Therefore we need to model the
first-order derivatives w.r.t. time.

Jan Eysermans (BUAP)

Analytic Geometry

First semester 2016-2017

5 / 11

Forward derivative
In order to solve such equations numerically, one needs to discretize the (first
order) derivatives. By definition ( is a parameter),
f (t + ) f (t)
.

After applying a Taylor expansion up to first order of f (t + ) (for a given, small,


), the derivative can be written as:
f 0 (t) = lim

f 0 (t) =

f (t + ) f (t)
+ O( ).

The first term is an approximation of the derivative (called the forward derivative)
and the error associated is linear in (i.e. the larger , the larger the error).
We cannot make arbitrary small, due to round-off effects when using floating
point numbers. Typical limitations are in the order of 108 . In general, the
size of must be deduced based on the scale of the problem. A good
approximation is using a time scale lower than the scale which makes the problem
almost linear.
Jan Eysermans (BUAP)

Analytic Geometry

First semester 2016-2017

6 / 11

Euler method
We can use the forward derivative formula to solve our (general) equations of
motion. After some algebra one obtains:
v(t + ) = v(t) + a(r(t), v(t)) + O( 2 ),
r(t + ) = r(t) + v(t) + O( 2 ).
Dropping the second order terms and changing notation, one becomes the
(discrete) Euler method equations:
vn+1 = vn + an ,
rn+1 = rn + vn .
Keep in mind that the error in both position and velocity is O( 2 ).

Jan Eysermans (BUAP)

Analytic Geometry

First semester 2016-2017

7 / 11

Euler-Cromer and Midpoint method


The Euler-Cromer method is a slight modification of the Euler equations:
vn+1 = vn + an ,
rn+1 = rn + vn+1 .
It is tempting to combine both methods resulting in the midpoint method:
vn+1 = vn + an ,
1
rn+1 = rn + (vn + vn+1 ).
2
After substituting the expression for vn+1 in the latter equation, one becomes:
1
rn+1 = rn + vn + an 2 .
2
For the midpoint method the error in the position is O( 3 ) improvement
Jan Eysermans (BUAP)

Analytic Geometry

First semester 2016-2017

8 / 11

Implementation in C++
The following general scheme can be used to obtain a numerical solution of the
trajectory:
1. Specify the initial conditions (r1 , v1 );
2. Choose a time step ;
3. Calculate the acceleration a(r, v);
4. Update the value for r and v (Euler, Euler-Cromer, midpoint);
5. Go again to step 3 or break if the amount of points has been reached.
The physical model determining the trajectory is stored in the function a(r, v).

Jan Eysermans (BUAP)

Analytic Geometry

First semester 2016-2017

9 / 11

Example 1: projectile motion (1)


The equations of motion for a projectile with mass m under an air resistance force
Fa (v) is given by:
1
dr
dv
= Fa (v) g
y,
= v.
dt
m
dt
By approximation, the air resistance is given by:
1
Fa (v) = Cd Avv,
2
where Cd is the drag coefficient, the air density and A the cross-sectional area
of the projectile. Without air resistance, the equation of motion is given by:
1
,
r(t) = r1 + v1 t gt2 y
2
for a given initial coordinate r1 and initial velocity v1 of the projectile.
Exercise: calculate the solution numerically using the Euler, Euler-Cromer and
midpoint method. Compare the results with the analytical formula.
Jan Eysermans (BUAP)

Analytic Geometry

First semester 2016-2017

10 / 11

Example 1: projectile motion (2)


Guidelines to program the projectile motion example:
- Define the input variables
- Define the main variables which we want to compute
- Define all the auxiliary variables needed in the algorithms
- Think about the output: how we want to store the result?
- When we need to stop our calculation in this case? What are the physical
conditions?
- How can we implement the three different algorithms?
- How can we implement the analytic formula and the comparison with the
numerical methods?

Jan Eysermans (BUAP)

Analytic Geometry

First semester 2016-2017

11 / 11

You might also like