You are on page 1of 33

ECEG-2112 : Computational Methods

Lecture 9
Ordinary Differential Equations (ODEs)

Taylor Series Methods

Read 25.1-25.4, 26-2, 27-1

1
Learning Objectives
 Derive Euler formula using the Taylor
series expansion.
 Solve the first order ODEs using Euler
method.
 Assess the error level when using Euler
method.
 Appreciate different types of errors in the
numerical solution of ODEs.
 Improve Euler method using higher-order
Taylor Series.
2
Taylor Series Method
The problem to be solved is a first order ODE:

dy ( x)
 f ( x, y ), y ( x0 )  y0
dx
Estimates of the solution at different base points:

y ( x0  h), y ( x0  2h), y ( x0  3h), ....


are computed using the truncated Taylor series
expansions.

3
Taylor Series Expansion
Truncated Taylor Series Expansion
n
h k  d k y 

y ( x0  h)  
k !  dx k

k 0
 x  x0 , y  y 0 

dy h2 d 2 y hn d n y
 y ( x0 )  h  2
 ... 
dx x  x0 ,
y y
2! dx x  x0 , n! dx n x  x0 ,
0 y  y0 y  y0

The nth order Taylor series method uses the


nth order Truncated Taylor series expansion.

4
Euler Method
 First order Taylor series method is
known as Euler Method.

 Only the constant term and linear term


are used in the Euler method and
sometimes referred as Tangent Method.

 The error due to the use of the truncated


Taylor series is of order O(h2).
5
First Order Taylor Series Method
(Euler Method)
dy
y ( x0  h)  y ( x0 )  h  O(h 2 )
dx x  x0 ,
y  y0

Notation :
xn  x0  nh, yn  y ( xn ),
dy
 f ( xi , yi )
dx x  xi ,
y  yi

Euler Method
yi 1  yi  h f ( xi , yi )
6
Euler Method
Problem :
Given the first order ODE : y ( x)  f ( x, y )
with the initial condition : y0  y ( x0 )
Determine : yi  y ( x0  ih) for i  1,2,...

Euler Method :
y0  y ( x0 )
yi 1  yi  h f ( xi , yi ) for i  1,2,...

7
Interpretation of Euler Method
y2

y1

y0

x0 x1 x2 x

8
Interpretation of Euler Method

Slope=f(x0,y0)
y1
y1=y0+hf(x0,y0)
hf(x0,y0)
y0

x0 x1 x2 x
h

9
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

10
Example 1
Use Euler method to solve the ODE:

dy
 1 x ,
2
y (1)  4
dx
to determine y(1.01), y(1.02) and y(1.03).

11
Example 1
f ( x, y )  1  x , 2
x0  1, y0  4 , h  0.01
Euler Method
yi 1  yi  h f ( xi , yi )

Step1 : y1  y0  h f ( x0 , y0 )  4  0.01(1  (1) 2 )  3.98


 
Step 2 : y2  y1  h f ( x1 , y1 )  3.98  0.01 1  1.01  3.9598
2

Step3 : 
y3  y2  h f ( x2 , y2 )  3.9598  0.01 1  1.02  3.9394
2

12
Example 1
f ( x, y )  1  x ,
2
x0  1, y0  4 , h  0.01
Summary of the result:
i xi yi
0 1.00 -4.00
1 1.01 -3.98
2 1.02 -3.9595
3 1.03 -3.9394

13
Example 1
f ( x, y )  1  x ,
2
x0  1, y0  4 , h  0.01
Comparison with true value:
i xi yi True value of yi
0 1.00 -4.00 -4.00
1 1.01 -3.98 -3.97990
2 1.02 -3.9595 -3.95959
3 1.03 -3.9394 -3.93909

14
Example 1
f ( x, y )  1  x ,
2
x0  1, y0  4 , h  0.01

A graph of the
solution of the
ODE for
1<x<2

15
Types of Errors
 Local truncation error:
Error due to the use of truncated Taylor
series to compute x(t+h) in one step.
 Global Truncation error:
Accumulated truncation over many steps.
 Round off error:
Error due to finite number of bits used in
representation of numbers. This error could
be accumulated and magnified in
succeeding steps.

16
Example 2: Euler’s Method (1 of 3)

 For the initial value problem


y  9.8  0.2 y, y (0)  0
we can use Euler’s method with h = 0.1 to approximate the
solution at t = 0.1, 0.2, 0.3, 0.4, as shown below.
y1  y0  f 0  h  0  9.8(0.1)  .98
y2  y1  f1  h  .98  9.8  0.2 .98(0.1)  1.94
y3  y2  f 2  h  1.94  9.8  0.2 1.94(0.1)  2.88
y4  y3  f 3  h  2.88  9.8  0.2 2.88(0.1)  3.80
Example 2: Exact Solution (2 of 3)

 We can find the exact solution to our IVP,


y  9.8  0.2 y, y (0)  0
y  0.2 y  49
dy
 0.2dt
y  49
ln y  49  0.2t  C
y  49  ke 0.2t , k  eC
y (0)  1  k  49

 y  49 1  e 0.2t 
Example 2: Error Analysis (3 of 3)

 From table below, we see that the errors are small.


This is most likely due to round-off error and the fact
that the exact solution is approximately linear on [0,
0.4]. Note:
yexact  yapprox
Percent Relative Error  100
yexact

t Exact y Approx y Error % Rel Error


0.00 0 0.00 0.00 0.00
0.10 0.97 0.98 -0.01 -1.03
0.20 1.92 1.94 -0.02 -1.04
0.30 2.85 2.88 -0.03 -1.05
0.40 3.77 3.8 -0.03 -0.80
Example 3: Euler’s Method (1 of 3)

 For the initial value problem


y  4  t  2 y, y (0)  1
we can use Euler’s method with h = 0.1 to approximate
the solution at t = 1, 2, 3, and 4, as shown below.
y1  y0  f 0  h  1  4  0  (2)(1) (0.1)  1.6
y2  y1  f1  h  1.6  4  0.1  (2)(1.6) (0.1)  2.31
y3  y2  f 2  h  2.31  4  0.2  (2)(2.31) (0.1)  3.15
y4  y3  f 3  h  3.15  4  0.3  (2)(3.15) (0.1)  4.15

 Exact solution:
7 1 11 2t
y  t e
4 2 4
Example 3: Error Analysis (2 of 3)

 The first ten Euler approxs are given in table below on


left. A table of approximations for t = 0, 1, 2, 3 is given
on right. See text for numerical results with h = 0.05,
0.025, 0.01.
 The errors are small initially, but quickly reach an
unacceptable level. This suggests a nonlinear solution.
t Exact y Approx y Error % Rel Error t Exact y Approx y Error % Rel Error
0.00 1.00 1.00 0.00 0.00 0.00 1.00 1.00 0.00 0.00
0.10 1.66 1.60 0.06 3.55 1.00 19.07 15.78 3.29 17.27
0.20 2.45 2.31 0.14 5.81 2.00 149.39 104.68 44.72 29.93
0.30 3.41 3.15 0.26 7.59 3.00 1109.18 652.53 456.64 41.17
0.40 4.57 4.15 0.42 9.14 4.00 8197.88 4042.12 4155.76 50.69
0.50 5.98 5.34 0.63 10.58
0.60 7.68 6.76 0.92 11.96 Exact Solution :
0.70 9.75 8.45 1.30 13.31
0.80 12.27 10.47 1.80 14.64 7 1 11
0.90 15.34 12.89 2.45 15.96 y    t  e 2t
1.00 19.07 15.78 3.29 17.27 4 2 4
Second Order Taylor Series Methods
dy ( x)
Given  f ( y, x), y ( x0 )  y0
dx
Second order Taylor Series method
2 2
dy h d y
yi 1  yi  h  2
 O(h )
3

dx 2! dx
2
d y
2
needs to be derived analytically.
dx
22
Third Order Taylor Series Methods
dy ( x)
Given  f ( y, x), y ( x0 )  y0
dx
Third order Taylor Series method
2 2 3 3
dy h d y h d y
yi 1  yi  h  2
 3
 O(h )
4

dx 2! dx 3! dx
2 3
d y d y
2
and 3 need to be derived analytically.
dx dx
23
High Order Taylor Series Methods
dy ( x)
Given  f ( y, x), y ( x0 )  y0
dx
th
n order Taylor Series method
dy h 2 d 2 y hn d n y n 1
yi 1  yi  h  2
 ....  n
 O(h )
dx 2! dx n! dx
2 3 n
d y d y d y
2
, 3 ,....., n need to be derived analytically.
dx dx dx

24
Higher Order Taylor Series Methods
 High order Taylor series methods are
more accurate than Euler method.

 But, the 2nd, 3rd, and higher order


derivatives need to be derived analytically
which may not be easy.

25
Example 4
Second order Taylor Series Method

Use Second order Taylor Series method to solve :


dx
 2 x  t  1, x(0)  1,
2
use h  0.01
dt

2
d x(t )
What is : 2
?
dt

26
Example 4
Use Second order Taylor Series method to solve :
dx
 2 x  t  1, x(0)  1,
2
use h  0.01
dt
dx
 1 2x2  t
dt
d 2 x(t ) dx
2
 0  4 x  1   4 x (1  2 x 2
 t ) 1
dt dt
2
h
xi 1  xi  h(1  2 xi  ti )  (  1  4 xi (1  2 xi  ti ))
2 2

2
27
Example 2
f (t , x)  1  2 x 2  t , t0  0, x0  1, h  0.01
2
h
xi 1  xi  h(1  2 xi  ti )  (  1  4 xi (1  2 xi  ti ))
2 2

2
Step 1 :

x1  1  0.01(1  2(1) 2
 0) 
0.01
2
(1  4(1)(1  2  0))  0.9901
2
Step 2 :

x2  0.9901  0.01(1  2(0.9901) 2


 0.01) 
0.01
2
(1  4(0.9901)(1  2(0.9901) 2  0.01))  0.9807
2
Step 3 :
x3  0.9716

28
Example 4
f (t , x)  1  2 x 2  t , t0  0, x0  1, h  0.01
Summary of the results:
i ti xi
0 0.00 1
1 0.01 0.9901
2 0.02 0.9807
3 0.03 0.9716

29
Programming Euler Method
Write a MATLAB program to implement
Euler method to solve:

dv
 1  2v  t.
2
v(0)  1
dt

for ti  0.01i, i  1,2,...,100


30
Programming Euler Method
f=inline('1-2*v^2-t','t','v')
h=0.01
t=0
v=1
T(1)=t;
V(1)=v;
for i=1:100
v=v+h*f(t,v)
t=t+h;
T(i+1)=t;
V(i+1)=v;
end

31
Programming Euler Method
f=inline('1-2*v^2-t','t','v')
h=0.01 Definition of the ODE
t=0
Initial condition
v=1
T(1)=t;
V(1)=v;
for i=1:100 Main loop
v=v+h*f(t,v)
t=t+h; Euler method
T(i+1)=t;
V(i+1)=v; Storing information
end

32
Programming Euler Method
Plot of the
solution

plot(T,V)

33

You might also like