You are on page 1of 15

ECC 3012

Engineering Mathematics II

ODE: Numerical method

Mohd Afandi P Mohammed


Department of Process and Food Engineering,
Faculty of Engineering, UPM
First-order differential equations
Introduction

 The most interesting situations in real life that we may want to


model, or represent quantitatively, are usually those in which the
variables change in time (e.g. biological, electrical or mechanical
systems). If the changes are continuous, the system can often be
represented with equations involving the derivatives of the
dependent variables. Such equations are called differential
equations.
 The main aim of a lot of modeling is to be able to write down a set
of differential equations (DEs) that describe the system being
studied as accurately as possible.
 Very few DEs can be solved analytically, so once again, numerical
methods are required.
Euler’s method

 To solve a first-order DE (strictly an ordinary—ODE) of the form


dy/dx = f (x, y), y(0) given.
 Euler’s method for solving this DE numerically consists of replacing dy/dx with
its Newton quotient, so that the DE becomes
y ( x  h)  y ( x )
 f ( x, y )
h
 After a rearrangement of terms,
y(x + h) = y(x) + hf (x, y).
 Suppose we want to integrate the DE over the interval x = a (a = 0 usually) to x =
b. By breaking this interval up into m steps of length h, so
m = (b − a)/h
Euler’s method

 if we define yi as y(xi) (the Euler estimate at the beginning of step


i), where xi =(i −1)h, then yi+1 = y(x + h), at the end of step i.
 After a relabel the terms,
y i+1 = yi + hf (xi , yi)
 where y1 =y(0). This notation enables us to generate a vector y
which we can then plot.
 This equation representing an update process.
Euler’s method

dy
1) Numerical function to solve:  f ( x, y ) Initial value
dx problem (IVP)
y  x0   y0

2) Using Euler’s method:

y0
h h
x0 x1 x2
Euler’s method

n yn+1
An
h An
y0 yn
h h h

x0 xn xn+1
x1 x2 xn

Euler’s equations
xn 1  xn  h yn 1  yn  h An
yn 1  yn  h An yn 1  yn  h An
An  f  xn , yn 
Euler’s method
Example 1
y  0  1
Given: dy  x 2  y 2 where:
dx h  0.1

What is: y  0.2  ?

We can solve this manually in table form


dy y  0  1 Euler’s equations
 x2  y 2 xn 1  xn  h
dx h  0.1
yn 1  yn  h An
An  f  xn , yn 
n xn yn An hAn
0 0 1 -1 -0.1
Step 1

x0 y0 x02  y02

n xn yn An hAn
0 0 1 -1 -0.1
Step 2
1 0.1 0.9 -0.8 -0.08

x1  x0  h y1  y0  h A0 x12  y12
Euler’s equations
xn 1  xn  h
yn 1  yn  h An
An  f  xn , yn 

n xn yn An hAn
0 0 1 -1 -0.1 Step 3
1 0.1 0.9 -0.8 -0.08
2 0.2 0.82

y  0.2  = 0.82 y2  y1  h A1
Example 2

The function x(t ) satisfies the differential equation

dy x  t

dx xt

And the IVP given as

x 1  2

Use Euler’s method to obtain an approximation to the value


of x(2) using a step size of h=0.1
Drawback of Euler’s method
Real solution
Convex
A4 Euler’s too low
A0
A3
Euler’s method solution
y  0
A1 A2

A2 A3 A4 Concave
A1 Euler’s method solution
Euler’s too high
A0
y  0
Real solution
Drawback of Euler’s method
To check the convex or concave:


y x y
2 2

y  2 x  2 yy

At (0,1)

y  0  1
y  0   1
y  0   2  0   2 1 1  2 (convex)
Drawback of Euler’s method

Error, e
Real solution
Smaller step

Larger step

Error, e, depends on step size, h

e c1h Euler is the first order method


(half the constant, half the error)
Runge Kutta method: theory

xn 1  xn  h
Bn  An  Bn 
Bn yn 1  yn  h  
 2 
An
Bn  f  xn 1 , yn 1 
h
yn 1  yn  hAn
2
e c2 h Second order method
RK2 method
k1  hf  xn , yn 
 1 1 
k2  hf  xn  h, yn  k1 
 2 2 
yn 1  yn  k2
Example 3
dy y  0  1
Given:  x 2  y 2 where:
dx h  0.1

What is: y  0.2  ?


Using RK2 method, and compare with Euler’s

You might also like