Lec7 Numerical Model
Lec7 Numerical Model
Lec7 Numerical Model
MDB 3053
Numerical Methods
2
ORDINARY DIFFERENTIAL EQUATION (ODE)
• ODE are equations which are composed of an
unknown function f and its derivatives df /dt
dv c v - dependent variable
g v t - independent variable
dt m
Here v is a function of t ONLY
3
Physical Phenomenon – solving by ODE
bungee jump
• Heat Transfer
dT
Q( x) kA
dx
dh
Atank Qin Qout Qin A2 2 gh
dt
5
Applications OF ODE
• Pendulum swing
d 2 g
2
sin 0
dt l
6
ORDINARY DIFFERENTIAL EQUATION (ODE)
7
TYPES OF ODE
dv c v(0) a
g v;
dt m
d 2 y 2 dy y 0; dy a; y b
dx2 dx dx x0
x0
8
TYPES OF ODE
Linear ODE:
y ' 2 y ' y 0
Nonlinear ODE:
y" 2 y ' y 2 0
9
ODE for Initial-Value Problem (IVP)
ODE : y 2 x 3 12 x 2 20 x 8.5
Initial condition : y 1 at x 0
Solution : y 0.5 x 4 4 x 3 10 x 2 8.5 x 1
y vs x IVP
need to ‘pin’ down the
solution by forcing it to pass
through the initial condition
i.e. y(0) = 1
dy/dx (slope) vs x
10
ODE – Numerical Solution
• One-Step Method:
• Numerical Methods:
- Euler’s Method
- Modified Euler or Heun’s Method
- Midpoint Method
- 4th-order Runge-Kutta
11
EULER’S METHOD
• Euler’s method is the simplest way to solve ODE of the form
dy
ODE: f ( x, y ) find y = ?
dx
• Strategy: A new value of yi+1 is predicted using the slope to
extrapolate linearly over the step size h.
yi 1 yi h f ( xi , yi )
True error
Initial
condition:
y(0) = 1
Solution yi 1 yi h f ( xi , yi )
Step 1
x = 2.25 f ( xi , yi ) y (2 2) / 2 2
y (2.25) yi h f ( xi , yi ) 2 0.25(2) 2.5
2.25 2.5
Step 2
y(2.5) 2.5 0.25 3.0278
x = 2.5
2.25
The exact solution is y(2.5) = 3.058
3.0278 3.05786
Error = 100 0.99% accurate up to 1st order
15
3.05786
SOLUTION
x y y x ln x (1 ln 2) x
y' Exact
solution:
x
y(2) = 2
16
How to Improve Euler’s Method
• Improvement by decreasing the step size h
• In general, if h is decreased by a factor of n, then error will be
decreased by about a factor of n as well linear proportional
• Approximation in Euler’s is worse if the function has abrupt
change
less
error
17
CLASS ACTIVITY
x y
Use Euler’s method: y' with y(2) 2
x
Calculate y(2.5) use h = 0.125 Exact = 3.05786
yi 1 yi h f ( xi , yi )
Ans: 3.0425, 0.5%
18
Modified Euler’s Method (or Heun’s Method)
• A fundamental source of error in Euler’s method is that the
derivative at the beginning of the interval is assumed to apply
across the entire interval.
f ( xi , yi ) f ( xi 1 , yi01 ) K 0 K1
slope
2 2
Heun’s Method
Trapezoidal rule’s
Predictor : yi01 yi f ( xi , yi )h expression
f ( xi , yi ) f ( xi 1 , yi01 )
Corrector : yi 1 yi h
2
19
Modified Euler’s Method (or Heun’s Method)
• Since Heun’s method is based on direct expression of
trapezoidal rule, the local truncation error is
f ( ) 3
Et h
12
• Approximated local error:
Ea O(h ) but accurate up to 2nd order globally
3
20
Euler’s Method
K 0 K1
2
Heun’s Method
improved slope
21
EXAMPLE – HEUN’S METHOD
Use Heun’s method to integrate ODE y 4e0.8 x 0.5 y
Predictor : yi01 yi f ( xi , yi )h
f ( xi , yi ) f ( xi 1 , yi01 )
Corrector : yi 1 yi h
2
23
SOLUTION
x y
y' with y(2) 2
22 x
K0 2.0
2 Exact : y x ln x (1 ln 2) x
yi 1 yi h K0 2 0.25(2) 2.5
0
Step 1
2.25 2.5
t = 2.25 K1 2.1111
2.25
y(2.25) 2 0.252.0 2.1111 2.5139
2
2.25 2.5139
K0 2.11728
2.25
Step 2 yi01 yi h K 0 2.5139 0.25(2.11728) 3.0432
t = 2.5
2.5 3.0432
K1 2.21728
2.5
0.25
y (2.5) 2.5139 (2.11728 2.21728)
2
3.0557
3.0557 3.05786
3.05786
100 0.07% accurate up to 2nd order 24
**Midpoint (or Improve Polygon) Method
• Another method is to improve the slope by determining the
derivative:
at the midpoint of the interval i.e. h/2
h
midpoint : yi 1/ 2 yi f ( xi , yi )
2 half step: h/2
Midpoint Method:
yi 1 yi f ( xi 1/ 2 , yi 1/ 2 )h
or yi 1 yi K1h
25
Midpoint Method
26
Runge-Kutta Methods (RK)
• Runge-Kutta (RK) methods achieve the accuracy of a
Taylor series approach without requiring the
calculation of higher derivatives.
yi 1 yi ( xi , yi , h)h
a1k1 a2 k 2 an k n is an increment function for slope
a ' s constants
k1 f ( xi , yi ) k’s are recurrent terms
k 2 f ( xi p1h, yi q11k1h)
k3 f ( xi p2 h, yi q21k1h q22 k 2 h)
k n f ( xi pn 1h, yi qn 1k1h qn 1, 2 k 2 h qn 1,n 1k n 1h)
27
2nd-order RK Method
• Various types of RK can be devised by using different
increment function as specified by n
• If n = 1, RK becomes Euler’s method
• For n = 2, use Ralston’s method (1962):
yi 1 yi ( xi , yi , h)h
1 2
k1 k 2
3 3
where
k1 f ( xi , yi )
3 3
k 2 f ( xi h, yi k1h)
4 4
28
Comparisons
29
Comparisons ODE : y 2 x 3 12 x 2 20 x 8.5
Initial condition : y 1 at x 0
less error
30
**EXAMPLE
Use Ralston’s method to solve ODE with y 2 x 3 12 x 2 20 x 8.5
a step size of h = 0.5, initial condition
y = 1, x = 0. Find y(1) = ? exact y(1) = 3.0
1 2
yi 1 yi h ( k1 k 2 ) ans: y(1) = 3.10156
Solution 3 3
3 3
k1 f ( xi , yi ) k 2 f ( xi h, yi k1h)
4 4
31
SOLUTION y 2 x 3 12 x 2 20 x 8.5
Ralston’s Method:
at step #1 (x = 0.5)
y 0.5x 4 4 x3 10 x 2 8.5 x 1
32
4th-Order Runge-Kutta Methods (RK4)
• The most popular RK method using n = 4
yi 1 yi ( xi , yi , h)h
1 • RK4 yields exact result if
(k1 2k 2 2k3 k 4 ) the solution to the ODE is
6 constant, linear or quadratic
where
• this method reduces to
k1 f ( xi , yi ) Simpson’s 1/3-rule
1 1 • RK4 is accurate up to
k 2 f ( xi h, yi k1h)
2 2 fourth-order term
1 1
k3 f ( xi h, yi k 2 h)
2 2
k 4 f ( xi h, yi k3h)
33
Graphical depiction of slope estimates in RK4 method
h
yi 1 yi (k1 2k2 2k3 k4 )
6
34
EXAMPLE
Use RK4 method to solve ODE with a y 2 x 3 12 x 2 20 x 8.5
step size of h = 0.5, initial condition y
= 1, x = 0. Find y(1) = ? exact y(1) = 3.0
35
SOLUTION y 2 x 3 12 x 2 20 x 8.5
RK4 Method:
at step #1 (x = 0.5)
slope, = 4.4375
exact!
37
SOLUTION
RK4 Method: y 4e0.8 x 0.5 y
at step #1 (x = 0.5)
K0 3 K1 3.5106
Step 1 K 3 3.4468 K 4 4.1056
t = 0.5 1
slope (3 2 * 3.5106 2 * 3.4468 3.1056) 3.5034
6
y (2.5) 2 0.5(3.5034) 3.7517
slope, = 3.5034
for i=1:N
x(i+1) = x(i)+h;
%
% Euler’s
y(i+1) = y(i) + h*feval(f,x(i),y(i))
end
end
for i=1:N
x(i+1) = x(i)+h;
% Predictor
ynew = y(i) + h*feval(f,x(i),y(i));
% Corrector
y(i+1) = y(i) + h*feval(f,x(i),y(i))/2 + h*feval(f,x(i+1),ynew)/2;
end
end
To run code, type:
> a = 0; b= 2; ya = 4; h = 0.1;
> [x,y]=HeunsMethod(‘myfunction’,a, b, ya, h)
41
RK4 METHOD
MATLAB has its built-in RK4 unction called as ODE45
RK4 Method
Cont’d – RK4 Method
COMPARISON
• Comparison between different numerical ODEscheme
44
Second Order Differential Equation
Chapter 27 in Textbook
SOLVING 2nd ORDER ODE
• Use Euler’s method to solve 2nd order ODE as below, use h = 0.1
d2y
2
0.5t y 0 y (0) 2 y(0) 0
dt
• Step 1: rewrite this as two 1st order ODE system
dy
Let z y (0) 2
dt
dz
0.5t y y(0) z (0) 0
dt
• Step 2: apply Euler’s method to dz/dt first, then find zi+1, followed
by dy/dt, and finally find yi+1
t=0 y ( 0) 2
z ( 0) 0
t = 0.1
y (0.1) yi h y yi h zi 2 (0.1)(0) 2
z (0.1) zi h z zi h (0.5ti yi ) 0 (0.1)(0.5)(0) 2 0.2
46
SOLVING 2nd ORDER ODE
• Continue for next few steps : dy
zi 0.2
dt
dz
0.5ti yi 0.5(0.1) 2 1.95
dt
dz
0.5ti yi 0.5(0.2) 1.98 1.88
dt
47
SOLVING 2nd ORDER ODE
dy
zi 0.583
• Continue for next few steps : dt
dz
0.5ti yi 0.5(0.3) 1.9405 1.7905
dt
• Table of solutions:
48
SUMMARY
35
Universiti Teknologi PETRONAS
MDB 3053
Numerical Methods
Chapter 27 in Textbook
**BOUNDARY-VALUE PROBLEM (BVP)
Temperature distribution in a rod due to convection based
on Newton’s cooling. T1 > T2 and T2 > Ta
d 2T
2
h(T Ta ) 0
dx
52
**Shooting Method
• Converts the BVP to initial-value problem (IVP).
• For example, the 2nd order equation can be expressed as two
first order ODEs:
dT
z
dx
dz
h(T Ta )
dx
53
**Shooting Method
54
**Shooting Method
Step 1:
First guess: z(0)=10 , use 4th order RK method with a step size of 2:
z(0)=10 T(10)=168.3797.
Step 2:
This differs from T(10)=200. Therefore a new guess is made,
z(0)=20 and the computation is performed again.
z(0)=20 T(10)=285.8980
Step 3:
Since the original ODE is linear, and 2 sets of points, (z, T)1 and
(z, T)2, are linearly related:
z(0) = 10 resulting T(10) = 168.3797
z(0) = 20 resulting T(10) = 285.898
NDD interpolation:
z ( x0 ) z ( x1 )
z x z ( x0 ) (T T0 )
(T0 T1 )
(20 10)
z ( x) 10 (200 169.3797) 12.6907
(285.898 169.3797)
OR
Lagrange Interpolation:
( z z1 ) ( z z0 )
T x T0 T1
( z0 z1 ) ( z1 z0 )
( z 20) ( z 10)
200 (169.3797) (285.8980)
(10 20) (20 10) solve to get z(0) = 12.6907
56
Shooting Method
Linear interpolate to
pin down T(10) = 200,
compute z(0) = 12.6907
57
**CLASS ACTIVITY
Solve BVP below for the second-order ODE using shooting method,
use h = 0.5
u 6t 0 t 1
Boundary conditions: u (0) 0, u (1) 1 Answer: z(0)=0.25
58