You are on page 1of 22

Part 7 NUMERICAL SOLUTION OF ODE MAT 575

Chapter 12

Euler’s &
Taylor’s
Method
At the end of this chapter, students should be able to:
 Recall ordinary differential equation and Taylor’s series
 Derive and apply Euler’s method
 Apply Taylor’s series of higher order in approximation problem involving
differential equation

12.1 Introduction
Differential equations serve as mathematical descriptions for many physical
phenomena. A few examples of commonly occurring differential equations
are:
a) ay(x) + by(x) + cy(x) = F(x)

b) E l yiv (x)  w(x)

w
c) y(x)  1  (y(x))
y
 Equation (1) occurs in the study of vibrating or oscillating mechanical
systems or in electrical circuits.
 Equation (2) arises in the study of beam deflections.
 Equation (3) deals with problems in cable suspension.

193
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

In many scientific and engineering applications, the independent variable x


(or sometimes t) normally represents time. Take a note that in this chapter
we are going to concentrate only on differential equations of first and second
order.
The numerical methods of solving initial value problems that we are going to
discuss here namely are
 Euler’s method
 Taylor’s Series Method
 Runge-Kutta Method
Euler’s and Taylor’s methods are actually derived from the Taylor’s series.
Taylor series will be introduced first to give you the idea of finding
approximation.

12.2 Ordinary Differential Equation


For a start it will be useful to review some elementary definitions and
concepts from the theory of differential equations.

Definition
A differential equation (DE) is an equation containing one or more derivatives
of an unknown function.

dn y dn1y d2 y dy
an  a n1   a2  a1  a0 y  f ( x )
dxn dxn1 dx 2
dx

Order Notation
dy
First order  D y  y
dx
d2 y
Second order 2
 D 2 y  y 
dx

The order of DE is the order of the highest derivative that it contains. Usually
x is used as independent variable, and y for dependent variable.

An ordinary differential equation of order n will then have the form:

yn  f(x, y(x), y(x), y3 (x), ......, yn-1(x)) nth order

y  f(x, y(x)) 1st order

194
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

y  f(x, y(x), y(x)) 2nd order


In a first course of differential equations, students learn various techniques for
solving some selected classes of differential equations, for example, methods
of solving separable equations, exact equations and first order linear
equations. The vast majority of equations encountered in practice however,
cannot be solved analytically or exactly. Thus, an alternative method is
needed in order to solve the equations by numerical methods (or
approximations).
Let’s review an example of solving a first order differential equation the
analytical way (i.e. seeking the exact solution).

Example 1
dy
Consider a differential equation - y  0 . Find the function y that will
dx
satisfy the equation.

Solution
Applying method of variable separable in ordinary differential equation
yields
1
dy  dx
y
1
 dy   dx
y
ln y = x + c

e x  c  exec  y
Hence,

y  ke x (k = ec a constant)
What we can say about the above equation is that it has many solutions

depending on the value k. The function y  kex is called the general


solution. Pictorially the solutions for the differential equation will be as below:
Conditions: Particular Solution:

If y(0) = 1 : y 0  1, x0  0 y  ex

If y(0) = 3 : y 0  3 , x0  0 y  3ex

195
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

y  3ex
y  2ex

y  ex

Figure 12.1
The conditions above (i.e. x 0 , y 0 ) are called initial values. Solving differential
equations with initial conditions are sometimes called initial value problems.
Thus, an initial value problem always consists of two parts:
(i) the differential equation y ' (x)  f(x, y(x)) , x defined on [a, b]

(ii) the initial condition y(a)  y0 , where a is actually x 0 i.e. y(x0 )  y0


The solution obtained from initial value problem is known as particular
solution.

Example 2
Solve the initial value problem.
dy
 x2  6x2 y , y(0)  2
dx

Solution
dy
 x 2  6x 2 y
dx
dy
 6x 2 y  x 2
dx
Identify p(x)  6x2

Determine Integrating Factor, I  x   e 


6x2dx

3
 e2x
Rewrite the standard form
ye2x  e2x x 2 dx
3 3
 Let u  2x3

du  6x 2 dx
1 u

6e du

196
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

1 u

e C
6
1
  e2x  C
3

6
Hence the general solution is given by
1 3
y  Ce2x
6
To determine the particular solution used the initial value given.
x0 ; y  2
1
2    C 1
6
11
C
6
Therefore the particular solution is;
1 11 2x3
y  e
6 6

Warm up exercise
dy
(i) Given the differential equation, ( 4  x )  x 1 y .
dx
a. Separate the common terms.
b. Integrate both side.
c. Write the equation in a general form.

dy
(ii) Given the initial value problem, x  y  x2  x  1 , y(1)  2 .
dx
a. Write the differential equation in a standard form of linear equation.
b. Find the Integrating Factor.
c. Find the particular solution for the differential equation.

12.3 Taylor Series

The Taylor series is a representation of a function as an infinite sum of terms


calculated from the values of its derivatives at a single point. The Taylor
series are named in honor of an English mathematician, Brook Taylor.

197
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

Definition
Let y(x) be a function with derivatives of all orders throughout some interval
containing c as an interior point. Then the Taylor series generated by y(x) at
x = c is
(n )
y( x )  y(c )  y' (c )(x  c ) 
y ' ' (c )
x  c 2  ...  y (c ) x  c n  ...
2! n!
 yn c 
  x  c n
n 0 n !

Corollary
A function y(x + c) can be expanded in a series of positive integral powers of
x:

y' ' ( c ) 2 y (n ) (c ) n
y( x  c )  y(c )  y' (c )x  x  ...  x  ...
2! n!

Steps
Identify the function
Identify the center
Identify the form
Differentiate f(x) and evaluate at the center

Example 3
1
Represent as a power series in
x 1
(a) x  1 (b) x

Solution

1
(a) Identify function: f (x) 
x 1
Identify center: c=1
Identify form:

f (c ) (n )
f ( x )  f (c )  f (c )(x  c )  x  c 2  ...  f (c ) x  c n  ...
2! n!

198
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

Differentiate f(x) and evaluate at center c = 1.


Differentiate Evaluate

 x  11
1 1
f (x)  f (1) 
x 1 2
1 1
f ( x )  x  1 2  f (1) 
x  1 2
22

f ( x )  2x  1 3 


2 2
f (1) 
x  1 3
23
6 6
f ( x )  6x  1 4  f (1) 
x  1 4
24

f iv ( x )  24x  1 5 
24 24
f iv (1) 
x  1 5
25
Plug into the identify form.

f (1)  iv
f ( x )  f (1)  f (1)(x  1)  x  12  f (1) x  13  f (1) x  14  ...
2! 3! 4!
1 1 1
  ( x  1) 
2
x  12 
6
x  13 
24
x  14  ...
x  1 2 22 2!2 3
3!2 4
4!2 5

1  x  1 x  1 2
x  1  x  1  ...
3 4
 1   
2  2 22 23 24 

When determining the Taylor series in term of (x – c), do not do any
simplification. Just leave it as it is.
1
(b) Identify function: f ( x  1) 
x 1
Identify center: c = 1

f (c ) 2 f (n ) ( c ) n
Identify form: f ( x  c )  f (c )  f (c )x  x  ...  x  ...
2! n!
Differentiate f(x) and evaluate at center c = 1
Differentiate Evaluate
1
f ( x)   x 1 f (1)  1
x
1
f ( x )  x  2  f (1)  1
x2
2
f ( x )  2x  3  f (1)  2
x3

199
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

6
f ( x )  6x  4  f (1)  6
x4
24
f iv ( x )  24x  5  f iv (1)  24
5
x
Plug into the identify form.

f (1) 2 f (1) 3 f iv (1) 4


f ( x  1)  f (1)  f (1)x  x  x  x  ...
2! 3! 4!
1
 1  x  x 2  x 3  x 4  ...
x 1

Warm up exercise
Consider the following function and represent f(x) as a power series
in (x – c) and x.

(i) f ( x)  x  1 ; c=1

(ii) f ( x)  ln(x  1) ; c = -1

12.4 Euler’s Method


Euler’s method will be the simplest version of Taylor series method. Due to
its large truncation error, Euler’s method is rarely used in practice. Euler’s
method uses the first two terms in Taylor’s series (it is actually Taylor series
of order 1).

y' ' ( x ) 2 y (n ) ( x ) n
y( x  h)  y( x )  y' ( x )h  h  ...  h  ...
2! n!
In the first step we compute
y( x0  h)  y( x0 )  y' ( x0 )h

y( x1)  y( x0 )  y' ( x0 )h

y1  y0  hy'0
dy
Let  y' ( x )  f ( x, y ) , then Euler’s method can be written as:
dx
y( x0  h)  y( x0 )  hf( x0, y0 )

y( x1)  y( x0 )  hf ( x0, y0 )

200
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

For i = 0, 1, 2, 3, …
y( xi1)  y( xi )  hf ( xi, y( xi ))
or
yi1  yi  hf(xi, y(xi ))

yi1  yi  hf( xi, yi )

dy
Note that  y' ( x ) is the first order derivatives and rewritten as function of
dx
two variables f(x, y). Hence, f(xi, yi) implies that the function is evaluated at xi
and yi. Geometrically it is an approximation of the curve y(x) by a polygon
whose side is tangent to the curve at x 0 .

y The value of y is estimated


using the slope at xi ,
extrapolated linearly over the
step size h0

Estimate

True

h
x
xi xi+1

Figure 12.1

Steps – Euler’s method


dy
 Identify  y' ( x )
dx
 Rewrite y' ( x)  f ( x, y)
 Identify x 0 , y0  y( x0 ) and h
 Compute f ( x0, y0 )
 Apply Euler’s method
o yi1  yi  hf( xi, yi )

Example 4
Given
dy
 y, y(0)  1.
dx
Using h  0.01 , estimate y(0.01), y(0.02), y(0.03), y(0.04) using Euler’s
method.

201
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

Solution
dy
 Identify  y' ( x )
dx
dy
y
dx
 Rewrite y' ( x)  f ( x, y)

f ( x, y)  y

 Identify x 0 , y0  y( x0 ) and h

x0  0 y0  1 h  0.01

 Compute f ( x0, y0 )

f ( x0, y0 )  y0
f (0,1)  1
 Apply Euler’s method
yi1  yi  hf( xi, yi )

y1  y 0  hf ( x 0 , y 0 )
 1  (0.01)f (0,1)
 1  (0.01)(1)  1.01
Repeat for i = 1, 2, 3 :
y2 = y1 + h f(x1 , y1)
=1.01 + (0.01) (1.01)
=1.0201

y3 = y2 + h. f(x2 , y2)
=1.0201 + (0.01) (1.0201)
=1.0303

y4 = y3 + h f(x3 , y3)
=1.0303 + (0.01) (1.0303)
=1.0406

202
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

Example 5
Given y '  t  y where y(0) = 2. Apply Euler’s method to the initial value
problems to find y(1.0) choosing:
(a) h = 0.5.
(b) h = 0.2.

Compare with the exact solution; y(t)  3et - t - 1. What is your observation?

Solution

Given
y ' t  y and y(0) = 2
rewrite
y '  f (t, y)  t  y
where
t0  0 y0  2
a) h  0.5 (2 steps)
y(0.5) = y1 = y0 + h(t0 , y0)
= 2 + (0.5) (2)
= 3

y(1.0) = y2 = y1 + h f(t1 , y1)


= 3 + (0.5) (3.5)
= 4.75

i t f(t,y) Euler Exact Error


0 0 2 3 2 1.0000
1 0.5 3.5 4.75 3.446164 1.3038
2 1 5.75 7.625 6.154845 1.4702

b) h = 0.2 (5 steps)
y(0.2) = y1 = 2.4
y(0.4) = y2 = 2.92
y(0.6) = y3 = 3.584
y(0.8) = y4 = 4.4208
y(1.0) = y5 = 5.4650

203
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

h = 0.2
i t f(t, y) Euler Exact Error
0 0 2 2.4 2 0.4000
1 0.2 2.6 2.92 2.464208 0.4558
2 0.4 3.32 3.584 3.075457 0.5085
3 0.6 4.184 4.4208 3.866456 0.5544
4 0.8 5.2208 5.46496 4.876623 0.5883
5 1.0 6.46496 6.757952 6.154845 0.6031

To increase accuracy use a smaller step size (h) or use more steps.

Example 6
Consider the initial value problem y '  1  t sin(ty) , 0 ≤ t ≤ 2 , y(0) = 0. Use
Euler’s method to approximate y(0.2).

Solution

h = 0.1

i t f(t,y)  1  t sin(ty) Euler

0 0 1 0.1
1 0.1 1.001 0.2001

Warm up exercise
dy
Consider  x 2  6x 2 y with y(0) = -2.
dx
(i) Identify x 0 and y 0

(ii) Identify and compute f ( x0, y0 )


(iii) Approximate y(1) choosing h = 0.5.

204
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

12.5 Taylor’s Series Method


As stated earlier Taylor’s Series is given by:

h2 h3 3 h4 4
y(x  h)  y(x)  hy ' (x)  y ' ' (x)  y (x)  y (x)  ........
2! 3! 4!

Taylor’s series order

h2
y(x  h)  y(x)  hy ' (x)  y ' ' (x) 2
2!

h2 h3 3
y(x  h)  y(x)  hy ' (x)  y ' ' (x)  y (x) 3
2! 3!

h2 h3 3 h4 4
y(x  h)  y(x)  hy ' (x)  y ' ' (x)  y (x)  y (x)
2! 3! 4!
4
.
.

h2 hn n
y(x  h)  y(x)  hy ' (x)  y ' ' (x)  ............  y (x) n
2! n!

Note that y ' (x)  f(x, y(x)) . By differentiation,

y ' ' (x)  f ' (x, y(x))


and the Taylor’s series becomes

h2 h3 h4
y(x  h)  y(x)  hf(x, y(x))  f' (x, y(x))  f' ' (x, y(x))  f' ' ' (x, y(x))  ...
2! 3! 4!
where f, f ' , f ' ' , f ' ' ' are evaluated at (x, y(x)).

Steps – Taylor’s method


 Identify the required order of Taylor series
 Identify f ( x, y )
 Determine f ' ( x, y) , f ' ' ( x, y) ; etc
 Identify x i , y i and h
 Compute f ( xi, yi ) , f ' ( xi, yi ) , f ' ' ( xi, yi ) ; etc
 Apply relevant Taylor’s method
h2 h3
yi 1  yi  hf(xi, yi )  f' (xi, yi )  f' ' (xi, yi )
2! 3!
h4
 f' ' ' (xi, yi )  ........
4!

205
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

For a small value of h, the higher powers h 2 , h3 , h 4 , ..... will be very small
and the corresponding terms can be ignored. The omission of further terms
causes an error, which is called the truncation error. In calculating the
approximation, the more number of terms used means the more accurate is
our answer.

Example 7
Use Taylor series of order 2 to estimate y(0.2) if

y' ( x)  y  x2  1 and y(0) = 0.5

Solution

 Identify the required order of Taylor series

h2
yi1  yi  hf(xi, yi )  f' (xi, yi )
2!
 Identify f ( x, y )

f ( x, y)  y  x2  1
 Determine f ' ( x, y) , f ' ' ( x, y) ; etc
f ' ( x, y)  y'2x
 f ( x, y)  2x
 Identify x i , y i and h

x0  0 , y0  0.5 and h = 0.2

 Compute f ( xi, yi ) and f ' ( xi, yi )

f ( x 0 , y 0 )  y 0  x 02  1
f (0, 0.5)  0.5  0  1
 1.5

f ' ( x 0 , y 0 )  f ( x 0 , y 0 )  2x 0
 1.5
 Apply relevant Taylor’s method

h2
y(0.2)  y(0)  hy'(0)  y' ' (0)
2!

h2
y1  y0  (0.2)f(x0 , y 0 )  f' (x0 , y 0 )
2!

206
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

(0.2)2
y1  0.5  (0.2)(1.5) (1.5)
2
 0.83

Hence, y(0.2)  0.83 .

Example 8
Derive the Taylor’s series method of order 4 for the ODE
dy
 y  et
dt
y( 0 )  1
and estimate y(0.1) and y(0.2).

Solution

Taylor’s series of order 4 is given by:

h2 h3 h4
yi1  yi  hf(ti, yi )  f' (ti, yi )  f' ' (ti, yi )  f' ' ' (ti, yi )
2! 3! 4!

f (t, y)  y  et
f ' ( t, y )  y'e t
 f ( t, y )  e t
f ' ' ( t, y )  y' ' e t
 f ( t, y )  e t  e t
 f ( t, y )  2e t
f ' ' ' ( t, y )  f ' ( t, y )  2e t
 f ( t, y )  e t  2e t
 f ( t, y )  3e t
h2 h3 h4
y i 1  yi  hf(ti , y i )  f' (ti , y i )  f' ' (ti , y i )  f' ' ' (ti , y i )
2! 3! 4!
h2
 y i  hf(ti , y i ) 
2!

f (ti, yi )  e t i 
h3
3!
 
f ( t i , y i )  2e t i 
h4
4!
 
f ( t i , y i )  3e t i 
 2 3
h 
4 h 2 3
h  ti
4
f(ti , y i )   
h h h
 y i  h     e
 2! 3! 4!   2 3 8 

Given
t0  0 y0  1 and h = 0.1

207
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

 h 2 h3 h 4   h 2 h3 h 4  t
y i 1  y i  h    f(ti , y i )     e
 2! 3! 4!   2 3 8 

y(0.1)  y 0  0.1 

0.12 0.13 0.14 
2!

3!

4! 
 t
 y0  e 0  
 0.12 0.13 0.14  t
 2

3
 e 0
8 

 
 1  0.1052 1  e 0  0.0053e0
 1  0.10522  0.0053
 1.2157

y(0.2)  y(0.1  0.1)


 y( t1  h)
 y2

 y1  0.1 

0.12 0.13 0.14 
2!

3!

4! 
t

 y1  e 1 )  
 2

3

 0.12 0.13 0.14  t

8
e 1

 
 1.2157  0.1052 1.2157  e0.1  0.0053e0.1
 1.4657

Example 9
For the IVP

y0  1 ;
dy
 2y  2 - e- 4t
dt
estimate y(0.1) using:
a) Euler’s method.
b) Taylor’s method of order 2.
c) Taylor’s method of order 4.

e4t e2t
Compare them to the exact solution y( t )  1   .
2 2

Solution

f (t, y)  2 - e-4t  2y y0  1


a) Euler’s method
y( x  h)  y( x )  hf ( x, y )
y(0.1)  y(0)  (0.1)f (0,1)
 1  (0.1)2  1  2(1)
 1  (0.1)(1)  0.9

208
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

b) Taylor’s method of order two

h2
y(x  h)  y(x)  hy ' (x)  y ' ' (x)
2!

y' ( t )  f ( t, y )  2 - e-4t  2y
f (0,1)  2  1  2  1

y' ' ( t )  f ' ( t, y )  4e-4t  2y'


f ' (0,1)  4  2( 1)  6

(0.1)2
y(0.1)  y(0)  (0.1)(-1)  (6)
2!
 1 - 0.1  0.03
 0.93
c) Taylor’s method of order 4

h2 h3 3 h4 4
y(x  h)  y(x)  hy ' (x)  y ' ' (x)  y (x)  y (x)
2! 3! 4!

y' ( t )  f ( t, y )  2 - e-4t  2y
f (0,1)  2  1  2  1

y' ' ( t )  f ' ( t, y )  4e-4t  2y'


f ' (0,1)  4  2( 1)  6

y' ' ' ( t )  f ' ' ( t, y )  16e-4t  2y' '


f ' ' (0,1)  16  2(6)  28

yiv ( t )  f ' ' ' ( t, y )  64e-4t  2y' ' '


f ' ' (0,1)  64  2( 28)  120

(0.1)2 (0.1)3 (0.1)4


y(0.1)  y(0)  (0.1)f(0,1)  f' (0,1)  f' ' (0,1)  f' ' ' (0,1)
2! 3! 4!
(0.1)2 (0.1)3 (0.1)4
 1  0.1(-1)  (6)  (-28)  (120)
2! 3! 4!
 0.9258

Exact value is

e4(0.1) e2(0.1)
y(0.1)  1  
2 2
 0.9258

209
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

Taylor's Taylor's
Exact Euler's
2nd 4th
Computation 0.9258 0.9 0.93 0.9258
Absolute
0 0.0258 0.0042 0
Error

It can be concluded that Taylor’s series method of order 4 gives a better


estimation compared to Euler’s and Taylor series method of order 2.

Warm up exercise
dy
Consider  x 2  6x 2 y with y(0) = -2.
dx
(i) Identify f ( x, y )

(ii) Determine f ' ( x, y) , f ' ' ( x, y) , and f ' ' ' ( x, y )

(iii) Identify x 0 , and y 0

(iv) Compute f ( x0, y0 ) , f ' ( x0, y0 ) , f ' ' ( x0, y0 ) and f ' ' ' ( x0, y0 ) .
(v) Approximate y(0.5).

Exercise 12

dy
1. Let  2xy with initial condition y0  2 and x0  2 . Approximate the
dx
solution at x1  1.1 by performing one iteration of Euler’s method with h = 0.1.

2. Consider the differential equation y'  x  y with initial condition y = 1 when


x = 0, do three iterations of Euler’s method with h = 0.1.

dy
3. Use Euler’s method to determine the solution to  y 2  t 2 with y(1) = 0,
dt
a) when t =2 for h = 0.2
b) when t = 5 for h = 0.5

210
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

dy y
4. Let  with initial condition y 0  4 and t 0  0 . Approximate the solution
dt 2
at t  0.4 by performing
a) two iterations of Taylor’s method of order two.
b) one iteration of Taylor’s method of order four.

5. Construct a table of x and y values for x [0, 1] with h = 0.1 for the initial value
1
problem y  , y(0) = 2 using:
xy
a) Euler’s method
b) Taylor’s method of order 2

dy
6. Solve the differential equation  x  y  xy with initial condition y(0) = 0
dx
to approximate y(0. 1), y(0.2), y(0.3) and y(0.5) by
a) Euler’s method.
b) Taylor’s method of order 2.
c) Taylor’s method of order 3.
d) Taylor’s method of order 4.

211
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

#6

i x h y f(x,y) Euler's
0 0 0.1 0 0 0
1 0.1 0.1 0 0.1 0.01
2 0.2 0.1 0.01 0.212 0.0312
3 0.3 0.1 0.0312 0.3406 0.0653
4 0.5 0.2 0.0653 0.598 0.1849

212
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

DEFINITION 7.6.1 (Initial Value Problems) Let be a continuous function on

a The problem of finding a solution of

(7.6.2)

in a neighbourhood of (or an open interval containing ) is called an Initial Value


Problem, henceforth denoted by IVP.

213
Part 7 NUMERICAL SOLUTION OF ODE MAT 575

214

You might also like