Numerical Solutions of
Ordinary Differential Equations
April 27, 2024
See: G. H. Golub, J. M. Ortega, Scientific Computing and Differential
Equations, Academic Press (1992) for details.
1 An ordinary differential equation
First order:
dy(x)
= f (x, y) (1)
dx
Second order:
d2 y(x) dy(x)
2
+b + ω 2 y(x) = 0 (2)
dx dx
2 Initial value problems vs. boundary value
problems
2.1 Boundary value problems
We give the equation and the values of the unknown function at the bound-
aries. Then we find the values at the intermediate points.
For example:
dy(x)
dx
+ 3y(x) = 0 (3)
1
y(0) = 1 (4)
y(5) = 0.4 (5)
The solution should satisfy these boundary values.
We will not study boundary value problems. We will study
initial value problems which are easier to handle.
2.2 Initial value problems
We give the equation and the value of the unknown function at the initial
point. Then we seek the value at another point.
For a first order equation we need one initial value:
dy(x)
dx
= f (x, y) (6)
y(x0 ) = y0 (7)
where x0 is the initial point. For example y(0) = 1.
For a second order equation we need two initial values (for the function
and its first derivative):
d2 y(x)
dx2
+ b dy(x)
dx
+ ω 2 y(x) = 0 (8)
y(x0 ) = y0 (9)
dy
|
dx x=x0
= ŷ0 (10)
etc.
3 Euler’s method
Let us solve the following differential equation:
dy
= f (x, y) (11)
dx
with the initial value y(x0 ) = y0 .
x0 is the initial point and we need the value of the unknown function at
xN . The points are
x0 , x1 , x2 , x3 , ..., xk−1 , xk , xk+1 , ...xN (12)
2
Thus we have
xk = x0 + kh (13)
xN −x0
where h = N
is the spacing between the points and k = 0, 1, 2, ..., N .
The formula for the Euler’s method is
yk+1 = yk + hf (xk , yk ) (14)
DERIVATION:
Taylor expansion of y around xk :
dy
y(x) = y(xk ) + (x − xk ) |x + ... (15)
dx k
dy
From the equation, we have dx
= f (x, y). So
y(x) = y(xk ) + (x − xk )f (xk , y(xk )) + ... (16)
Take x as the next grid point xk+1 :
y(xk+1 ) = y(xk ) + (xk+1 − xk )f (xk , y(xk )) + ... (17)
Rename y(xk+1 ) = yk+1 , y(xk ) = yk , (xk+1 − xk ) = h and neglect the higher
order terms. Finally we obtain
yk+1 = yk + hf (xk , yk ) (18)
4 Runge-Kutta (RK) methods
Euler’s method has a very slow rate of convergence as h decreases. For the
Runge-Kutta (RK) methods, the error tends to zero at a faster rate as h
tends to zero.
4.1 Second order RK (RK2)
h
yk+1 = yk + [f (xk , yk ) + f (xk+1 , yk + hf (xk , yk ))] (19)
2
Note that we just replaced f (xk , yk ) of Euler’s method by an average of f ,
evaluated at two different places (average of two slopes).
3
4.2 Fourth order RK (RK4)
h
yk+1 = yk + (F1 + 2F2 + 2F3 + F4 ) (20)
6
where
F1 = f (xk , yk ) (21)
h h
F2 = f xk + , yk + F1 (22)
2 2
h h
F3 = f xk + , yk + F2 (23)
2 2
F4 = f xk + h, yk + hF3 (24)
The formula (20) is a weighted average of four slopes. The midpoint slopes
F2 and F3 have greater weight.
**********************************************************************
EXERCISE 1:
dy
=x+y (25)
dx
with the initial value y(0) = 1. Find y(5) using 100 steps. Use a) Euler’s
method, b) RK2, c) RK4.
EXACT RESULT: y(5)=290.8263182
1 % S o l v e dy/dx=x+y
2 % u s i n g Euler ' s method
3
4 clear all
5 clc
6
7 % Right hand s i d e o f t h e eqn . i s our f u n c . :
8 f u n c = @( x , y ) x+y ;
9
10 %Values :
11 x i n i t i a l =0;
12 y i n i t i a l =1;
13 x f i n a l =5;
14
4
15 %Number o f s t e p s :
16 N=100;
17
18 h=( x f i n a l = x i n i t i a l ) /N;
19
20 %Prepare th e v e c t o r s :
21 x v a l u e s=l i n s p a c e ( x i n i t i a l , x f i n a l ,N) ;
22 y v a l u e s=z e r o s ( 1 , 1 0 0 ) ;
23
24 %S e t t h e i n i t i a l v a l u e :
25 y v a l u e s ( 1 )=y i n i t i a l ;
26
27 %Euler ' s method :
28 f o r i =1:N=1
29 y v a l u e s ( i +1)=y v a l u e s ( i )+h * f u n c ( x v a l u e s ( i ) , y v a l u e s ( i ) ) ;
30 end
31
32 %P r i n t y v a l u e a t x=x f i n a l :
33 d i s p ( y v a l u e s (N) )
34 %P l o t t he s o l u t i o n :
35 plot ( xvalues , yvalues )
**********************************************************************
5 Second order ODEs
Take for example the equation for the damped harmonic motion:
d2 y(t) dy(t)
2
+b + ω 2 y(t) = 0 (26)
dt dt
with b = 0.3 and ω = 0.7. The initial values are: y(0) = 1 and y ′ (0) = 1
where the prime denotes the first derivative. For b = 0, we get the harmonic
motion.
Our numerical methods can work with first order equations. Thus, we
will write this second order equation as two first order equations and solve
them simultaneously.
5
Define
dy
≡z (27)
dt
and use this in the original equation
dz
+ bz + ω 2 y = 0 (28)
dt
Now we have two equations, namely
dz
= −bz − ω 2 y (29)
dt
dy
≡z (30)
dt
which will be solved simultaneously.
EXERCISE 2:
As a coding example, let us solve this system with RK4 from t = 0 to
t = 30 with 1000 points and plot the behavior.
1. Define two functions:
f(t,y,z)=z
g(t,y,z)=-b*z-(w*w)*y
2. Set
w=0.7
b=0.3
tinitial=0
yinitial=1
zinitial=1
tfinal=30
numpoints=1000
h=(tfinal-tinitial)/numpoints
3. Define three vectors, all with 1000 elements:
tvalues:
tvalues(first element)=tinitial
6
tvalues(last element)=tfinal
yvalues:
yvalues(first element)=yinitial
zvalues:
zvalues(first element)=zinitial
4. Enter a loop for the method:
1 f o r i=s t a r t : s t o p
2 t=t v a l u e s ( i ) ;
3 y=y v a l u e s ( i ) ;
4 z=z v a l u e s ( i ) ;
5
6 F1y=f ( t , y , z ) ;
7 F1z=g ( t , y , z ) ;
8
9 F2y=f ( t +(h / 2) , y+(h / 2) * F1y , z+(h / 2) * F1z ) ;
10 F2z=g ( t +(h / 2) , y+(h /2 ) * F1y , z+(h / 2) * F1z ) ;
11
12 F3y=f ( t +(h / 2) , y+(h / 2) * F2y , z+(h / 2) * F2z ) ;
13 F3z=g ( t +(h / 2) , y+(h /2 ) * F2y , z+(h / 2) * F2z ) ;
14
15 F4y=f ( t+h , y+h * F3y , z+h * F3z ) ;
16 F4z=g ( t+h , y+h * F3y , z+h * F3z ) ;
17
18 y v a l u e s ( i +1)=y v a l u e s ( i ) +(h / 6) * ( F1y+2* F2y+2* F3y+F4y ) ;
19 z v a l u e s ( i +1)=z v a l u e s ( i ) +(h / 6) * ( F1z+2* F2z+2* F3z+F4z ) ;
20 end
5. Plot tvalues vs. yvalues.