Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lec7 Numerical Model

Download as pdf or txt
Download as pdf or txt
You are on page 1of 58

Universiti Teknologi PETRONAS

MDB 3053
Numerical Methods

ODE – Initial Value Problem

Reading Assignment: Chapter 25


Lesson Outcomes

By the end of the lesson, students should be able

1. To solve ODE for initial value problem (IVP) using


numerical methods

2. To solve ODE for boundary value problem


(BVP) using numerical methods

2
ORDINARY DIFFERENTIAL EQUATION (ODE)
• ODE are equations which are composed of an
unknown function f and its derivatives df /dt

• ODE play a fundamental role in engineering because


many physical phenomena are best formulated
mathematically in terms of the rate of change.

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

closed-form solution vs numerical solution


4
Applications of ODE

• Heat Transfer
dT
Q( x)  kA
dx

• Fick’s law of mass transfer


dC
j ( x)   DAB
dx

• Fluid Dynamics – Rate equation for draining of a tank

dh
Atank  Qin  Qout  Qin  A2 2 gh
dt

5
Applications OF ODE

• Newton’s Law of Cooling


dT
 k (T  Tw )
dx

• Pendulum swing

d 2 g
2
 sin   0
dt l

6
ORDINARY DIFFERENTIAL EQUATION (ODE)

• ODE involves one dependent variable. A partial


differential equation (or PDE) involves two or more
independent variables.

• Differential equations are also classified as to their


order.
 A first order equation contains a first derivative:
dT
 k (T  Tw )
dx
 A second order equation includes a 2nd order derivative.
d 2 g
 sin   0
dt 2 l
• Higher order equations can be reduced to a system of
first order equations, by redefining a variable.

7
TYPES OF ODE

First Order ODE:

dv c v(0)  a
 g  v;
dt m

Second Order ODE:

d 2 y  2 dy  y  0; dy  a; y b
dx2 dx dx x0
x0

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:

new value  old value  slope  step size

• The formula can be applied step-by-step to compute


out into the future.

• 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.

new value  old value  slope  step size


yi 1  yi  f ( xi , yi )  h
slope
forward point base point

step size, h  x  xi1  xi


12
Comparison between Euler’s method and true solution

yi 1  yi  h  f ( xi , yi )

True error

Initial
condition:
y(0) = 1

In this graph, Euler’s method has over-estimated the solution! 13


ERROR ANALYSIS
Error Analysis for Euler’s Method
• Euler’s method is accurate up to 1st order
• Two types of error:
Truncation error – caused by chopping
f ( xi , yi ) 2
Ea  h
2!
Ea  O ( h )
2  local error is 2 nd order, but

accurate up to 1st order globally

Round-off errors – caused by the limited no


of significant digits that can be stored
14
EXAMPLE: EULER’S METHOD
x y
Given y' with y(2)  2
x
calculate y(2.5) use step, h = 0.25 Exact = 3.05786

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.

• One method is to improve the slope by determining two (2)


derivatives:
 at the initial point 2 derivatives are averaged to obtain an
 at the end point improved estimate of the slope

f ( xi , yi )  f ( xi 1 , yi01 ) K 0  K1
slope  
2 2
Heun’s Method
Trapezoidal rule’s
Predictor : yi01  yi  f ( xi , yi )h expression

f ( xi , yi )  f ( xi 1 , yi01 )
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

• Heun’s is more superior than


Euler’s method because
decreasing h decreases the error
at a faster rate.
• Heun’s is accurate up to 2nd
order

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

from x = 0 to x = 4 with a step size of 1. initial condition: y(0) = 2.


Predictor : yi01  yi  f ( xi , yi )h
K0 = 4e0 – 0.5(2) = 3
y0i+1 = 2 + 3(1) = 5 f ( xi , yi )  f ( xi 1 , yi01 )
Step 1 K1 = 4e0.8(1) – (5)(0.5) = 6.4021 Corrector : yi 1  yi  h
2
(x = 1) slope = (K0 + K1)/2 = 4.7011
f ( xi , yi )  f ( xi 1 , yi01 ) K 0  K1
slope  
yi+1 = 2 + 4.7011(1) = 6.7011 2 2

K0 = 4e0 – 0.5(6.7011) = 5.5516


Step 2 y0i+1 = 6.7011 + (1)(5.5516) = 12.2527
(x = 2) K1 = 4e0.8(2) – 0.5(12.2527) = 13.6858
slope = (K0 + K1)/2 = 9.6187

yi+1 = 6.7011 + (1)(9.6187) = 16.3198  accurate up to 2nd order


22
Example: Modified Euler’s or Heun’s Method

Use Heun’s method to integrate: y ' 


x  y
with y(2)  2
x
Calculate y(2.5) use h = 0.25 Exact = 3.05786

Solution Ans: 3.0557, 0.07%

Predictor : yi01  yi  f ( xi , yi )h

f ( xi , yi )  f ( xi 1 , yi01 )
Corrector : yi 1  yi  h
2

23
SOLUTION
x y
y' with y(2)  2
22 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.252.0  2.1111  2.5139
2

2.25  2.5139
K0   2.11728
2.25
Step 2 yi01  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

slope at midpoint : yi1/ 2  f ( xi 1/ 2 , yi 1/ 2 )  K1

Midpoint Method:

yi 1  yi  f ( xi 1/ 2 , yi 1/ 2 )h

or yi 1  yi  K1h
25
Midpoint Method

taking slope at midpoint

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

True solution: y  0.5x 4  4 x3  10 x 2  8.5x  1

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

new estimated slope 

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

ans: y(1) = 3.0


Solution  = 4.4375
yi 1  yi   ( xi , yi , h)h
1
  (k1  2k 2  2k3  k 4 )
6
where
k1  f ( xi , yi )
1 1
k 2  f ( xi  h, yi  k1h)
2 2
1 1
k3  f ( xi  h, yi  k 2 h)
2 2
k 4  f ( xi  h, yi  k3h)

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!

 accurate up to 4th order 36


CLASS ACTIVITY
Use RK4 method to solve ODE with a y  4e0.8 x  0.5 y
step size of h = 0.5, initial condition
y(0)=2. Find y(1) = ? exact y(1) = 6.19463

ans: y(1) = 6.1950


Solution
yi 1  yi   ( xi , yi , h)h
1
  (k1  2k 2  2k3  k 4 )
6
where
k1  f ( xi , yi )
1 1
k 2  f ( xi  h, yi  k1h)
2 2
1 1
k3  f ( xi  h, yi  k 2 h)
2 2
k 4  f ( xi  h, yi  k3h)

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

 accurate up to 4th order


38
39

EULER’S METHOD(MATLAB Code)


function [x,y] = EulerMethod(f, xinit, xend, yinit, h)
% Number of iterations
N = (xend-xinit)/h; function dydx = myfunction(x,y)
dydx = 2*x – 4*x*y;
end
% Initialize arrays
% The first elements take xinit and yinit,
% the rest fill with 0
x = [xinitzeros(1, N)];
y = [yinit zeros(1, N)];

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

To run code, type:


> a = 0; b= 2; ya = 4; h = 0.1;
> [x,y]=EulerMethod(‘myfunction’,a, b, ya, h)
40

HEUN’S METHOD(MATLAB Code)


function [x,y] = HeunsMethod(f, xinit, xend, yinit, h)
% Number of iterations
N = (xend-xinit)/h; function dydx = myfunction(x,y)
dydx = 2*x – 4*x*y;
end
% Initialize arrays
% The first elements take xinit and yinit,
% the rest fill with 0
x = [xinitzeros(1, N)];
y = [yinit zeros(1, N)];

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

Euler’s method Heun’s method RK4

• Simplest scheme • Derivative based on • Popular , stable and


• Derivative based on average of 2 points: accurate
1 initial point initial and end point • Derivates based on
• Fast computation • More accurate than 4 points
time Euler’s method • More computation
• not so accurate for • Up to second-order time
function with abrupt accurate • Up to fourth-order
change accurate
• Accurate up to 1st-
order (linear)

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

y (0.2)  yi  h  y  yi  h  zi  2  (0.1)(0.2)  1.98


t = 0.2
z (0.2)  zi  h  z   zi  h  (0.5ti  yi )  0.2  (0.1)(1.95)  0.395

dz
 0.5ti  yi  0.5(0.1)  2  1.95
dt

y (0.3)  yi  h  y  yi  h  zi  1.98  (0.1)(0.395)  1.9405


t = 0.3
z (0.3)  zi  h  z   zi  h  (0.5ti  yi )  0.395  (0.1)(1.88)  0.583

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

y (0.4)  yi  h  y  yi  h  zi  1.9405  (0.1)(0.583)  1.8822


t = 0.4
z (0.4)  zi  h  z   zi  h  (0.5ti  yi )  0.583  (0.1)(1.7905)  07621

dz
 0.5ti  yi  0.5(0.3)  1.9405  1.7905
dt

• Table of solutions:

48
SUMMARY

• 2 numerical techniques to perform ODE:


• Initial Value Problem – Euler’s, Heun’s, RK4
• Boundary value Problem – Shooting Method

 We have covered Chapter 25 & 27 in textbook

35
Universiti Teknologi PETRONAS

MDB 3053
Numerical Methods

**ODE – Boundary Value Problem

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

If we define the values at the extreme points or boundaries of a


system  they are called BVP

Example boundary conditions: T = T1 at x = 0, T = T2 at x = L


51
Formulation of BVP
d 2T
2
 h(T  Ta )  0
dx
Given known parameters :
Ta  20
solve by using the
L  10 m shooting method
h  0.01 m  2 (Heat transfer coefficient) which is a trial-
and-error approach
T (0)  T1  40
Boundary Conditions
T ( L)  T2  200

Analytical Solution: T  73.4523e0.1x  53.4523e 0.1x  20

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

• A trial-and-error approach is then implemented to solve the


IVP. Let’s make a guess, say z(0)=10.
• The solution is then obtained by integrating the two 1st order
ODEs simultaneously.

53
**Shooting Method

Step 1: Assume z(A) = y’(A) = a0 and solve using one-step


method until the solution at x = B is satisfied. Let this
solution be y0(x)  first shooting
Step 2: Assume z(A) = y’(A) = a1 and solve using one-step
method until the solution at x = B is satisfied. Let this
second solution be y1(x)  2nd shooting

Step 3: if at x = B, y0(B) = b0 and y1(B) = b1 . Then use


the linear form of Lagrange interpolation to compute the
value of z(A) = ? such that it meets the correct y(B) value.

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

We can use either NDD or Lagrange linear interpolation to find


z(0):
55
**Shooting Method

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

 1st shooting, guess


z(0) = 10, solving T(10)
= 168.3797 (< 200)

 2nd shooting, guess


z(0) = 20, solving T(10) =
285.8980 (> 200)

 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

You might also like