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

Lecture 2: Higher-order Methods

Midpoint (Modified Euler) Method


Problem: y '( x) = f ( x, y ), y ( x0 ) = y0

h
y 1 = yi + f ( xi , yi )
i+ 2
2
h
yi +1 = yi + h f ( x 1 , y 1 ), x 1 = xi +
i+ i+ i+ 2
2 2 2

or
h h
yi +1 = yi + h f ( xi + , yi + f ( xi , yi ) )
2 2

MAFE208IU-L12 2
Midpoint (Modified Euler) Method

( xi , yi )

xi x 1 xi +1
i+
2
h
y 1 = yi + f ( xi , yi ) , yi +1 = yi + h f ( x 1 ,y 1 )
i+ 2 i+ i+
2 2 2
MAFE208IU-L12 3
Midpoint (Modified Euler) Method

slope = f ( xi , yi )

( xi , yi )

xi x 1 xi +1
i+
2
h
y 1 = yi + f ( xi , yi ) , yi +1 = yi + h f ( x 1 ,y 1 )
i+ 2 i+ i+
2 2 2
MAFE208IU-L12 4
Midpoint (Modified Euler) Method

(x ,y )
slope = f ( xi , yi ) i+
1
2
i+
1
2

( xi , yi )

xi x 1 xi +1
i+
2
h
y 1 = yi + f ( xi , yi ) , yi +1 = yi + h f ( x 1 ,y 1 )
i+ 2 i+ i+
2 2 2
MAFE208IU-L12 5
Midpoint (Modified Euler) Method

slope = f ( x 1 ,y 1 )
i+ i+
(x 1 ,y 1 ) 2 2

i+ i+
2 2

( xi , yi )

xi x 1 xi +1
i+
2
h
y 1 = yi + f ( xi , yi ) , yi +1 = yi + h f ( x 1 ,y 1 )
i+ 2 i+ i+
2 2 2
MAFE208IU-L12 6
Midpoint (Modified Euler) Method

slope = f ( x 1 ,y 1 )
i+ i+
(x 1 ,y 1 ) 2 2
i+ i+
2 2

( xi , yi )

xi x 1 xi +1
i+
2

h
y 1 = yi + f ( xi , yi ) , yi +1 = yi + h f ( x 1 ,y 1 )
i+ 2 i+ i+
2 2 2
MAFE208IU-L12 7
Example 1
Use the Midpoint Method with h=0.1 for the
initial-value problem
y ' = 1+ x + y
2

y (0) = 1
to approximate y(0.1) and y(0.2)

MAFE208IU-L12 8
xi = a + ih = 0.1i
Solution h h 1 1
x 1 = xi + = a + ih + = a + (i + )h = 0.1(i + )
i+ 2 2 2 2
2

Problem: f ( x, y ) = 1 + x 2 + y, y0 = y (0) = 1, h = 0.1


Step1:
h
y 1 = y0 + f ( x0 , y0 ) = 1 + 0.05(1 + 0 + 1) = 1.1
0+ 2
2

y1 = y0 + h f ( x 1 ,y 1 ) = 1 + 0.1(1 + 0.0025 + 1.1) = 1.2103


0+ 0+
2 2

Step 2:
h
y 1 = y1 + f ( x1 , y1 ) = 1.2103 + .05(1 + 0.01 + 1.2103) = 1.3213
1+ 2
2

y2 = y1 + h f ( x 1 ,y 1 ) = 1.2103 + 0.1(2.3438) = 1.4446


1+ 1+
MAFE208IU-L12 2 2 9
Example 2
Find approximate values and the errors of the
solution of the initial-value problem by Midpoint
method
y ' = 3xy, 0  x  1/ 2,
with step size h=0.1, and initial condition y(0) = 1

MAFE208IU-L12 10
Solution
• Exact solution

y ' = 3xy  y '/ y = 3x   y '/ ydx =  3xdx


ln( y ) = 3 x / 2 + C
2

Using initial condition:


ln( y (0)) = ln(1) = 0 = C
 ln( y ) = 3 x / 2  y = e
2 3 x 2 /2

MAFE208IU-L12 11
Midpoint method:
h h
yi +1 = yi + h f ( xi + , yi + f ( xi , yi ) )
2 2

x y-exact y-E Error y-ME Error

0 1 1 0 1 0
0.1 1.015 1 0.01511 1.015 0.0001131
0.2 1.062 1.03 0.03184 1.061 0.0004764
0.3 1.145 1.092 0.05274 1.143 0.001187
0.4 1.271 1.19 0.08119 1.269 0.002445
0.5 1.455 1.333 0.1221 1.45 0.004621
MAFE208IU-L12 12
Exercise
Find approximate values and the errors of the
solution of the initial-value problem
y ' = 3 y − 2 x, 0  x  1,
with step size h=0.2, and initial condition y(0) = 2
by
a) Euler Method
b) Midpoint Method

MAFE208IU-L12 13
Heun’s Predictor Corrector Method
Problem: y '( x) = f ( x, y ), y ( x0 ) = y0

Predictor: yi0+1 = yi + h f ( xi , yi )
f ( xi , yi ) + f ( xi +1 , yi0+1 )
Corrector: yi +1 = yi + h
2

or
h
yi +1 = yi + ( f ( xi , yi ) + f ( xi + h, yi + h f ( xi , yi ) ) )
2

MAFE208IU-L12 14
Heun’s Predictor Corrector Method
(Prediction)

( xi +1 , yi0+1 )

( xi , yi )

xi xi +1

Prediction 0
yi +1 = yi + h f ( xi , yi )
MAFE208IU-L12 15
Heun’s Predictor Corrector Method
(Prediction)

( xi +1 , yi0+1 )
slope = f ( xi +1 , yi0+1 )

( xi , yi )

xi xi +1

Prediction y 0
i +1 = yi + h f ( xi , yi )
MAFE208IU-L12 16
Heun’s Predictor Corrector Method
(Correction)
f ( xi , yi ) + f ( xi +1 , yi0+1 )
slope =
2
( xi +1 , yi0+1 )

( xi , yi ) ( xi +1 , yi +1 )

xi xi +1

yi +1 = yi + ( f ( xi , yi ) + f ( xi +1 , yi0+1 ) )
h
2
MAFE208IU-L12 17
Example 1

Use the Heun's Method with h = 0.1 for the initial-value problem
y '( x) = 1 + x 2 + y
y (0) = 1
to approximate y(0.1) and y(0.2)

MAFE208IU-L12 18
Solution
Problem: f ( x, y ) = 1 + y + x 2 , y0 = y ( x0 ) = 1, h = 0.1

Step1:
Predictor: y10 = y0 + h f ( x0 , y0 ) = 1 + 0.1(2) = 1.2

Corrector: y1 = y0 + ( f ( x0 , y0 ) + f ( x1 , y10 ) ) = 1.215


h
2
Step 2:
Predictor: y20 = y1 + h f ( x1 , y1 ) = 1.4326

Corrector: y2 = y1 + ( f ( x1 , y1 ) + f ( x2 , y20 ) ) = 1.4452


h
2
MAFE208IU-L12 19
Example 2
Find approximate values and the errors of the
solution of the initial-value problem by Heun’s
method
y ' = 3xy, 0  x  1/ 2,
with step size h=0.1, and initial condition y(0) = 1

MAFE208IU-L12 20
Solution
• Exact solution

y ' = 3xy  y '/ y = 3x   y '/ ydx =  3xdx


ln( y ) = 3 x / 2 + C
2

Using initial condition:


ln( y (0)) = ln(1) = 0 = C
 ln( y ) = 3 x / 2  y = e
2 3 x 2 /2

MAFE208IU-L12 21
Heun method:
h
yi +1 = yi + ( f ( xi , yi ) + f ( xi + h, yi + h f ( xi , yi ) ) )
2

x y-exact y-Euler Error-E y-Heun Error-H

0 1 1 0 1 0
0.1 1.015 1 0.01511 1.015 0.0001131
0.2 1.062 1.03 0.03184 1.062 0.000248
0.3 1.145 1.092 0.05274 1.144 0.0004629
0.4 1.271 1.19 0.08119 1.27 0.0008695
0.5 1.455 1.333 0.1221 1.453 0.001677
MAFE208IU-L12 22
Exercise
Find approximate values and the errors of the
solution of the initial-value problem
dy
= yt − 1.1t , 0  t  2,
dt
with step size h=0.5, and initial condition y(0) = 1
by
a) Modified Euler Method
b) Heun’s Method

MAFE208IU-L12 23
Runge-Kutta methods
yi +1 = yi + h ( xi , yi , h)
where  ( xi , yi , h) is called the increment function:
 = a 1 k1 + a 2 k2 + ... + a n kn
k1 = f ( xi , yi )
k2 = f ( xi + p1h, yi + q11k1h)
k3 = f ( xi + p2 h, yi + q21k1h + q22 k2 h)
...
kn = f ( xi + pn −1h, yi + qn −1,1k1h + qn −1,2 k2 h + ... + qn −1,n −1kn −1h)

MAFE208IU-L12 24
Second-order Runge-Kutta methods
• The second-order version of Runge-Kutta
methods is
yi +1 = yi + h(a 1 k1 + a 2 k2 )
k1 = f ( xi , yi )
 = p1 ,  = q11
k2 = f ( xi +  h, yi +  k1h)
where
1 1
a1 + a2 = 1, a2 = , a2  =
2 2

MAFE208IU-L12 25
Derivation of 2nd-order Runge-Kutta Methods
yi +1 = yi + h(a 1 k1 + a 2 k2 )

k1 = f ( xi , yi ), k2 = f ( xi +  h, yi +  k1h)

yi +1 = yi + h ( a 1 f ( xi , yi ) + a 2 f ( xi +  h, yi +  hf ( xi , yi )) )
x = xi :
f ( x +  h, y +  h f ) = f +  h f x +  hf ( x, y ) f y + O (h 2 )

yi +1 = y ( x) + ( a1 + a2 ) h f ( x, y ) + h 2 ( a2 f x +  a2 f y f ) + O (h 3 )

Problem: Find  ,  , a1 , a2
to match as many terms of the Taylor series as possible.
h2 h3
y ( x + h) = y ( x) + hy '( x) + y ''( x) + y '''( x) + ...
2 6
MAFE208IU-L12 26
Derivation of 2nd-order Runge-Kutta
Methods
h2 h3
y ( x + h) = y ( x ) + hy '( x) + y ''( x) + y '''( x) + ...
2 6

yi +1 = y ( x) + ( a1 + a2 ) h f ( x, y ) + h 2 ( a2 f x +  a2 f y f ) + O (h 3 )

1 1
 a1 + a2 = 1,  a2 = ,  a2 =
2 2

y ' = f ( x, y)
y " = f x ( x, y) + f y ( x, y) y ' = f x ( x, y ) + f y ( x, y ) f ( x, y)
MAFE208IU-L12 27
Derivation of 2nd-order Runge-Kutta
Methods
yi +1 = yi + h(a 1 k1 + a 2 k2 )
k1 = f ( xi , yi )
k2 = f ( xi +  h, yi +  k1h)
1 1
a1 + a2 = 1,  a2 = ,  a2 =
2 2

Heun’s method and Midpoint method are special cases of 2nd-order R-K methods:

Heun's method: Modified Euler method:


1 1 1
a1 = , a2 = ,  = 1,  = 1 a1 = 0, a2 = 1,  = =
2 2 2
MAFE208IU-L12 28
4th-order Runge-Kutta Method
h
yi +1 = yi + ( k1 + 2k2 + 2k3 + k4 )
6
k1 = f ( xi , yi )
h h
k2 = f ( xi + , yi + k1 )
2 2
h h
k3 = f ( xi + , yi + k2 )
2 2
k4 = f ( xi + h, yi + hk3 )
i = 0,1, 2,...
MAFE208IU-L12 29
Example
• Find the approximate solution by fourth-order
Runge-Kutta method and the error of the
initial-value problem
y ' = 3xy, 0  x  1/ 2,
with step size h = 0.1, and initial condition y(0) = 1

MAFE208IU-L12 30
x yexact y Approximate Errors
0 1 1 0
0.1 1.01511 1.01511 2.11572e-009
Solution
0.2 1.06184 1.06184 2.30571e-008
0.3 1.14454 1.14454 1.02112e-007
0.4 1.27125 1.27125 3.55607e-007
0.5 1.45499 1.45499 1.11013e-006

MAFE208IU-L12 31
Exercise
Find the first two approximate values yi , i=1,2,
and the errors of the solution of the initial-value
problem
y ' = 3 y − 2 x, x  0,
with step size h=0.5, and initial condition y(0) = 1
by the 4th-order Runge-Kutta method

Exact solution: y = ( 6 x + 7e + 2 )
1 3x

9
MAFE208IU-L12 32

You might also like