You are on page 1of 14

Numerical Methods for Civil Engineers

Lecture 10

Ordinary Differential Equations


- Basic Ideas
- Eulers Method
- Higher Order One-step Methods
- Predictor-Corrector Approach
- Runge-Kutta Methods
- Adaptive Stepsize Algorithms
Mongkol JIRAVACHARADET

SURANAREE

INSTITUTE OF ENGINEERING

UNIVERSITY OF TECHNOLOGY

SCHOOL OF CIVIL ENGINEERING

Ordinary Differential Equations


Rate of change of one variable with respect to another.

dy
= f (t , y )
dt
where t = independent variable
y = dependent variable

Initial Condition

y(t0) = y0

Example: Mass-spring system

m
c

d 2x
dx
ma + cv + kx = m
+ c + kx = 0
dt
dt

EULERS METHOD
(t t0 ) 2
y (t0 ) +L
From Taylor series: y (t ) = y (t0 ) + (t t0 ) y (t0 ) +
2!
Retaining only first derivative:

y (t ) = y0 + h f (t0 , y0 )

where h = (t - t0), y0 = y(t0), and f(t0, y0) = y(t0)


Predict
y
error
True

y1 = y0 + h f (t0 , y0 )

y2 = y1 + h f (t1 , y1 )

h
t0

t1

yi = yi 1 + h f (ti 1 , yi 1 )

New value = Old value + slope x step size

Example: Manual Calculation with Eulers Method

dy
= t 2 y, y (0) = 1
dt
1
Exact solution: y = ( 2t 1 + 5e 2t )
4
Set h = 0.2
ti

f (ti-1, yi-1)

0.0
0.2
0.4
0.6

NA
0-2(1) = -2.0
0.2-2(0.6) = -1.0
0.4-2(0.4) = -0.4

Euler
yi=yi-1+h f (ti-1, yi-1)
initial cond. = 1.0
1.0+0.2(-2.0) = 0.6
0.6+0.2(-1.0) = 0.4
0.4+0.2(-0.4) = 0.32

Exact

Error

1.0000
0.6879
0.5117
0.4265

0
-0.0879
-0.1117
-0.1065

Implementing Eulers Method


odeEuler.m
function [t,y] = odeEuler(diffeq,tn,h,y0)
% Input:
diffeq = (string) name of m-file that
%
evaluate right hand side of ODE
%
tn = stopping value of independent variable
%
h = stepsize
%
y0 = initial condition at t=0
t = (0:h:tn);
n = length(t);
y = y0*ones(n,1);
for i=2:n
y(i) = y(i-1)+h*feval(diffeq,t(i-1),y(i-1));
end

Example:

dy
= t 2 y,
dt

y (0) = 1, h = 0.2

rhs1.m
function dydt = rhs1(t,y)
dydt = t-2*y;
>> [t,y] = odeEuler(rhs1,0.6,0.2,1.0)
t =
0
0.2000
0.4000
0.6000
y =
1.0000
0.6000
0.4000
0.3200

Exact Solution :
>> y0 = (1/4)*(2*t-ones(size(t))+5*exp(-2*t))
y0 =
1.0000
0.6879
0.5117
0.4265

>> t0=0:0.01:0.6;
>> y0 = (1/4)*(2*t0-ones(size(t0))+5*exp(-2*t0));
>> plot(t0,y0,t,y)

Improvements of Eulers Method


Heuns Method: estimate slope from derivatives at the beginning
and at the end of the interval.
y

Euler:
y0i = yi-1 + hk1

slope:
k2 = f(ti, y0i)

slope:
k1 = f(ti-1, yi-1)

h
ti-1

ti

k1 + k 2
f (t i 1, y i 1 ) + f (t i , y i0 )
Average slope =
=
2
2

k + k2
y i = y i 1 + h 1

2
Use slope:
(k1 + k2)/2

h
ti-1

ti

Heuns Method
dy
= f (t , y )
dt

k1 = f (ti 1 , yi 1 )

y i0

k2 = f (ti 1 + h, yi 1 + hk1 )
Euler

k +k
yi = yi 1 + h 1 2
2

Heun
True
yi-1
h
ti-1

ti

Predictor-Corrector Approach
Heuns Method:

k1 = f (ti 1 , yi 1 )
k2 = f (ti 1 + h, yi 1 + hk1 )
yi = yi 1 + h

Eulers Method:

yi = yi 1 + h f (ti 1 , yi 1 )

k1 + k2
2

Predictor: yi0 = yi 1 + h f (ti 1 , yi 1 )


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

Iterating the Corrector of Heuns Method


To improve the estimation

yi

f (ti 1 , yi 1 ) + f (ti , y )
yi 1 + h
2
0
i

Example: Use Heuns method to integrate

y = 4e 0.8 x 0.5 y ,

y (0) = 2

From x = 0 to 4, step size = 1


Analytical solution:

Predictor,

y=

4 0.8 x 0.5 x
e e
+ 2e 0.5 x
(
)
1.3

yi0 = yi 1 + h f (ti 1 , yi 1 )
y10 = 2 + 1 (4e 0 0.5(2)) = 5

f (ti 1 , yi 1 ) + f (ti , yi0 )


Corrector, yi = yi 1 + h
2
(4e 0 0.5(2)) + (4e 0.8(1) 0.5(5))
y1 = 2 + 1
= 6.7011
2

True value: y =

4 0.8(1) 0.5(1)
e
e
+ 2e 0.5(1) = 6.1946
(
)
1.3

Relative error, Et =

nd

rd

Corrector,

3 Corrector,

6.1946 6.7011
100% = 8.18%
6.1946

(4e 0 0.5(2)) + (4e 0.8(1) 0.5(6.7011))


y1 = 2 + 1
2
= 6.2758, Et = 1.31%
(4e 0 0.5(2)) + (4e 0.8(1) 0.5(6.2758))
y1 = 2 + 1
2
= 6.3821, Et = 3.03%

Error may increases for large step sizes.

Iteration using up-arrow in MATLAB


>> y = 2+1*(4*exp(0)-0.5*2)

% Predictor of y1

y = 5
>> y = 2+(1/2)*((4*exp(0)-0.5*2)+(4*exp(0.8*1)-0.5*y))
% 1st Corrector of y1

y = 6.7011
Press

and

Enter

y = 6.7011

% 2nd Corrector of y1

y = 6.2758

% 3rd Corrector of y1

y = 6.3821

% 4th Corrector of y1

y = 6.3555

% 5th Corrector of y1

y = 6.3609

% until no change

MATLABs Implementation
func1.m
function dydx = func1(x,y)
dydx = 4*exp(0.8*x)-0.5*y;

odeHeun.m
function [x,y] = odeHeun(diffeq,xn,h,y0,iter)
% Input:
iter = number of corrector iterations
x = (0:h:xn);
n = length(x);
y = y0*ones(n,1);
for i=2:n
y(i) = y(i-1)+h*feval(diffeq,x(i-1),y(i-1));
for j=1:iter
y(i) = y(i-1)+h*(feval(diffeq,x(i-1),y(i-1)) ...
+ feval(diffeq,x(i),y(i)))/2;
end
end

For x = 0 to 4, step size = 1, y(0) = 2, Iteration = 15


>> [x,y] = odeHeun(func1,4,1,2,15)
x =
0
1
2
3
4
y =
2.0000
6.3609
15.3022
34.7433
77.7351

y_true =
2.0000
6.1946
14.8439
33.6772
75.3390

Et =
0.00
2.68
3.09
3.17
3.18

Compare with Eulers Method:


>> [x,y] = odeEuler(func1,4,1,2)
x =
0
1
2
3
4
y =
2.0000
5.0000
11.4022
25.5132
56.8493

Et =
0.00
19.28
23.19
24.24
24.54

Comparison of True Solution with Eulers and Heuns Method


>> xtrue = (0:0.1:4);
>> ytrue = (4/1.3)*(exp(0.8*xtrue)-exp(-0.5*xtrue))...
+ 2*exp(-0.5*xtrue);
>> [xeuler,yeuler] = odeEuler(func1,4,1,2);
>> [xheun,yheun] = odeHeun(func1,4,1,2,15);
>> plot(xtrue,ytrue,xeuler,yeuler,o,xheun,yheun,+)
80

60

True
Euler
Heun 15

40
20
0
0

2
x

RUNGE-KUTTA (RK) METHODS


General Formula using Weighted Average of slopes
m

yi = yi 1 + l kl
l =1

For Heuns method 1 = 2 = 0.5


m

The weights must satisfy

l =1

=1

Second-Order RK (Ralstons) Method:

2
1
yi = yi 1 + h k1 + k 2
3
3
k1 = f ( xi 1 , yi 1 )
3

k2 = f xi 1 + h,
4

yi 1 + k1h
4

Third-Order Runge-Kutta Methods


yi = yi 1 +

h
( k1 + 4k2 + k3 )
6

where

k1 = f ( xi 1 , yi 1 )
1
1

k2 = f xi 1 + h, yi 1 + k1h
2
2

k3 = f ( xi 1 + h, yi 1 k1h + 2k 2 h )

Fouth-Order Runge-Kutta Methods


The most popular RK methods
Classical 4th-order RK: yi = yi 1 +

h
( k1 + 2k2 + 2k3 + k4 )
6

where

k1 = f ( xi 1 , yi 1 )
1
1

k2 = f xi 1 + h, yi 1 + k1h
2
2

1
1

k3 = f xi 1 + h, yi 1 + k2 h
2
2

k4 = f ( xi 1 + h, yi 1 + k3 h )

Example: Use 4th-order RK method to integrate

y = 4e 0.8 x 0.5 y ,

y (0) = 2

From x = 0 to 4, step size = 1


Step 1: x0 = 0, y0 = 2

k1 = 4e0 0.5(2) = 3
1
k2 = 4e0.8(0.5) 0.5(2 + 3 1) = 4.2173
2
1
k3 = 4e0.8(0.5) 0.5(2 + 4.2173 1) = 3.9130
2
k4 = 4e0.8(1) 0.5(2 + 3.9130 1) = 5.9457

1
(3 + 2 4.2173 + 2 3.9130 + 5.9457 )
6
= 6.2011

y1 = 2 +

True value: y =

4 0.8(1) 0.5(1)
e
e
+ 2e 0.5(1) = 6.1946
(
)
1.3

Relative error, Et =

6.1946 6.2010
100% = 0.1038%
6.1946

MATLABs Implementation
odeRK4.m
function [x,y] = odeRK4(diffeq,xn,h,y0)
x = (0:h:xn);
n = length(x);
y = y0*ones(n,1);
for i=2:n
k1 = feval(diffeq,x(i-1),y(i-1));
k2 = feval(diffeq,x(i-1)+h/2,y(i-1)+k1*h/2);
k3 = feval(diffeq,x(i-1)+h/2,y(i-1)+k2*h/2);
k4 = feval(diffeq,x(i-1)+h,y(i-1)+k3*h);
y(i) = y(i-1)+h/6*(k1+2*k2+2*k3+k4);
end

For x = 0 to 4, step size = 1, y(0) = 2, Iteration = 15


>> [x,y] = odeRK4(func1,4,1,2)
x =
0
1
2
3
4
y =
2.0000
6.2010
14.8625
33.7213
75.4392

y_true =
2.0000
6.1946
14.8439
33.6772
75.3390

Et(%) =
0.00
0.1038
0.1250
0.1309
0.1328

Adaptive Stepsize Algorithms


MATLABs ode23 and ode45 Routines
ode23 use second- and third-order RK simultaneously
ode45 use fourth- and fifth-order RK simultaneously
Example: y = 4e 0.8 x 0.5 y ,

y (0) = 2

func1.m

For x = 0 to 4, y(0) = 2
>>
>>
>>
>>

x0 = 0; xn = 4; y0 = 2;
[x45,y45] = ode45(func1,[x0,xn],y0);
xtrue = (0:0.1:4);
ytrue = (4/1.3)*(exp(0.8*xtrue)...
- exp(-0.5*xtrue))+ 2*exp(-0.5*xtrue);
>> plot(xtrue,ytrue,x45,y45)

You might also like