You are on page 1of 36

Numerical Methods

Numerical Solution of ODEs


Topic: Euler method
Ordinary Differential Equations
• Equations which are composed of an unknown function
and its derivatives are called Ordinary differential
equations (ODE).

• Differential equations play a fundamental role in


engineering because many physical phenomena are
best formulated mathematically in terms of their rate
of change.
• For example, acceleration on a falling body is given by
ODE:
dv c v- dependent variable
g v
dt m t- independent variable
2
What is an ODE?
First order ODE’s relate the first derivative of a function
(time rate of change) with the function itself. The ODE is
first order if only the first derivative of the function is
included.

dy (t ) at
 ay (t ) y(t )  y(0)e
dt
Exponential growth or decay is governed by this
simple ODE. Put some t for a = -1.0 to check.
“Solving” an ODE
Answer the question:
what function can we construct such that it has
the relationship to its derivative that the ODE
expresses?
Solving an ODE by hand requires lots of mathematical
intuition and experience.
ODE is mathematical model
An engineering process can often be described using
ODEs; for example

• Radioactive decay
• Chemical processes
• Mechanical processes
• Controls of almost anything
• Population dynamics
• Diffusion of almost anything
• Transient electrical circuits
ODE’s and Linear Systems

• Linear systems express • ODE’s express the


the relationship between relationship between
different variables. different functions and
• Linear systems usually their derivatives.
represent static systems,
that is not varying in • ODE’s often represent
time. dynamic systems that
vary in time.
How to solve simple first order ODE

dy (t ) Rate of change of y proportional


 a y (t ) to y itself.
dt
dy (t ) dy (t ) Rearrange terms and
 a dt     a dt integrate both sides.
y (t ) y (t )
ln( y)  A  a t  ln( y0 )  A Assign integration const
using initial condition.
 y 
ln( y )  ln( y0 )  a t  ln    a t
 y0 
at
y (t )  y0 e Use logarithm rules of
addition to get final result.
Plot y(t) for positive and negative a

y(t)=10e2t y(t)=10e-2t

The y(t) either grows or decays very rapidly as t increases.


This is exponential growth or decay. A constant value of y(t)
demands that a  0.
A model for population dynamics
  birth rate (year -1 )
  death rate (year -1 )
a     net rate of gain or loss of population
y (t )  population at time t
dy (t )
  y (t )   y (t )  (    ) y (t )  a y (t )
dt
y (t )  y0 ea t  y0 e(   )t If  then the population
stays constant at y0. Otherwise
it grows or decays according
to this difference.
A different model of population dynamics

dy (t ) Birth-death rate term and


 a y (t )   y (t )
2
new overcrowding term.
dt
Famous ODE: Simple pendulum

d 2
ML 2   MG sin  
dt L
d 2 G
2
  sin 
dt L M

Second order non-linear ODE, G


because we have second derivative.

To solve analytically, make the approximation sinq~q.


This makes the ODE linear for small amplitude oscillations.
Ordinary Differential Equations
• When a function involves one dependent variable, the
equation is called an ordinary differential equation (or
ODE). A partial differential equation (or PDE) involves
two or more independent variables.

• Differential equations are also classified as to their


order.
• A first order equation includes a first derivative as its highest
derivative.
• A second order equation includes a second derivative.
• Higher order equations can be reduced to a system of
first order equations, by redefining a variable.
Euler’s Method
Leonhard Euler made a huge number of
contributions to mathematics, almost half after
he was totally blind.

(When this portrait was made he had already


lost most of the sight in his right eye.)

It was Euler who originated the following Leonhard Euler


1707 - 1783
notations:

f  x  (function notation) e (base of natural log) i  1 


 (summation) y (finite change)  (pi)
Euler’s Method for ODEs
 f x, y , y 0   y0
dy
dx y

y y  y0 True
slope   1 value
x x1  x0 y1,
Predicted

slope  f x0 , y0 
. value
Φ
( x0 , y0)

y1  y0  f x0 , y0 x1  x0 
Step size, h
y1  y0  f x0 , y0 h x

y2  y1  f x1 , y1 h
Figure 1. Graphical interpretation of the
first step of Euler’s method
Euler’s Method for ODEs

y1  y0  f x0 , y0 h y

y2  y1  f x1 , y1 h True Value

y3  y2  f x2 , y2 h
yi+1,
: yi Predicted
: Φ value
h
yi 1  yi  f xi , yi h Step size
xi xi+1 x

h  xi 1  xi
Figure 2. General graphical interpretation of Euler’s method
Example 1 on Euler method
There are many differential equations that can not be solved.
We can still find an approximate solution.

We will practice with an easy one that can be solved.


dy
 2x Initial value: y0  1
dx

Exact Solution:
dy  2 x dx y  x2  C 1 0C

y  x2  1
Example 1 on Euler method
dy
 f ( x, y )  2 x, y (0)  1
dx
First point: x1  x0  h  0  0.5  0.5
y1  y0  f  x0 , y0 h  y0  2 x0 h
 1.0  ( 2  0)  0.5  1.0  0.0  1
exact  (0) 2  1  1
error  0.

Second point: x2  x1  h  0.5  0.5  1.0


y2  y1  f  x1 , y1 h  y1  2 x1 h
 1.0  (2  0.5)  0.5  1.5
exact  x12  1  (0.5) 2  1  1.25
Er  (1.25  1.5) / 1.25  100  20%
Example 1 on Euler method

n xn yn f h*f yn+1 Exact

0 0 1 0 0 1 1

1 0.5 1 1 0.5 1.5 1.25

2 1 1.5 2 1 2.5 2.00

3 1.5 2.5 3 1.5 4.0 3.25

4 2.0 4.0 4.0 2.0 6.0 5.


Example 1 on Euler method
As shown in
figure, the 6
difference Euler method
5 Exact solution
between exact
solution and 4
values from
y(x)

3
Euler method
grows as we 2
move forward
in x. 1

0.0 0.5 1.0 1.5 2.0


x
Example 2 :
Let us consider a differential equation as

dy
 f ( x, y )  x  y , y (0)  1
dx
is rewritten as
dy
 x  y, y 0   1 f x, y   x  y
dx

x1  x0  h  0  0.1  0.1
y1  y0  f  x0 , y0 h  y0   x0  y0 h
 1.0  (0  1.0)  0.1  1.0  0.1  0.9

x2  x1  h  0.1  0.1  0.2


y2  y1  f x1 , y1 h  y1  x1  y1 h
 0.9  (0.1  0.9)  0.1  0.9  0.08  0.82
Example 2 :
dy
(continued) -  f ( x, y )  x  y , y (0)  1
dx

x4  x0  4h  0.0  0.4  0.4


y4  y3  f x3 , y3 h  y3  x3  y3 h
 0.758  (0.3  0.758)  0.1  0.758  0.0458  0.7122

x5  x0  5h  0.0  0.5  0.5


y5  y4  f x4 , y4 h  y4  x4  y4 h
 0.7122  (0.4  0.7122)  0.1  0.7122  0.03122
 0.68098
Example 2 :
dy
(continued) -  f ( x, y )  x  y , y (0)  1
dx
Actual solution is : y  2 exp(  x)  x  1

A comparison table for estimated values and the true values and error

x 0 0.1 0.2 0.3 0.4 0.5

y (estimated) 1 0.9 0.82 0.758 0.7122 0.6810

y (true) 1 0.9097 0.8375 0.7816 0.7406 0.7131

Error = true 0 0.0097 0.0175 0.0236 0.0284 0.0321


- estimated
Example 2 :
dy
(continued) -  f ( x, y )  x  y , y (0)  1
dx
Actual solution is : 1.05
Euler method for ODEs
y  2 exp(  x)  x  1 1.00
estimated values
0.95 true values

A comparison graph for 0.90


estimated values and the true
y(x) 0.85
values.
0.80
It shows that the difference
0.75
between actual value and the
estimated value continues to 0.70
get larger as the x increases.
0.65
0.0 0.1 0.2 0.3 0.4 0.5
x
Example 3 :
Use Euler method to solve the following Ordinary differential
equation:
dy
 f ( x, y )  x  2 y , y (0)  1.0
dx
The exact solution is : y  2 exp(  x)  x  1

First point:
x1  x0  h  0  0.1  0.1
y1  y0  f x0 , y0 h  y0   x0  2 y0 h
 1.0  (0  2.0)  0.1  1.0  0.2  0.8

Second point: x2  x1  h  0.1  0.1  0.2


y2  y1  f x1 , y1 h  y1  x1  2 y1 h
 0.8  (0.1  1.6)  0.1  0.8  0.15  0.65
Example 3 :
dy
Example (continued) -  x  2 y, y (0)  1
dx
Third iteration: x3  x0  3h  0  0.3  0.3
y3  y2  f x2 , y2 h  y2   x2  2 y2 h
 0.65  (0.2  2  0.65)  0.1  0.54

Fourth iteration: x  x  4h  0.0  0.4  0.4


4 0

y4  y3  f x3 , y3 h  y3  x3  2 y3 h
 0.54  (0.3  2  0.54)  0.1  0.462
Fifth iteration:
x5  x0  5h  0.0  0.5  0.5
y5  y4  f x4 , y4 h  y4   x4  2 y4 h
 0.462  (0.4  2  0.462)  0.1  0.4096
Example 3 :
dy
Example (continued) -  x  2 y, y (0)  1
dx
Actual solution is : 5 x 1
y exp( 2 x )  
4 2 4
A comparison table for estimated values and the true values and error

x 0 0.1 0.2 0.3 0.4 0.5

y (estimated) 1 0.8 0.65 0.54 0.462 0.4096

y (true) 1 0.8234 0.6879 0.5860 0.5117 0.4598

Error = true 0 0.0234 0.0379 0.0460 0.0497 0.0502


- estimated
Example 3 :
dy
Example (continued) -  f ( x, y )  x  2 y , y (0)  1
dx
Actual solution is :
1.0 Euler method for ODEs
5 x 1
y  exp( 2 x )  
4 2 4 estimated values
0.9
true values

A comparison graph for 0.8


estimated values and the true 0.7
values. y(x)
0.6
It shows that the difference
0.5
between actual value and the
estimated value continues to 0.4
get larger as the x increases.
0.0 0.1 0.2 0.3 0.4 0.5
x
A computer program for Euler method
% A program that Uses euler method
% to solve ode: dy/dx = x - 2y when y(0) = 1.0
% Input: x0 = initial guess; h = step size
% n = number of iterations; default: n = 10;
% Output: x = estimate of y;
n = 10; x0 = 0.0; y0 = 1.0; h = 0.1;
x = x0; y = y0; % Initial Guess

fprintf(' x y-est y-true error\n');

for k=1:n
f = x - 2.0*y ;
y = y + f*h;
x = x + h;
yexact = 1.25*exp(-2.*x)+0.5*x-0.25;
error= yexact - y;
fprintf('%6.2f %12.6f %12.6f %12.6f\n', x,y,yexact,error);
end
Example 3 :
Results from computer program in MATLAB :

x y-est y-true error


0.10 0.800000 0.823413 0.023413
0.20 0.650000 0.687900 0.037900
0.30 0.540000 0.586015 0.046015
0.40 0.462000 0.511661 0.049661
0.50 0.409600 0.459849 0.050249
0.60 0.377680 0.426493 0.048813
0.70 0.362144 0.408246 0.046102
0.80 0.359715 0.402371 0.042655
0.90 0.367772 0.406624 0.038851
1.00 0.384218 0.419169 0.034951
>>
Review of the Euler Methods
• For Initial value problems
dy
 f ( x, y ) y ( x0 )  y0
dx
• Often impossible to generate analytical solution y(x)
• Instead compute approximate solution at equally spaced node
points

x1  x0  h x2  x0  2h x3  x0  3h 
y( x1 )  y1 y( x2 )  y2 y( x3 )  y3 
• h = step size
• Taylor series expansion
dy ( x) h 2 d 2 y ( x)
y ( x  h)  y ( x )  h  2

dx 2! dx
Forward Euler Method
• First-order Taylor series expansion
dy ( x)
y ( x  h)  y ( x )  h  y ( x)  hf ( x, y )
dx
• Iterative calculation
y1  y0  hf ( x0 , y0 ) y2  y1  hf ( x1 , y1 )
yn 1  yn  hf ( xn , yn ) n  0,1,2,
• Approximation error
dy ( x) h 2 d 2 y ( )
y ( x  h)  y ( x )  h  x   xh
dx 2! dx 2
• Local truncation error: O(h2)
• Global truncation error: O(h)  first-order method
Backward Euler Method
• Forward Euler
dy ( x) yn 1  yn
 f ( x, y )  f ( xn , y n ) yn 1  yn  hf ( xn , yn )
dx h
• Explicit formula for yn+1 (explicit method)

• Backward Euler
dy ( x) yn 1  yn
 f ( x, y )  f ( xn 1 , yn 1 ) yn 1  yn  hf ( xn 1 , yn 1 )
dx h

• Implicit formula for yn+1 (implicit method)


• Allows larger h values to be used with comparable
errors  more stable
• Generally preferred to forward Euler
Error Propagation
• Behavior of Euler algorithm is typical of all algorithms
for numerical solutions;
• Here Error grows at each step;
• We usually do not know this global error, but we would
like to control it;
• So first look at local error for Euler algorithm;
• Then discuss general relationship between local and
global error.
Use of 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 xa
3! dx xa

Look at one step from known initial condition,


a = x0, to x0 + h s o x – a = h

dy 1 d2y 2 1 d3y 3
y ( x 0  h)  y ( x 0 )  h 2
h  3
h  ....
dx 0 2! dx 0 3! dx 0

In ODE notation, dy/dx|0 = f(x0, y(x0))


Local and Global Error in Euler method
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
Euler Algorithm Truncation Error

• For the first step when we know f(x0,y(x0)), and we can see
the first term in the local truncation error.
• Local truncation error for Euler algorithm is of second
order and proportional to h2 .
• It can be show that when local error is of order n, the
method has a global error of order n-1.
Local and Global Error in Euler method
• The Euler method has second order local error;
• Then it will have first order global error;

• A rough comparison of errors versus step size (h) is
given below:

Step size First step Final error


h = 0.1 5.17x10-3 1.25 x10-1
h = 0.01 5.02 x10-5 1.35 x10-2
h = 0.001 5.00 x10-7 1.36 x10-3

You might also like