Chapter 6 - Ordinary Differential Equations
Chapter 6 - Ordinary Differential Equations
Scientific Computing
SoICT 2023
2
Contents
• Differential Equations
• Euler method
• Runge-Kutta method
• MATLAB functions
3
Differential Equations
4
Input Value Problem
• The initial value problem (IVP) for the first order differential
equations can be written as:
y (t ) = f ( y , t ); y (t0 ) = y0
'
• If f depends on y?
5
Input Value Problem: Example
• E.g. 1: Population growth rate depends on population. If the
population at time t is y(t), then the population growth rate at
time t is
y'(t) = k*y(t)
where k is a row of positive numbers.
• E.g. 2: Lotka-Voltera equation about predator (fox) and prey
(rabbit). Let F(t) be the number of foxes and R(t) be the
number of rabbits at time t, we have:
R'(t) = (a – bF)R
F'(t) = (cR – d)F
where a, b, c, d are positive constants
The above differential equations are very difficult to solve by
analytical methods.
6
Numerical For Differential Equations
Approximate solution
Exact solution
y0
h
t0 t1 t2 t3 t4 t5
7
The Existence and Uniqueness of solution
8
Forward Euler (FE)
10
FE: Example
11
Forward Euler
12
Forward Euler
13
FE for a System of Differential Equations
y’ = f(y,z,t), y(0) = y0
yn+1 = yn + h f(yn,zn,tn)
14
FE for a Second order Differential Equation
15
Modified Euler method
16
Modified Euler method
17
Backward Euler (BE)
yn +1 − yn (1)
≈ yn' +1 = f ( yn +1 , tn +1 )
h
• Note: (2) does not give us the explicit formula because we have to
calculate the value of the function f for the unknown argument yn+1
18
BE: Example
yk = yk-1 – f(yk-1)/f’(yk-1)
MATLAB function:
fzero('x+0.5*x^3-1',1)
19
Summary of Euler method
20
Runge-Kutta method (RK)
yn +1 = yn + ∫ f ( y, t )dt
tn
(2)
21
The Second order Runge-Kutta method
22
The Second order Runge-Kutta method
yn +1 = yn + hf ( yn , tn )
(4)
h
[
yn +1 = yn + f ( yn , tn ) + f ( yn +1 , tn +1 )
2
]
Or we can rewrite as:
k1 = hf ( yn , tn )
k2 = hf ( yn + k1 , tn +1 ) (5)
1
yn +1 = yn + [k1 + k2 ]
2
23
The Second order Runge-Kutta method
24
RK for second order differential equation
y’ = z, y(0) = 1,
25
RK for second order differential equation
k1 = h z n
l1 = h (qn – a zn – b yn)
k2 = h (zn + l1)
26
The Third order Runge-Kutta method
where ȳn+1/2 and ȳn+1 is the estimate (since yn+1/2 and yn+1 are unknown)
as follows :
ȳn+1/2 = yn + h/2 f(yn,tn) (10)
ȳn+1 = yn + h[θ f(yn,tn)+(1- θ) f(ȳn+1/2, tn+1/2)] (11)
27
The Third order Runge-Kutta method
• Substituting (10) and (11) into (9), we get the 3rd order Runge-
Kutta method in the following form:
k1 = h f(yn,tn)
k2 = h f(yn + ½ k1,tn + ½ h)
28
The Fourth order Runge-Kutta method
k1 = h f(yn,tn)
k2 = h f(yn + ½ k1,tn + ½ h)
k4 = h f(yn + k3,tn + h)
k1 = h f(yn,tn)
30
The Fourth order Runge-Kutta method
y' = -1/(1+y2),
y(0)=1,
31
MATLAB functions to solve Differential Equations
32
MATLAB function: ODE23
• Command:
[T,Y] = ODE23(ODEFUN,TSPAN,Y0)
• Input parameters:
• TSPAN is the integral interval [t0,t1]
• Result:
• Each row in the resulting array Y corresponds to the time in the column
vector T
33
MATLAB function: ODE23
34
35