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

Calculo Numérico II

IF-392

Semana 02
Dr. Pierre Giovanny Ramos Apestegui

Cálculo Numérico II-IF392


Improvements of Euler’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.
• Two simple modifications are available to
circumvent this shortcoming:
• Heun’s Method
• The Midpoint (or Improved Polygon)
Method

Cálculo Numérico II-IF392


Learning Objectives:
• To be able to solve first order differential
equations using the Midpoint Method.
• To be able to solve first order differential
equations using the Heun’s Predictor
Corrector Method.

Cálculo Numérico II-IF392


Midpoint and Heun’s Predictor-Corrector
Methods

• Review Euler Method


• Heun’s Method
• Midpoint Method

Cálculo Numérico II-IF392


Euler Method
Problem Euler Method
y ( x) = f ( x, y ) y0 = y ( x0 )
y ( x0 ) = y0 yi +1 = yi + h f ( xi , yi )
for i = 1,2,...

2
Local Truncation Error O(h )
Global Truncation Error O(h)
Cálculo Numérico II-IF392
Introduction
Problem to be solved is a first order ODE :
y ( x) = f ( x, y ), y ( x0 ) = y0

 The methods proposed in this lesson have the


general form:
yi +1 = yi + h 
 For the case of Euler:  = f ( xi , yi )
 Different forms of  will be used for the
Midpoint and Heun’s Methods.
Cálculo Numérico II-IF392
Midpoint Method

Problem Midpoint Method


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

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

Local Truncation Error O(h 3 )


Global Truncation Error O(h 2 )
Cálculo Numérico II-IF392
Motivation
• The midpoint can be summarized as:
• Euler method is used to estimate the solution at
the midpoint.
• The value of the rate function f(x,y) at the mid
point is calculated.
• This value is used to estimate yi+1.
• Local Truncation error of order O(h3).
• Comparable to Second order Taylor series
method.

Cálculo Numérico II-IF392


Midpoint Method

( xi , yi )

x0 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
Cálculo Numérico II-IF392
Midpoint Method

slope = f ( xi , yi )

( xi , yi )

x0 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
Cálculo Numérico II-IF392
Midpoint Method

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

( xi , yi )

x0 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
Cálculo Numérico II-IF392
Midpoint Method

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

i+ i+
2 2

( xi , yi )

x0 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
Cálculo Numérico II-IF392
Midpoint Method

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

( xi , yi )

x0 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
Cálculo Numérico II-IF392
Example 1

Use the Midpoint Method to solve the ODE


y ( x) = 1 + x + y
2

y (0) = 1
Use h = 0.1. Determine y(0.1) and y(0.2)

Cálculo Numérico II-IF392


Example 1
Problem : f ( x, y ) = 1 + x 2 + y , y0 = y (0) = 1, h = 0.1
Step 1 :
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+
2 2

Cálculo Numérico II-IF392


Modified Euler Method
Problem: First Step
𝑷𝒓𝒆𝒅𝒊𝒄𝒕𝒐𝒓:
𝑦ሶ 𝑥 = 𝑓 𝑥, 𝑦 From the Euler method:
𝑦0 = 𝑦(𝑥0 )
𝒚𝟏 = 𝒚𝟎 + 𝒙𝟏 − 𝒙𝟎 𝒇(𝒙𝟎 , 𝒚𝟎 )
Second Step
𝑪𝒐𝒓𝒓𝒆𝒄𝒕𝒐𝒓:

1
Average derivative= 𝑓 𝑥0 , 𝑦0 + 𝑓 𝑥1 , 𝑦1
2

The new value


of 𝑦1 is:
Cálculo Numérico II-IF392
Modified Euler Method

The iterative equation for 𝑦𝑖 is:


Cálculo Numérico II-IF392
Modified Euler Method
If we use Simpson's 1/3 method to determine the corrector

Equating the equations


The iterative equation for 𝑦𝑖 is:

Cálculo Numérico II-IF392


Exercise:
Write an Octave or Matlab script to solve:

(a) Solve with the Midpoint method using h = 0.1

In each part, calculate the error between the true solution


and the numerical solution at the points where the
numerical solution is determined.

Cálculo Numérico II-IF392


Heun’s Predictor
Corrector

Cálculo Numérico II-IF392


Heun’s Predictor Corrector Method

Problem Heun' s Method


y ( x ) = f ( x, y ) y0 = y ( x0 )
y ( x0 ) = y0 Predictor : yi0+1 = yi + h f ( xi , yi )

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

Local Truncation Error O(h 3 )


2
Global Truncation Error O(h )
Cálculo Numérico II-IF392
Heun’s Predictor Corrector
(Prediction)

( xi +1 , yi0+1 )

( xi , yi )

xi xi +1

Prediction 0
yi +1 = yi + h f ( xi , yi )
Cálculo Numérico II-IF392
Heun’s Predictor Corrector
(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 )
Cálculo Numérico II-IF392
Heun’s Predictor Corrector
(Correction)
f ( xi , yi ) + f ( xi +1 , yi0+1 )
slope =
2
( xi +1 , yi0+1 )

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

xi xi +1

y1
i +1 = yi +
h
2
(
f ( xi , yi ) + f ( xi +1 , yi0+1 ) )
Cálculo Numérico II-IF392
Example 2
Use the Heun' s Method to solve the ODE
y ( x) = 1 + x + y
2

y (0) = 1
Use h = 0.1. One correction only
Determine y(0.1) and y(0.2)

Cálculo Numérico II-IF392


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

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

Corrector : y = y0 +
1
1
h
2
( )
f ( x0 , y0 ) + f ( x1 , y10 ) = 1.2105

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

Corrector : y = y1 +
1
2
h
2
( )
f ( x1 , y1 ) + f ( x2 , y20 ) = 1.4452
Cálculo Numérico II-IF392
Summary
• Euler, Midpoint and Heun’s methods are similar in the
following sense:

yi +1 = yi + h  slope
• Different methods use different estimates of the slope.
• Both Midpoint and Heun’s methods are comparable in
accuracy to the second order Taylor series method.

Cálculo Numérico II-IF392


Problem
Consider the following first-order ODE:

(a) Solve with the Euler method using h = 0.2.


(b) Solve with the Midpoint method using h = 0.2.
(b) Solve with the Heun's method using h = 0.2.

The analytical solution of the ODE is:

𝟏
𝒚=
𝒆−𝒙 𝒙𝟐 + 𝟐𝒙 + 𝟐 + 𝟏
In each part, calculate the error between the true solution and the
numerical solution at the points where the numerical solution is
determined.
Cálculo Numérico II-IF392
Comparison
Local Global
Method truncation truncation
error error

Euler Method yi +1 = yi + h f ( xi , yi ) O(h 2 ) O ( h)


Heun's Method
Predictor : yi0+1 = yi + h f ( xi , yi ) O(h 3 ) O(h 2 )
k +1
Corrector : y = yi +
i +1
h
2
(
f ( xi , yi ) + f ( xi +1 , yik+1 ) )
h
Midpoint y 1 = yi + f ( xi , yi ) O(h 3 ) O(h 2 )
i+ 2
2

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

Cálculo Numérico II-IF392


Runge-Kutta Methods (RK)
• Runge-Kutta methods achieve the accuracy of a
Taylor series approach without requiring the
calculation of higher derivatives.
𝑦𝑖+1 = 𝑦𝑖 + 𝜑(𝑥𝑖 , 𝑦𝑖 , ℎ)ℎ
𝜑 = 𝑎1 𝑘1 + 𝑎2 𝑘2 + ⋯ + 𝑎𝑛 𝑘𝑛 Increment function
𝑎′𝑠 = constants
𝑘1 = 𝑓(𝑥𝑖 , 𝑦𝑖 )
𝑘2 = 𝑓(𝑥𝑖 + 𝑝1 ℎ, 𝑦𝑖 + 𝑞11 𝑘1 ℎ) p’s and q’s are constants
𝑘3 = 𝑓(𝑥𝑖 + 𝑝2 ℎ, 𝑦𝑖 + 𝑞21 𝑘1 ℎ + 𝑞22 𝑘2 ℎ)

𝑘𝑛
= 𝑓(𝑥𝑖 + 𝑝𝑛−1 ℎ, 𝑦𝑖 + 𝑞𝑛−1,1 𝑘1 ℎ + 𝑞𝑛−1,2 𝑘2 ℎ + ⋯
+ 𝑞𝑛−1,𝑛−1 𝑘𝑛−1 ℎ)
Cálculo Numérico II-IF392
• k’s are recurrence functions. Because each k is a
functional evaluation, this recurrence makes RK
methods efficient for computer calculations.
• Various types of RK methods can be devised by
employing different number of terms in the increment
function as specified by n.
• First order RK method with n=1 is in fact Euler’s
method.
• Once n is chosen, values of a’s, p’s, and q’s are
evaluated by setting general equation equal to terms in
a Taylor series expansion.
Second-Order Runge-Kutta Methods

yi +1 = yi + (a1k1 + a2 k 2 )h
Cálculo Numérico II-IF392
Second-Order Runge-Kutta Methods

yi +1 = yi + (a1k1 + a2 k 2 )h

• Remember: The Taylor series for a function of two


variables is defined as:
𝝏𝒈 𝝏𝒈
𝒈 𝒙 + 𝒓, 𝒚 + 𝒔 = 𝒈 𝒙, 𝒚 + 𝒓 +𝒔 +⋯
𝝏𝒙 𝝏𝒚
• Remember: That 𝑓 𝑥𝑖 , 𝑦𝑖 must be determined by
derivation using the chain rule:
𝝏𝒇(𝒙𝒊 , 𝒚𝒊 ) 𝝏𝒇(𝒙𝒊 , 𝒚𝒊 ) 𝒅𝒚
𝒇′ 𝒙𝒊 , 𝒚𝒊 = +
𝝏𝒙 𝝏𝒚 𝒅𝒙
Cálculo Numérico II-IF392
• Values of a1, a2, p1, and q11 are evaluated by setting the second
order equation to Taylor series expansion to the second order
term. Three equations to evaluate four unknowns constants are
derived.
We have : yi +1 = yi + ( a1k1 + a2 k 2 ) h
f ' ( xi , yi ) 2
However yi +1 = yi + f ( xi , yi ) h + h
2!
f ( xi , yi ) f ( xi , yi ) dy
But f ' ( xi , yi ) = +
x y dx
 f ( xi , yi ) f ( xi , yi ) dy  h 2
Then yi +1 = yi + f ( xi , yi ) h +  +
 x y dx  2!
k1 = f ( x i , yi )
k 2 = f ( xi + p1h, yi + q11k1h)
We now expand k 2 = f ( xi + p1h, yi + q11k1h)
f ( xi , yi ) f ( xi , yi )
k 2 = f ( xi , yi ) + p1h + q11k1h
x y
Cálculo Numérico II-IF392
• We replace k1 and k2 in yi +1 = yi + (a1k1 + a2 k 2 )h to get
  f ( xi , yi ) f ( xi , yi ) 
yi +1 = yi + a1 f ( xi , yi ) + a2  f ( xi , yi ) + p1h + q11k1h h
  x y 
or
f ( xi , yi )
yi +1 = yi + a1h f ( xi , yi ) + a2 h f ( xi , yi ) + a2 p1h 2
x
f ( xi , yi )
+ a2 q11 f ( xi , yi )h 2

y

Compare with  f ( xi , yi ) f ( xi , yi )  h2
yi +1 = yi + f ( xi , yi )h +  + f ( xi , yi )
 x y  2!

and obtain a1 + a2 = 1 (3 equations-4 unknowns)


1
a 2 p1 =
2
1
a2 q11 =
2 Cálculo Numérico II-IF392
• Because we can choose an infinite number of values for
a2, there are an infinite number of second-order RK
methods.
• Every version would yield exactly the same results if
the solution to ODE were quadratic, linear, or a
constant.
• However, they yield different results if the solution is
more complicated (typically the case).
• Three of the most commonly used methods are:

• Huen Method with a Single Corrector (a2=1/2)


• The Midpoint Method (a2=1)
• Raltson’s Method (a2=2/3)

Cálculo Numérico II-IF392


• Huen Method with a Single Corrector (a2=1/2)

• The Midpoint Method (a2=1)

• Raltson’s Method (a2=2/3)

Cálculo Numérico II-IF392


Classical Fourth-Order Runge-
Kutta Method
The most popular RK methods are fourth-order, and
the most commonly used form is:
1
yi+1 = yi + (k1 + 2k2 + 2k3 + k4 )h
6
where: k1 = f (t i , yi )
 1 1 
k2 = f t i + h, yi + k1h 
  2 2 
 1 1 
k3 = f t i + h, yi + k2 h 
 2 2 
k4 = f (t i + h, yi + k3h)

Cálculo Numérico II-IF392


Exercise
Solving by hand a first-order ODE using the fourth-order
Runge-Kutta method.

Using h = 0.5.
Solution:

The first point of the solution is (0,3), which is the point where the initial
condition is given. The values of x and y at the first point are x1=0 and y1=3.
The rest of the solution is done in steps. In each step the next value of the
independent variable is calculated by:

Cálculo Numérico II-IF392


The value of the dependent variable Yi+1 is calculated by first
evaluating K1, K2, K3 and K4 using:

And then substituting the Ks:

First step: In the first step i = 1

Cálculo Numérico II-IF392


Second step: In the second step i = 2

Cálculo Numérico II-IF392


Third step: In the third step i = 3

Cálculo Numérico II-IF392


Cálculo Numérico II-IF392
Cálculo Numérico II-IF392

You might also like