Numerical Methods Ch:Ode Equation and Its Practical Application
Numerical Methods Ch:Ode Equation and Its Practical Application
SUPRIYA RAI
JRS
CONTENT
INTIAL VALUE PROBLEMS AND CORRELATION WITH ODE FUNCTION
EULER EQUATION
HUENNS METHOD/ AND MODIFIED EULER EQUATION
RK METHOD/Runge-Kutta methods
Solutions to Equations
The solution to a differential equation (unlike an algebraic equation) is a function. The problem
with this is there are many ways to describe functions. Some can be described in terms of some
equation relating the dependent and independent variable, some by a graph and some by a very
complicated rule.
The way we will solve a differential equation is to use the definition of a function. That is to say
if we are given a differential equation y(x) = f(x,y) with a boundary condition y(x0) = y0 and
another value of x, say xact we can find a number yact so that y(xact) = yact.
The point (x0,y0) is called the initial point and the point (xact,yact) is called the terminal point
.
yact
y(x) (xact,yact)
y0
(x0,y0)
x0 xact
Eulers Method
The method that Euler used to estimate a solution (i.e. the corresponding value of y for a
given value of x) of a differential equation was to follow the tangent line from the initial
point to the terminal point.
yn
Here we use the value yn to estimate the value
of yact. This can be directly computed from the yact
information given by the following equation. y(x) (xact,yact)
yn y0 f ( x0 , y0 )( xact x0 ) y0
(x0,y0)
yn y0 f ( x0 , y0 )( xact x0 )
x0 xact
The insight that Euler had was to see how this estimate could be improved on. The
strategy he used was to divide the interval [x0,xact] (or [xact,x0] in the case xact<x0) into
equal subintervals and recompute the tangent line as you go. This would not allow the
tangent line to drift far from the function itself. This would hopefully produce a more
accurate estimate for yact at the end.
To Apply Eulers Method (x4,y4)
1. Divide the interval n equal subintervals. (In
(x3,y3)
our example 4.) (xact,yact)
(x1,y1)
2. Compute the width of each subinterval which
is x=h=(xn-x0)/n. y0 (x2,y2)
(x0,y0)
3. Compute the sequence of points as follows: x
x1 x0 x
x0 x1 x2 x3 x4
( x1 , y1 )
y1 y0 f ( x0 , y0 ) x
x2 x1 x In general the coordinates of the
( x2 , y2 ) point (xn+1,yn+1) can be computed
y2 y1 f ( x1 , y1 ) x from the coordinates of the point
(xn,yn) as follows:
x3 x2 x
( x3 , y3 )
y3 y2 f ( x2 , y2 ) x xn1 xn x
( xn1 , yn1 )
x4 x3 x yn1 yn f ( xn , yn ) x
( x4 , y4 )
y4 y3 f ( x3 , y3 ) x
Example:
Given the differential equation to the right with its boundary
dy
dx x y 2
y(0) 1
conditions find the value of y(2) using Eulers method with 4
iterations.
3.
x1 0 12 12
ye 3
y1 1 0 2 1 12 1
x2 12 12 1
8
y(2) e 14.3919
3
4.
y2 1 2 1 12 1 18 1.125
1 2
x3 1 12 1.5
5.
y3 1.125 1 1.125 12 1.125 .5625 1.6875
2
x4 1.5 12 2
6.
y4 1.6875 1.5 1.6875
2
1
2 1.6875 1.89844 3.58594
Interpretation of Euler Method
y2
y1
y0
x0 x1 x2 x
7
Interpretation of Euler Method
Slope=f(x0,y0)
y1
y1=y0+hf(x0,y0)
hf(x0,y0)
y0
x0 x1 x2 x
h
8
Interpretation of Euler Method
y2 y2=y1+hf(x1,y1)
Slope=f(x1,y1)
hf(x1,y1)
Slope=f(x0,y0)
y1 y1=y0+hf(x0,y0)
hf(x0,y0)
y0
x0 x1 x2 x
h h
9
Local versus Global Error
At the initial condition we know the solution, y, exactly
First step introduces some error
Remaining steps have single step error plus previous accumulated
error
Ej = y(xj) yj is global truncation error
Difference between numerical and exact solution after several steps
This is the error we want to control
10
Euler Example Plot
0.8
0.7
0.6
0.5
0.4
y
Exact
Numerical
0.3
0.2
0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
11
Error Propagation
Behavior of Euler algorithm is typical of all algorithms for numerical
solutions
Error grows at each step
We usually do not know this global error, but we would like to control
it
Look at local error for Euler algorithm
Then discuss general relationship between local and global error
12
Taylor Series to Get Error
Expand y(x) in Taylor series about x = a
dy 1 d2y 1 d 3
y
y ( x) y ( a ) ( x a) 2
( x - a)
2
3
( x - a) 3 ....
dx x a 2! dx xa
3! dx xa
13
Local Euler Error
Result of Taylor series on last chart
1 d2y 2 1 d3y 3
y( x0 h) y( x0 ) hf ( x0 , y( x0 )) 2
h 3
h ....
2! dx 0 3! dx 0
15
Better Algorithms
Seek high accuracy with low computational work
Could improve Euler accuracy by cutting step size, but this is not
efficient
Use other algorithms that have higher order errors
Runge-Kutta methods typically used
This is a class of methods that use several function evaluation methods per
step
16
Second-order Runge Kutta
Huens method
y i01 y i hi 1 f ( xi , y i ) xi 1 xi hi 1
0 0
hi 1 y y h f ( x , y i 1 )
y i 1 yi f ( xi , y i ) f ( xi 1 , y i 1 )
0 i i 1 i 1 i 1
2 2
17
Runge-Kutta Methods
Third Order Runge Kutta (RK3)
K1 f ( xi , yi )
1 1
K 2 f ( xi h, yi K1h)
2 2
1
K 3 f ( xi h, yi K1h 2 K 2 h)
2
y ( x h) y ( x) K1 4 K 2 K 3
1
6
18
Runge-Kutta Methods
Fourth Order Runge Kutta (RK4)
K1 f ( xi , yi )
1 1
K 2 f ( xi h, yi K1h)
2 2
1 1
K 3 f ( xi h, yi K 2 h)
2 2
K 4 f ( xi h, yi K 3 h)
yi 1 yi K1 2 K 2 2 K 3 K 4
h
6
19
Fourth-order Runge Kutta
Uses four derivative evaluations per step
k1 2 k 2 2 k 3 k 4
y i 1 y i xi 1 xi hi 1
6
k1 hi 1 f ( xi , yi )
k 2 hi 1 f xi
hi 1
, yi
k1
2 2
k 3 hi 1 f xi
hi 1
, y i 2
k
2 2
k 4 hi 1 f ( xi hi 1 , y i k 3 )
20
Comparison of Methods
Look at Euler, Heun, Modified Euler and fourth-order Runge-Kutta
Solve dy/dx = e-y-x with y(0) = 1
Compare numerical values to exact solution y = ln( ey0 + e-x0 e-x)
Look at errors in the methods at x = 1 as a function of step size
Compare error propagation (increase in error as x increases)
21
Error versus Step Size for Simple ODE Solvers
1.E+00
dy
e x y with y y0 1 at x x0 0
1.E-01
1.E-02
1.E-03
dx
1.E-04
1.E-05
1.E-06
1.E-07 Euler
Error
1.E-08 Huen
1.E-09 Modified Euler
Runge-Kutta
1.E-10
1.E-11
1.E-12
1.E-13
1.E-14
1.E-15
1.E-16
1.E-07 1.E-06 1.E-05 1.E-04 1.E-03 1.E-02 1.E-01
Step size, h
22
Error Propagation in Solutions of ODEs
1.E+00
1.E-01
1.E-02
1.E-03
1.E-04
Euler (N = 10)
1.E-05 Euler (N = 100)
1.E-06 Euler N = 1,000)
Error
Huen (N =10)
1.E-07
Huen (N = 100)
1.E-08 Huen (N = 1,000)
1.E-09 RK4 (H = 10)
RK4 (N = 100)
1.E-10
1.E-11
1.E-12
1.E-13
1.E-14
0 0.2 0.4 0.6 0.8 1
23
x
Some Basic Concepts
A numerical method is convergent with the solution of the ODE if the
numerical solution approaches the actual solution as h 0 (with
increase in numerical precision at smaller h)
Mainly a theoretical concept; algorithms in use are convergent
Stability refers to the ability of a numerical algorithm to damp any
errors introduced during the solution
24
More on Stability
Finite difference equations in numerical algorithms, when iterated,
may be numerically increase without bound
Stability usually is obtained by keeping step size h small, sometimes
smaller than the h required for accuracy
For most ODEs stability is not a problem, but it is for stiff systems of
ODEs and for partial differential equations
25
Runge-Kutta Methods (Motivation)
26
Example 1
Second Order Runge-Kutta Method
Problem : h 0.01
dy
1 y 2 x3 , y (1) 4 f ( x, y ) 1 y 2 x 3
dx
x0 1, y0 4
Use RK 2 to find y (1.01), y (1.02)
Step 1 :
K1 f ( x0 , y0 ) (1 y0 x0 ) 18.0
2 3
y1 y0 K1 K 2 4
h 0.01
(18 16.92) 3.8254
2 2
27
Example 1
Second Order Runge-Kutta Method
Problem : h 0.01
dy
1 y 2 x3 , y (1) 4 f ( x, y ) 1 y 2 x 3
dx
Use RK 2 to find y (1.01), y (1.02) x1 1.01, y1 3.8254
Step 2 :
K1 f ( x1 , y1 ) (1 y1 x1 ) 16.66
2 3
y2 y1 K1 K 2 3.8254
h 0.01
(16.66 15.45) 3.6648
2 2
28
Numerically Solving ODE in Matlab (Example 1)
Problem :
dy
1 y 2 x3 , y (1) 4, over the interval [1,2].
dx
Step 1: Create a M-file for dy/dx as firstode.m
function yprime=firstode(x,y);
yprime=1+y^2+x^3;
Step 2: At a Matlab command window
>>[x,y]=ode45(@firstode,[1,2],-4);
>> [x,y]
Matlab returns two column vectors, the first with values of x and the
second with value of y.
29
Numerically Solving ODE in Matlab (Example 1)
5
>> plot(x,y,'+')
4
-1
-2
-3
-4
1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2
30
Solving a system of first order ODEs
Methods discussed earlier such as Euler, Runge-
Kutta,are used to solve first order ordinary
differential equations
31
The general approach to solve high order ODE
z1 ' z2
z ' 1 3z 6 z ,
y ' '3 y '6 y 1 convert
2 2 1
solve
y ' (0) 1; y (0) 4 4
Z (0)
1
Second order ODE Two first order ODEs
32
Conversion Procedure
high order ODE convert System of first order ODE solve
33
Example of converting High order ODE to
first order ODEs
Convert y ' '3 y '6 y 1, y ' (0) 1; y (0) 4
to a system of first order ODE
34
Example of converting High order ODE to
first order ODEs
old new Initial Equation
name name cond.
y z1 4 z '1 z2
y' z2 1 z '2 1 3z 2 6 z1
z '1 z2 4
z ' 1 3z 6 z , Z (0) 1
2 2 1
35
Numerically Solving high order ODE in Matlab
Example :
y' ' xy e x y '3 sin 2 x, y (0) 2, y ' (0) 8, over the interval [0,4].
z1 ' z2
,
z 2 ' xz1 e z 2 3 sin 2 x
x
2
Z ( 0)
8
36
Numerically Solving high order ODE in Matlab
Example :
y' ' xy e x y '3 sin 2 x, y (0) 2, y ' (0) 8, over the interval [0,1].
37
Numerically Solving ODE in Matlab
20
To plot y against x 18
16
>> plot(x, z(:,1))
14
Because the 12
vector z has
first 10
component
z1=y 8
2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
38