0% found this document useful (0 votes)
95 views35 pages

Chapter 6 - Ordinary Differential Equations

This document discusses numerical methods for solving ordinary differential equations. It introduces the forward Euler, backward Euler, and modified Euler methods. It also discusses the Runge-Kutta method and its advantages over Euler methods in achieving higher accuracy with larger step sizes.

Uploaded by

Tâm Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views35 pages

Chapter 6 - Ordinary Differential Equations

This document discusses numerical methods for solving ordinary differential equations. It introduces the forward Euler, backward Euler, and modified Euler methods. It also discusses the Runge-Kutta method and its advantages over Euler methods in achieving higher accuracy with larger step sizes.

Uploaded by

Tâm Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

1

Chapter 6: Ordinary Differential Equation

Scientific Computing

SoICT 2023

2
Contents

• Differential Equations
• Euler method
• Runge-Kutta method
• MATLAB functions

3
Differential Equations

• Finding solutions of differential equations is usually divided


into two types (initial value problems and boundary
condition problems) depending on whether we need to find
solutions that satisfy initial or boundary conditions.

• Most initial value problems describe systems that are


considered time-dependent, and the solution of the problem
depends on the initial condition.

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
'

where y' is the first derivative of y, f(y,t) is a function of two variables


y and t, y(t0)=y0 is the initial condition of the problem.

• If f does not depend on y, then y' can be calculated by integrating


the function f.

• 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

The numerical method requires calculating the value at the


time grid- point: tn = tn-1 + h; n = 1, 2, …; h is the step
length.

Approximate solution
Exact solution

y0
h

t0 t1 t2 t3 t4 t5

7
The Existence and Uniqueness of solution

• Theorem 1: If f is a continuous function on the rectangle:


R = {(t,y) : |t – t0| ≤ α, |y – y0| ≤ β }
then IPV has a solution y(t) with |t - t0| ≤ min{α, β /M},
where M = max {|f(t,y)| : (t,y) ϵ R}.
• Theorem 2: If both f and ∂ f/ ∂ y are continuous on a rectangle
R = {(t,y) : |t – t0| ≤ α, |y – y0| ≤ β }
then IPV has a unique solution y(t) with |t - t0| ≤ min{α, β /M}, where M = max
{|f(t,y)| : (t,y) ϵ R}.
• Theorem 3: Suppose t0 is in the interval [a,b]. If f is continuous with a ≤ t ≤ b,
- ∞ ≤ y ≤ ∞ and y is Liptchitz continuous, that is, we can find a constant L such
that for all y1, y2 and t in [a,b] we have
|f(t,y2) - f(t,y1)| ≤ L|y2 - y1|
then IVP has a unique solution y(t) on the interval [a,b].

8
Forward Euler (FE)

• Considering differential equation: y' = f(y,t)


Forward Euler method is obtained by using the forward
difference formula to approximate y’:
yn +1 − yn '
≈ y n = f ( y n , tn )
h (1)
yn +1 = yn + hf ( yn , tn )
(1) => (2)
• We can rewrite (2) in an iterative form as follows:
y1 = y0 + hf ( y0 , t0 )
y2 = y1 + hf ( y1 , t1 )
...... (3)
yn = yn −1 + hf ( yn −1 , tn −1 )
9
FE: Example

• Consider differential equation:


y' = -20y + 7e-0.5t ,
y(0) = 5
• Solve above differential equation with t in [0, 0.04],
h=10-2;10-3;10-4
• Estimate the error, given that the exact solution of is:
y = 5e-20t + (7/19.5)(e-0.5t - e-20t)

10
FE: Example

t h = 0.01 h = 0.001 h = 0.0001


Result Error Result Error Result Error
0.01 4.07000 0.08693 4.14924 0.0769 4.15617 0.0076
0.02 3.32565 0.14072 3.45379 0.1259 3.46513 0.0124
0.03 2.72982 0.17085 2.88524 0.1544 2.89915 0.0153
0.04 1.87087 0.18440 2.42037 0.1684 2.43554 0.0167

11
Forward Euler

• FE method is simple, but it has two disadvantages:


• Large rounding error.
• Instability occurs when the time constant of the equation is
negative, unless the time step h is small enough.
• E.g: consider y' = - αy, y(0) = y0 where y0 > 0, α > 0.
The exact solution of the problem is: y = y0 e-αt , y goes to
0 as t goes to infinity.

12
Forward Euler

• If you solve it with FE method, then:


• If αh < 1 then the solution is minimized and positive
• If αh > 1, the sign of the solution is alternate.
Especially if αh > 2 then the amplitude of the solution increases
with each step, and the solution fluctuates.
=> Unstable

13
FE for a System of Differential Equations

• Consider a system of Differential Equations :

y’ = f(y,z,t), y(0) = y0

z’ = g(y,z,t), z(0) = z0 (5)

• Forward Euler for a system of Differential Equations:

yn+1 = yn + h f(yn,zn,tn)

zn+1 = zn + h g(yn,zn,tn) (6)

14
FE for a Second order Differential Equation

• To solve higher-order differential equation, we can decompose it


into a system of first-order differential equations.
• For example: Consider second order differential equation:
y’’(t) – 0.05 y’(t) + 0.15 y(t) = 0
y’(0) = 0 (7)
y (0) = 1
Let y’ = z then rewrite (7) as following
y’ = z, y(0) = 1,
z’ = 0.05z - 0.15 y, z(0) = 0 (8)
(8) becomes a system of first-order differential equations

15
Modified Euler method

• Modified Euler method is more accurate and stable than FE


method. Modified FE bases on trapezoidal rule to calculate
integral y'=y(t):
yn+1 = yn + h/2 [f(yn+1, tn+1) + f(yn,tn)] (8)
• If f is linear with y then (8) is linear with yn+1, so we can
easily determine yn+1
• If f is nonlinear with y then (8) is nonlinear yn+1. Finding yn+1
is similar to solving nonlinear equations as studied in
Chapter 4.

16
Modified Euler method

• Use modified FE to solve differential equation with h=0.1:


y’ = -y1.5 + 1, y(0) = 10, with 0 ≤ t ≤ 1
• Applying modified FE:
yn+1 = yn + h/2 [-(yn+1)1.5 – (yn)1.5 + 2](9)
with n = 0:
y1 = y0 + h/2 [-(y1)1.5 – (y0)1.5 + 2]
The best approximation for y1 on the right-hand side is y0.
Let y1 = y0:
y1 = y0 + h/2 [-(y0)1.5 – (y0)1.5 + 2] (10)
• Similarly, we can calculate yn

17
Backward Euler (BE)

• Cónider y’ = f(y,t), Backward Euler is obtained by using the backward


difference formula to approximate y’:

yn +1 − yn (1)
≈ yn' +1 = f ( yn +1 , tn +1 )
h

(1) => yn +1 = yn + hf ( yn +1 ,t n +1) (2)

• 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

• To find yn+1 we can solve (2) as a nonlinear equation as described in


chapter 4.

18
BE: Example

• Consider y’ = y3, y(0) = 1.

Applying BE with h=0.5:

y1 = y0 + h f(y1,t1) = 1 + 0.5 (y1)3 (3)

(3) can be solved by Newton iterative method:

yk = yk-1 – f(yk-1)/f’(yk-1)

MATLAB function:

fzero('x+0.5*x^3-1',1)

19
Summary of Euler method

• The Forward Euler method is based on the forward difference


approximation. Its error within an iteration is proportional to h2
and its global error is proportional to h. This method is simple but
has a large error and high instability.
• The modified Euler method is based on the trapezoid rule. Its
error within an iteration is proportional to h3 and the global error
is proportional to h2.
• The Backward Euler method is based on backward difference
approximation. Its error is similar to the Forward Euler method.
However, this method is stable, so it is used to solve non-smooth
problems (which are difficult to solve by other methods).

20
Runge-Kutta method (RK)

• The disadvantage of the Euler methods is that the order of


accuracy is small. High accuracy requires h to be very small.
• In Runge-Kutta method, the order of accuracy is increased by
using the intermediate points in each iteration.
• Consider: y’ = f(y,t), y(0) = y0 (1)
To calculate yn+1 at tn+1 = tn + h with yn known, we integrate the
above equation in the interval [tn,tn+1] as follows :
t n +1

yn +1 = yn + ∫ f ( y, t )dt
tn
(2)

The Runge-Kutta method was developed by applying integral


methods to integrate the right-hand side of (2).

21
The Second order Runge-Kutta method

• We apply the trapezoid rule to the right-hand side of (2) as


follows:
t n +1
1
∫ f ( y , t ) dt = h [ f ( y n , tn ) + f ( y n +1 , tn +1 ) ] (3)
tn
2
• In (3), yn+1 is unknown. Thus, the second term is approximated by
f(ȳn+1,tn+1), where ȳn+1 is the first estimate of yn+1 computed
according to the Forward Euler.
• The method obtained in this way is called the second order
Runge-Kutta formula

22
The Second order Runge-Kutta method

• The second order Runge-Kutta formula :

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

• The second order Runge-Kutta method is equivalent to the modified


Euler method applied with only one iteration.

• The accuracy order of the second-order Runge-Kutta PP is h2, which


equal to the modified Euler method with the condition that the iterative
procedure for solving nonlinear equations in it is convergent.

Thus, using the second order Runge-Kutta method with a sufficiently


small h is better than using modified Euler method.

• Using the second order Runge-Kutta method is quite simple.

24
RK for second order differential equation

• Consider second order differential equation:

y’’(t) + a y’(t) + b y(t) = q(t)

y(0) = 1, y’(0) = 0, (6)

where a and b are constants, q(t) known.

Let z=y’(t), we have:

y’ = z, y(0) = 1,

z’ = q(t) – a z – b y, z(0) = 0 (7)

25
RK for second order differential equation

• Applying the Second order Runge-Kutta method:

k1 = h z n

l1 = h (qn – a zn – b yn)

k2 = h (zn + l1)

l2 = h (qn+1 -a (zn + l1) – b (yn + k1) (8)

yn+1 = yn + ½ (k1 + k2)

zn+1 = zn + ½ (l1 + l2)

26
The Third order Runge-Kutta method

• The 3rd order Runge-Kutta method is based on applying a higher-


accuracy order integral to the 2nd term in equation (2).
Using Simson1/3 formula, (2) is approximated by:

yn+1 = yn + h/6 [f(yn,tn) + 4f(ȳn+1/2, tn+1/2) + f(ȳn+1, t1)] (9)

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)

where θ (unknown) is used to determine the maximum accuracy of the


method.

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)

k3 = h f(yn + θk1 + (1- θ)k2,tn + h) (12)

yn+1 = yn + 1/6 (k1 + 4k2 + k3)

• It can be shown that θ = -1 is optimal.

28
The Fourth order Runge-Kutta method

• The development of the fourth-order Runge-Kutta method is similar to


the third-order Runge-Kutta method, except that there is an additional
intermediate step in calculating the derivative. The fourth-order Runge-
Kutta method has a local error proportional to h3.

• The fourth-order Runge-Kutta method is based on the Simson1/3 rule:

k1 = h f(yn,tn)

k2 = h f(yn + ½ k1,tn + ½ h)

k3 = h f(yn + ½ k2,tn + ½ h) (13)

k4 = h f(yn + k3,tn + h)

yn+1 = yn + 1/6 (k1 + 2k2 + 2k3 + k4)


29
The Fourth order Runge-Kutta method

• The fourth-order Runge-Kutta method is based on the Simson3/8


rule:

k1 = h f(yn,tn)

k2 = h f(yn + 1/3 k1,tn + 1/3 h)

k3 = h f(yn + 1/3 k1+ 1/3 k2,tn + 2/3 h) (14)

k4 = h f(yn + k1 – k2+ k3,tn + h)

yn+1 = yn + 1/8 (k1 + 3k2 + 3k3 + k4)

30
The Fourth order Runge-Kutta method

• Example 1: Calculate y(1) by solving:

y' = -1/(1+y2),

y(0)=1,

using the fourth order Runge-Kutta method with h=1

31
MATLAB functions to solve Differential Equations

• DE solving functions: ODE45, ODE113, ODE15S, ODE23S, ODE23T,


ODE23TB

• Setting parameter: ODESET, ODEGET

• Simulate result: ODEPLOT, ODEPHAS2, ODEPHAS3, ODEPRINT

• Find solution: dsolve

32
MATLAB function: ODE23

• Command:

[T,Y] = ODE23(ODEFUN,TSPAN,Y0)

• Input parameters:
• TSPAN is the integral interval [t0,t1]

• Y0 is the initial value

• ODEFUN(T,Y) returns the column vector corresponding to the value f(t,y)

• Result:
• Each row in the resulting array Y corresponds to the time in the column
vector T

33
MATLAB function: ODE23

• E.g.: y’ = sin t, y(0) = 1, 0 ≤ t ≤ 2PI.


(Given exact solution: y = -cos(t) + 2)
• MATLAB script
% Write a function that calculates the value of the function on the right side:
function dydt = fp(t,y)
% Calculate value for y’ = sin(t)
dydt = [sin(t)];
% Find solution using ODE23
[t,y] = ode23(@fp,[0,2*pi],[1]);
plot(t,y);
hold on
plot(t,-cos(t),'r')

34
35

You might also like