0% found this document useful (0 votes)
117 views

Lecture Module 8 Presentation

This document discusses a lecture module on ordinary differential equations. It covers several topics related to solving ordinary differential equations including Taylor series methods like Euler's method and Taylor's method. It provides examples of using these methods to solve basic ordinary differential equations and compares the numerical solutions to exact solutions. Functions for implementing Euler's method and Taylor's method in FreeMat are also presented.

Uploaded by

Wong Hang Sing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Lecture Module 8 Presentation

This document discusses a lecture module on ordinary differential equations. It covers several topics related to solving ordinary differential equations including Taylor series methods like Euler's method and Taylor's method. It provides examples of using these methods to solve basic ordinary differential equations and compares the numerical solutions to exact solutions. Functions for implementing Euler's method and Taylor's method in FreeMat are also presented.

Uploaded by

Wong Hang Sing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

BDA34003

ENGINEERING MATHEMATICS 4

Lecture Module 8: Ordinary Differential Equations

Waluyo Adi Siswanto


[email protected]
Learning Outcomes

 Students have the knowledge in solving


engineering ordinary differential equation
problems
 Students will be able to analyse the response
(output) of differential equation systems when
initial conditions (input) applied
 Students will be able to decide the appropriate
method to analyse the response of a system
when an input is applied

BDA34003 Engineering Mathematics IV 2


Waluyo Adi Siswanto ([email protected])
Topics

➢ Ordinary differential equation in Engineering


➢ Taylor series
➢ First order Taylor series (Euler's method)
➢ Second order Taylor series (Taylor's method)
➢ Runge Kutta Method
➢ Second order: Improved Euler, Heun, Modified Euler
➢ Fourth order: Clasical, Runge-Kutta-Merson
➢ Multi-step Method
➢ Boundary Value Problems

BDA34003 Engineering Mathematics IV 3


Waluyo Adi Siswanto ([email protected])
Ordinary Differential Equation
in Engineering
Problem examples in vibration:

Free vibration Forced vibration

k c k c

F (t)

x (t) m x (t) m

Equation of motion:

2 2
d x dx d x dx
m 2 + c + k x=0 m 2 + c + k x= F
dt dt dt dt
dv dv
or m + c v+ k x=0 or m + c v+ k x= F
dt dt
BDA34003 Engineering Mathematics IV 4
Waluyo Adi Siswanto ([email protected])
Ordinary Differential Equation
in Engineering

Initial value (What is that ?)

When you disturb the system (by hitting it with velocity v 0 ) k c


As a result, the mass will be vibrating v0

The equation of the system


function of t
dv
m + c v=−k x x (t) m
dt
x (t)

and you want to know the velocity


at t=5 (for example) t
v(t)
So you want to find (solve) the velocity v0
at certain time with an initial velocity (value)
(value
t
5

BDA34003 Engineering Mathematics IV 5


Waluyo Adi Siswanto ([email protected])
Taylor Series

Taylor series can be used to find the variable after initial value:

y ( x i+ 1 )= y( x i )+ h y ' ( x i ) Euler's method

2 3
h h
y ( x i+ 1 )= y( x i )+ h y ' ( x i )+ y ' ' ( xi )+ y ' ' ' ( x i ) + ...
2! 3!

2
h
y ( x i+ 1 )= y( x i )+ h y ' ( x i )+ y ' ' ( xi) Taylor's method
2

BDA34003 Engineering Mathematics IV 6


Waluyo Adi Siswanto ([email protected])
Euler and Taylor Methods

Taylor series can be used to find the variable after initial value:

v(t i+ 1 )=v (t i )+ h v ' (t i ) Euler's method

2 3
h h
v(t i+ 1 )=v (t i )+ h v ' (t i )+ v ' ' (t i )+ v ' ' ' (t i ) + ...
2! 3!

2
h
v(t i+ 1 )=v (t i )+ h v ' (t i )+ v ' ' (t i ) Taylor's method
2
When you are dealing with velocity
this does not exist (not applicable).
The highest order is v' , which is acceleration.

BDA34003 Engineering Mathematics IV 7


Waluyo Adi Siswanto ([email protected])
Euler function:

function [x,y]=Euler(func,a,b,y0,h)
% Solving y'=f(x,y) with initial condition y(0)=y0
% func = handle of function.
% for example --> func= @(x,y) (x+y)
% a = lowest of x
% b = highest of x
% h = incremental x
format long;
n = (b-a)/h ;
x =(a+h:h:b);
y(1) = y0 + h*feval(func,a,y0);
for i =2:n
y(i)=y(i-1)+h*feval(func,x(i-1),y(i-1));
end
x = [a x].';
y = [y0 y].';

BDA34003 Engineering Mathematics IV 8


Waluyo Adi Siswanto ([email protected])
Taylor function:
function [x,y]=Taylor(func1,func2,a,b,y0,h)
% Solving y'=f(x,y) with initial condition y(0)=y0
% func1 = handle the first derivative.
% func2 = handle the second derivative
% for example --> func1= @(x,y) (x+y)
% a = lowest of x
% b = highest of x
% h = incremental x
format long;
n = (b-a)/h;
hh = h^2/2;
x =(a+h:h:b);
y(1) = y0 + h*feval(func1,a,y0)+hh*feval(func2,a,y0);
for i =2:n
y(i)=y(i-1)+h*feval(func1,x(i-1),y(i-1))+hh*feval(func2,x(i-1),y(i-1));
end
x = [a x].';
y = [y0 y].';

BDA34003 Engineering Mathematics IV 9


Waluyo Adi Siswanto ([email protected])
Example 8-1

The variable of a system y depends on another variable x,


following a differential equation
2x
2y ' + 3y=e

Find the y when x=1. The initial condition at x=0, y=1

Use Euler's method with the increment h = 0.2

Check your calculation using Euler function in freemat

BDA34003 Engineering Mathematics IV 10


Waluyo Adi Siswanto ([email protected])
Equation for Euler's method y ( x i+ 1 )= y( x i )+ h y ' ( x i )

2x 2x
2y ' + 3y=e e −3y
y '=
2

step x y h y'

initial 0 0 1 −0.2
1 0.2 0.8000 −0.0908
2 0.4 0.7092 0.0098
3 0.6 0.7190 0.1163
4 0.8 0.8353 0.2447
5 1 1.0800

BDA34003 Engineering Mathematics IV 11


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 12
Waluyo Adi Siswanto ([email protected])
Example 8-2

Do Example 8-1 using Taylor's method

3x
1 2x 6 − 2
Compare the result with the exact solution y ( x)= e + e
7 7

Use the Taylor function in freemat to double check your calculation table

BDA34003 Engineering Mathematics IV 13


Waluyo Adi Siswanto ([email protected])
2
Equation for Taylor's method h
y ( x i+ 1 )= y( x i )+ h y ' ( x i )+ y ' ' ( xi )
2
2x 2x 2x
2y ' + 3y=e e −3y 2 e −3y '
y '= y' '=
2 2

2
step x y h y' (h /2) y ' ' exact

initial 0 0 1 −0.2 0.0500


1 0.2 0.8500 −0.0908 0.0457
2 0.4 0.7899 0.0098 0.0467
3 0.6 0.8222 0.1163 0.0536
4 0.8 0.9611 0.2447 0.0680
5 1 1.2361 1.2468

BDA34003 Engineering Mathematics IV 14


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 15
Waluyo Adi Siswanto ([email protected])
Runge-Kutta Method
Second Order RK :
Mid Point / Improved Euler Method

This is an improvement of Euler's method

Euler y ( x i+ 1 )= y( x i )+ h y ' ( x i )

Improved Euler f ( x i , y i )= y ' (x i )


(mid point)
y i+ 1= y i+ k 2

k 1 =h f ( x i , y i )

h k1
k 2=h f ( x i + , yi+ )
2 2

BDA34003 Engineering Mathematics IV 16


Waluyo Adi Siswanto ([email protected])
Second order Runge-Kutta mid point function:

function [x,y] = RK2(func,a,b,y0,h)


%
n = (b-a)/h;
x =(a+h:h:b);
k1 = h*feval(func,a,y0);
k2 = h*feval(func,a+(h/2),y0+(k1/2));
y(1)= y0 + k2;
for i=1 : n-1
k1 = h*feval(func,x(i),y(i));
k2 = h*feval(func,x(i)+(h/2),y(i)+(k1/2));
y(i+1) = y(i) +k2;
end
x = [a x].';
y = [y0 y].';

BDA34003 Engineering Mathematics IV 17


Waluyo Adi Siswanto ([email protected])
Example 8-3

Do Example 8-1 using Mid Point method

3x
1 2x 6 − 2
Compare the result with the exact solution y ( x)= e + e
7 7

Check your result using RK2 function

BDA34003 Engineering Mathematics IV 18


Waluyo Adi Siswanto ([email protected])
2x 2x
2x e −3y e −3y
2y ' + 3y=e y '= f ( x i , y i )=
2 2

k 1 =h f ( x i , y i ) k 1 =h (
e 2x −3y
2 )
h k1
k 2=h f ( x i + , y i + )
2 2

( ( )
)
e 2x −3y
(h )
2
e 2(x+ (h /2))−3( y+ )
k 2=h
2
2
k 2=
2 (
e
[ (
h 2( x+ (h /2))
−3 y+
h 2x
4 )])
( e −3y )

y i+ 1= y i+ k 2

BDA34003 Engineering Mathematics IV 19


Waluyo Adi Siswanto ([email protected])
step x y k1 k2 exact

0 0 1 −0.2 −0.1479
1 0.2 0.8521 −0.1064 −0.0575
2 0.4 0.7946 −0.0158 0.0358
3 0.6 0.8304 0.0829 0.144
4 0.8 0.9744 0.203 0.2822
5 1 1.2566 1.2468

BDA34003 Engineering Mathematics IV 20


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 21
Waluyo Adi Siswanto ([email protected])
Runge-Kutta Method
Second Order RK : Heun's Method

Heun's method
f ( x i , y i )= y ' (x i )

1 3
y i+ 1= y i+ k 1+ k 2
4 4

k 1 =h f ( x i , y i )

2h 2k 1
k 2=h f ( x i + , yi + )
3 3

BDA34003 Engineering Mathematics IV 22


Waluyo Adi Siswanto ([email protected])
Second order Runge-Kutta Heun function:

function [x,y] = RK2Heun(func,a,b,y0,h)


%
format long;
n = (b-a)/h;
x =(a+h:h:b);
k1 = h*feval(func,a,y0);
k2 = h*feval(func,a+(2*h/3),y0+(2*k1/3));
y(1)= y0 + k1/4 + 3*k2/4;
for i=1 : n-1
k1 = h*feval(func,x(i),y(i));
k2 = h*feval(func,x(i)+(2*h/3),y(i)+(2*k1/3));
y(i+1) = y(i) + k1/4 + 3*k2/4;
end
x = [a x].';
y = [y0 y].';

BDA34003 Engineering Mathematics IV 23


Waluyo Adi Siswanto ([email protected])
Runge-Kutta Method
Second Order RK : Modified Euler's Method

Modified Euler's method


f ( x i , y i )= y ' (x i )

1 1
y i+ 1= y i+ k 1+ k 2
2 2

k 1 =h f ( x i , y i )
k 2=h f ( x i + h , y i + k 1 )

BDA34003 Engineering Mathematics IV 24


Waluyo Adi Siswanto ([email protected])
Second order Runge-Kutta Modified Euler function:

function [x,y] = RK2ModEuler(func,a,b,y0,h)


%
format long;
n = (b-a)/h;
x =(a+h:h:b);
k1 = h*feval(func,a,y0);
k2 = h*feval(func,a+h,y0+k1);
y(1)= y0 + k1/2 + k2/2;
for i=1 : n-1
k1 = h*feval(func,x(i),y(i));
k2 = h*feval(func,x(i)+h,y(i)+k1);
y(i+1) = y(i) + k1/2 + k2/2;
end
x = [a x].';
y = [y0 y].';

BDA34003 Engineering Mathematics IV 25


Waluyo Adi Siswanto ([email protected])
Example 8-4

Do Example 8-1 using Heun's method

3x
1 2x 6 − 2
Compare the result with the exact solution y ( x)= e + e
7 7

Check your results using RK2Heun function

BDA34003 Engineering Mathematics IV 26


Waluyo Adi Siswanto ([email protected])
2x 2x
2x e −3y e −3y
2y ' + 3y=e y '= f ( x i , y i )=
2 2

k 1 =h f ( x i , y i ) k 1 =h (
e 2x −3y
2 )
2h 2k 1
k 2=h f ( x i + , yi+ )
3 3

( ( )
)
e 2x −3y
(2h )
2
e 2(x+ (2h /3))−3( y+ )
k 2=h
2
3
k 2=
2 (
h 2( x+ (2h /3))
e
[ ( h
)])
−3 y+ ( e 2x −3y )
3

1 3
y i + 1= y i+ k 1+ k 2
4 4

BDA34003 Engineering Mathematics IV 27


Waluyo Adi Siswanto ([email protected])
step x y k1 k2 exact

0 0 1 −0.2 −0.1294
1 0.2 0.8529 −0.1067 −0.0398
2 0.4 0.7964 −0.0164 0.0549
3 0.6 0.8335 0.082 0.167
4 0.8 0.9793 0.2015 0.3126
5 1 1.2641 1.2468

BDA34003 Engineering Mathematics IV 28


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 29
Waluyo Adi Siswanto ([email protected])
Example 8-5

Do Example 8-1 using Modified Euler's method

3x
1 2x 6 − 2
Compare the result with the exact solution y ( x)= e + e
7 7

Check your results using RK2ModEuler function

BDA34003 Engineering Mathematics IV 30


Waluyo Adi Siswanto ([email protected])
2x 2x
2x e −3y e −3y
2y ' + 3y=e y'= f ( x i , y i )=
2 2

k 1 =h f ( x i , y i ) k 1 =h (
e 2x −3y
2 )
k 2=h f ( x i + h , y i + k 1 )

( ( ) )
2x
2( x+ ¿ h) e −3y
e −3( y+ h )
k 2=h
2
2
k 2=
2(
h 2(x+ h)
e
[ (
h
)])
−3 y+ ( e 2x −3y )
2

1 1
y i+ 1= y i+ k 1+ k 2
2 2

BDA34003 Engineering Mathematics IV 31


Waluyo Adi Siswanto ([email protected])
step x y k1 k2 exact

0 0 1 −0.2 −0.0908
1 0.2 0.8546 −0.1072 −0.0017
2 0.4 0.8002 −0.0175 0.0972
3 0.6 0.84 0.08 0.2193
4 0.8 0.9897 0.1984 0.3825
5 1 1.2801 1.2468

BDA34003 Engineering Mathematics IV 32


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 33
Waluyo Adi Siswanto ([email protected])
Runge-Kutta Method
Fourth Order RK : Classical Method

Classical RK method f ( x i , y i )= y ' (x i )


(RK4)
1
y i+ 1= y i+ (k + 2 k 2+ 2 k 3+ k4)
6 1

k 1 =h f ( x i , y i )

h k1
k 2=h f ( x i + , y i + )
2 2

h k2
k 3 =h f ( x i + , y i+ )
2 2
k 4 =h f ( xi + h , y i + k 3 )

BDA34003 Engineering Mathematics IV 34


Waluyo Adi Siswanto ([email protected])
Fourth order Runge-Kutta Classsical function:
function [x,y] = RK4Classic(func,a,b,y0,h)
%
format long;
n = (b-a)/h;
x =(a+h:h:b);
k1 = h*feval(func,a,y0)
k2 = h*feval(func,a+(h/2),y0+(k1/2))
k3 = h*feval(func,a+(h/2),y0+(k2/2))
k4 = h*feval(func,a+(h),y0+(k3))
y(1)= y0 + k1/6 + 2*k2/6 + 2*k3/6 + k4/6
for i=1 : n-1
k1 = h*feval(func,x(i),y(i));
k2 = h*feval(func,x(i)+h/2,y(i)+k1/2);
k3 = h*feval(func,x(i)+(h/2),y(i)+(k2/2));
k4 = h*feval(func,x(i)+(h),y(i)+(k3));
y(i+1)= y(i) + k1/6 + 2*k2/6 + 2*k3/6 + k4/6;
end
x = [a x].';
y = [y0 y].';

BDA34003 Engineering Mathematics IV 35


Waluyo Adi Siswanto ([email protected])
Runge-Kutta Method
Fifth Order RK :
Runge-Kutta-Merson method

Runge-Kutta-Merson method f ( x i , y i )= y ' (x i )

1 2 1
y i+ 1= y i+ k 1+ k 4+ k 5
6 3 6

k 1 =h f ( x i , y i )
1 1
k 2=h f ( x i + h , y i+ k 1 )
3 3
1 1 1
k 3 =h f ( x i + h , y i + k 1+ k 2)
3 6 6
1 1 3
k 4 =h f ( xi + h , yi + k 1+ k 3 )
2 8 8
1 3
k 5 =h f ( x i + h , yi + k 1− k 3+ 2 k 4 )
2 2

BDA34003 Engineering Mathematics IV 36


Waluyo Adi Siswanto ([email protected])
Fourth order Runge-Kutta Mearson function:

function [x,y] = RK5Merson(func,a,b,y0,h)


%
format long;
n = (b-a)/h;
x =(a+h:h:b);
k1 = h*feval(func,a,y0);
k2 = h*feval(func,a+(h/3),y0+(k1/3));
k3 = h*feval(func,a+(h/3),y0+(k1/6)+(k2/6));
k4 = h*feval(func,a+(h/2),y0+(k1/8)+(3*k3/8));
k5 = h*feval(func,a+(h),y0+(k1/2)-(3*k3/2)+(2*k4));
y(1)= y0 + k1/6 + 2*k4/3 + k5/6 ;
for i=1 : n-1
k1 = h*feval(func,x(i),y(i));
k2 = h*feval(func,x(i)+(h/3),y(i)+(k1/3));
k3 = h*feval(func,x(i)+(h/3),y(i)+(k1/6)+(k2/6));
k4 = h*feval(func,x(i)+(h/2),y(i)+(k1/8)+(3*k3/8));
k5 = h*feval(func,x(i)+(h),y(i)+(k1/2)-(3*k3/2)+(2*k4));
y(i+1)= y(i) + k1/6 + 2*k4/3 + k5/6;
end
x = [a x].';
y = [y0 y].';
BDA34003 Engineering Mathematics IV 37
Waluyo Adi Siswanto ([email protected])
Example 8-6

Do Example 8-1 using the Classical Runge Kutta (RK4) method

3x
1 2x 6 − 2
Compare the result with the exact solution y ( x)= e + e
7 7

Check your result using RK4Classic function

BDA34003 Engineering Mathematics IV 38


Waluyo Adi Siswanto ([email protected])
2x 2x
2x e −3y e −3y
2y ' + 3y=e y'= f ( x i , y i )=
2 2

k 1 =h f ( x i , y i ) k 1 =h (
e 2x −3y
2 )
h k1
k 2=h f ( x i + , y i + )
2 2

h k2
k 3 =h f ( x i + , y i+ )
2 2
k 4 =h f ( xi + h , y i + k 3 )

1
y i+ 1= y i+ (k 1 + 2 k 2+ 2 k 3+ k4)
6

BDA34003 Engineering Mathematics IV 39


Waluyo Adi Siswanto ([email protected])
step x y k1 k2 k3 k4

0 0 1 −0.2 −0.1479 −0.1557 −0.1041


1 0.2 0.8481 −0.1052 −0.0564 −0.0638 −0.0127
2 0.4 0.7884 −0.014 0.0374 0.0297 0.0866
3 0.6 0.8229 0.0851 0.1459 0.1368 0.2074
4 0.8 0.9659 0.2055 0.2844 0.2725 0.3674
5 1 1.247

1.2468 (Exact solution)

BDA34003 Engineering Mathematics IV 40


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 41
Waluyo Adi Siswanto ([email protected])
Example 8-7

Do Example 8-1 using the Runge-Kutta-Merson method

3x
1 2x 6 − 2
Compare the result with the exact solution y ( x)= e + e
7 7

BDA34003 Engineering Mathematics IV 42


Waluyo Adi Siswanto ([email protected])
2x 2x
2x e −3y e −3y
2y ' + 3y=e y'= f ( x i , y i )=
2 2

k 1 =h f ( x i , y i )
1 1
k 2=h f ( x i + h , y i+ k 1 )
3 3
1 1 1
k 3 =h f ( x i + h , yi + k 1+ k 2)
3 6 6
1 1 3
k 4 =h f ( xi + h , yi + k 1+ k 3 )
2 8 8
1 3
k 5 =h f ( x i + h , yi + k 1− k 3+ 2 k 4 )
2 2

1 2 1
y i+ 1= y i+ k 1+ k 4+ k 5
6 3 6

BDA34003 Engineering Mathematics IV 43


Waluyo Adi Siswanto ([email protected])
x y k1 k2 k3 k4 k5

0 1 −0.2 −0.1657 −0.1509 −0.1534 −0.0967


0.2 0.8483 −0.1053 −0.0735 −0.0677 −0.0607 −0.0102
0.4 0.7886 −0.014 0.0191 0.0156 0.034 0.0841
0.6 0.823 0.0851 0.124 0.1096 0.1431 0.1991
0.8 0.9658 0.2056 0.2557 0.2276 0.2819 0.3516
1 1.2466

1.2468 (Exact solution)

BDA34003 Engineering Mathematics IV 44


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 45
Waluyo Adi Siswanto ([email protected])
Multi-Step Method
Adam's Method

P h
Second order Adams predictor-corrector: y i+ 1= y i+ (3 f i − f i−1 )
2
(two initial values required)
C h P
y i+ 1= y i+ ( f + f i)
2 i+ 1

P h
Third order Adams predictor-corrector: y i+ 1= y i+ (23 f i −16 f i−1 + 5 f i−2 )
12
(three initial values required)
C h P
y i+ 1= y i+ (5 f i+ 1+ 8 f i − f i−1 )
12

P h
Fourth order Adams predictor-corrector: y i+ 1= y i+ (55 f i−59 f i−1 + 37 f i−2−9 f i−3 )
24
(four initial values required)
C h P
y i+ 1= y i+ (9 f i+ 1 + 19 f i −5 f i−1 + f i −2 )
24

BDA34003 Engineering Mathematics IV 46


Waluyo Adi Siswanto ([email protected])
Third order Adams predictor-corrector, starting RK2 Midpoint:
function [x,y] = Adams3RK2(func,a,b,y0,h)
%
format long;
% RK2 midpoint to start
n = (b-a)/h;
x =(a+h:h:b);
k1 = h*feval(func,a,y0);
k2 = h*feval(func,a+(h/2),y0+(k1/2));
y(1)= y0 + k2;
for i=1 : 1
k1 = h*feval(func,x(i),y(i));
k2 = h*feval(func,x(i)+(h/2),y(i)+(k1/2));
y(i+1) = y(i) +k2;
end
% Third order Adams predictor corrector
hh = h/12;
y(3) = y(2)+hh*(23*feval(func,x(2),y(2))-16*feval(func,x(1),y(1))+5*feval(func,a,y0));
y(3) = y(2)+hh*(5*feval(func,x(3),y(3))+8*feval(func,x(2),y(2))-feval(func,x(1),y(1)));
for i=3 : n-1
y(i+1) = y(i)+hh*(23*feval(func,x(i),y(i))-16*feval(func,x(i-1),y(i-1))+5*feval(func,x(i-2),y(i-2)));
y(i+1) = y(i)+hh*(5*feval(func,x(i+1),y(i+1))+8*feval(func,x(i),y(i))-feval(func,x(i-1),y(i-1)));
end
x = [a x].';
y = [y0 y].';

BDA34003 Engineering Mathematics IV 47


Waluyo Adi Siswanto ([email protected])
Third order Adams predictor-corrector, starting RK4 classical:
function [x,y] = Adams3RK4(func,a,b,y0,h)
%
format long;
% RK4 classical to start
n = (b-a)/h ;
x =(a+h:h:b);
k1 = h*feval(func,a,y0);
k2 = h*feval(func,a+(h/2),y0+(k1/2));
k3 = h*feval(func,a+(h/2),y0+(k2/2));
k4 = h*feval(func,a+(h),y0+(k3));
y(1)= y0 + k1/6 + 2*k2/6 + 2*k3/6 + k4/6;
for i=1 : 1
k1 = h*feval(func,x(i),y(i));
k2 = h*feval(func,x(i)+h/2,y(i)+k1/2);
k3 = h*feval(func,x(i)+(h/2),y(i)+(k2/2));
k4 = h*feval(func,x(i)+(h),y(i)+(k3));
y(i+1)= y(i) + k1/6 + 2*k2/6 + 2*k3/6 + k4/6;
end
% Third order Adams predictor corrector
hh = h/12;
y(3) = y(2)+hh*(23*feval(func,x(2),y(2))-16*feval(func,x(1),y(1))+5*feval(func,a,y0));
y(3) = y(2)+hh*(5*feval(func,x(3),y(3))+8*feval(func,x(2),y(2))-feval(func,x(1),y(1)));
for i=3 : n-1
y(i+1) = y(i)+hh*(23*feval(func,x(i),y(i))-16*feval(func,x(i-1),y(i-1))+5*feval(func,x(i-2),y(i-2)));
y(i+1) = y(i)+hh*(5*feval(func,x(i+1),y(i+1))+8*feval(func,x(i),y(i))-feval(func,x(i-1),y(i-1)));
end
x = [a x].';
BDA34003 Engineering Mathematics IV 48
y = [y0 y].'; Waluyo Adi Siswanto ([email protected])
Example 8-8

Do Example 8-1 using the third order Adams method


Use RK4 to provide necessary data to start Adams method

3x
1 2x 6 − 2
Compare the result with the exact solution y ( x)= e + e
7 7

Check your result using Adams2RK4 function

BDA34003 Engineering Mathematics IV 49


Waluyo Adi Siswanto ([email protected])
2x 2x
2x e −3y e −3y
2y ' + 3y=e y'= f ( x i , y i )=
2 2

k 1 =h f ( x i , y i ) k 1 =h (
e 2x −3y
2 )
h k1 h k2
k 2=h f ( x i + , y i + ) k 3 =h f ( x i + , y i+ )
2 2 2 2

1
k 4 =h f ( xi + h , y i + k 3 ) y i+ 1= y i+ (k + 2 k 2+ 2 k 3+ k4)
6 1
After 3 data, then use

P h
y i+ 1= y i+ (23 f i −16 f i−1 + 5 f i−2 )
12
C h P
y i+ 1= y i+ (5 f i+ 1+ 8 f i − f i−1 )
12

BDA34003 Engineering Mathematics IV 50


Waluyo Adi Siswanto ([email protected])
x y f k1 k2 k3 k4

0 1 1 −0.2 −0.1479 −0.1557 −0.1041


0.2 0.8481 −0.5262 −0.1052 −0.0564 −0.0638 −0.0127
0.4 0.7884 −0.0698
P
0.6 y 0.8186 0.4321
C
y 0.8239 0.4242
P
exact
0.8 y 0.9613 1.0346

y
C
0.9678 1.0248
P
1 y 1.2417 1.8320
C
1.2500 1.2468
y

BDA34003 Engineering Mathematics IV 51


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 52
Waluyo Adi Siswanto ([email protected])
Example 8-9

x
y'=
y
Solve the ODE above, from 0 to 1 , every 0.2
by using the third order Adams method
(Use RK4 to provide necessary data to start Adams method)

Check your result using Adams2RK4 function

BDA34003 Engineering Mathematics IV 53


Waluyo Adi Siswanto ([email protected])
x y f k1 k2 k3 k4

0 1 0 0.0000 0.0200 0.0198 0.0392


0.2 1.0198 0.1961 0.0392 0.0577 0.0572 0.0743
0.4 1.0770 0.3714
P
0.6 y 1.1671 0.5141
C
y 1.1661 0.5145
P
0.8 y 1.2807 0.6247

y
C
1.2806 0.6247
P
1 y 1.4138 0.7073

y
C
1.4143

BDA34003 Engineering Mathematics IV 54


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 55
Waluyo Adi Siswanto ([email protected])
Boundary Value Problems

The ODE involving first derivative of second derivative can be replaced


by numerical differentiations
y i+ 1 − y i−1
yi ' =
2h

y i+ 1 − 2 y i + yi−1
yi ' ' =
h2

Then all increments are written in terms of simultaneous equations.

All boundary conditions are incorporated in the equations

And finally solve the simultaneous equations to find all y at once.

BDA34003 Engineering Mathematics IV 56


Waluyo Adi Siswanto ([email protected])
Boundary Value Problems
Handling Boundary Conditions

Boundary conditions:

a. Without differentiation

The equation should be considered from i=1 to n-1


The boundary conditions at i=0 and i=n are substituted in the equations

b. With differentiation

The equation should be considered from i=0 to n


The differentiation of the boundary conditions should be replace by numerical
Differentiation before substituting into the equations.

BDA34003 Engineering Mathematics IV 57


Waluyo Adi Siswanto ([email protected])
Example 8-10

A system is represented by a differential equation y ' ' − xy ' + 3 y=11 x

It is required to find the response y from 1 to 2 with increment h = 0.2

The boundary conditions, y (1)=1.5

y (2)=15

BDA34003 Engineering Mathematics IV 58


Waluyo Adi Siswanto ([email protected])
Defining equation in numerical (finite different) form:

y ' ' − xy ' + 3 y=11 x


y i+ 1 − 2 y i + y i−1 y i+ 1− y i−1
− xi + 3 y i =11 x i
h2 2h
h 2 2
y i+ 1 − 2 yi + y i−1 − x i ( y i+ 1 − y i−1 )+ 3 h y i=h 11 xi
2
h 2 h 2
(1+ x i ) y i−1 + (3 h −2) y i + (1− x i ) y i+ 1=h 11 x i
2 2

Since the increment h = 0.2 , the equation can be simplified

(1+ 0.1 x i ) y i−1 −1.88 y i+ (1−0.1 x i ) y i+ 1=0.44 x i

BDA34003 Engineering Mathematics IV 59


Waluyo Adi Siswanto ([email protected])
x (i=1 : n−1) equation

x 1 =1.2 (1+ 0.1 x 1 ) y 0 −1.88 y 1+ (1−0.1 x 1 ) y 2 =0.44 x 1 y 0=1.5

−1.88 y 1+ 0.88 y 2 =−1.152

x 2 =1.4 (1+ 0.1 x 2 ) y 1 −1.88 y 2 + (1−0.1 x 2 ) y 3 =0.44 x 2

1.14 y 1 −1.88 y 2 + 0.86 y 3 =0.616

x 3 =1.6 (1+ 0.1 x 3 ) y 2 −1.88 y 3+ (1−0.1 x 3 ) y 4 =0.44 x 3

1.16 y 2−1.88 y 3 + 0.84 y 4 =0.704

x 4=1.8 (1+ 0.1 x 4 ) y 3−1.88 y 4+ (1−0.1 x 4 ) y 5 =0.44 x 4

1.18 y 3 −1.88 y 4 + 0.82 y 5 =0.792 y 5 =15

1.18 y 3 −1.88 y 4 =−11.508

BDA34003 Engineering Mathematics IV 60


Waluyo Adi Siswanto ([email protected])
In matrix form :

[ ]{ } { }
−1.88 0.88 0 0 y1 −1.152
1.14 −1.88 0.86 0 y 2 = 0.616
0 1.16 −1.88 0.84 y3 0.704
0 0 1.18 −1.88 y4 −11.508

See Lecture Module 3 to solve simultaneous equations

x 1 =1.2 y 1 =2.845
x 2 =1.4 y 2=2.7688
x 3 =1.6 y 3 =7.3698
x 4=1.8 y 4 =10.747

BDA34003 Engineering Mathematics IV 61


Waluyo Adi Siswanto ([email protected])
Example 8-11

3 4
A system follows a governing equation of y ' ' + xy= x − 3
along the x span from 1 to 2 x

The system is disturbed at the BC: y ' (1)=4 y ' (2)− y (2)=1.5

Find the system response y, every increment x = 0.2

BDA34003 Engineering Mathematics IV 62


Waluyo Adi Siswanto ([email protected])
y0 y1 y2 y3 y4 y5

h=0.2

x 0 =1 x 1 =1.2 x 2 =1.4 x 3 =1.6 x 4=1.8 x 5 =2

y ' (1)=4 y ' (2)− y (2)=1.5

y 1− y−1 y 6− y 4
=4 − y 5 =1.5
2h 2h

y−1 = y 1−1.6 y 6 = y 4 + 0.4 y 5 + 0.6

BDA34003 Engineering Mathematics IV 63


Waluyo Adi Siswanto ([email protected])
3 4
System equation: y ' ' + xy= x − 3
x
y i+ 1 − 2 y i + y i−1 4
+ x i yi = x 3i −
h2 x 3i
0.16 3
y i+ 1 − 2 yi + y i−1 + 0.04 x i y i=0.04 x i − 3
xi
3 0.16
y i+ 1 − 2 yi + y i−1 + 0.04 x i y i=0.04 x i − 3
xi

BDA34003 Engineering Mathematics IV 64


Waluyo Adi Siswanto ([email protected])
x (i=0 : n) equation
i=0 0.16 3
y i+ 1 − 2 yi + y i−1 + 0.04 x i y i=0.04 x i −
3
xi
y 1 − 2 y 0 + y 1−1.6+ 0.04 y 0 =0.04−0.16
− 1.96 y 0 + 2 y 1 =1.48

i=1 0.16 3
y i+ 1 − 2 yi + y i−1 + 0.04 x i y i=0.04 x i −
3
xi
3 3
y 2 − 2 y 1 + y 0 + 0.04(1.2) y 1 =0.04(1.2 )−0.16/(1.2 )
y 0−1.952 y 1+ y 2 =−0.0235

i=2 0.16 3
y i+ 1 − 2 yi + y i−1 + 0.04 x i y i=0.04 x i −
3
xi
3 3
y 3 − 2 y 2 + y 1+ 0.04(1.4) y 2=0.04(1.4 )−0.16 /(1.4 )
y 1 −1.944 y 2 + y 3 =0.0515

BDA34003 Engineering Mathematics IV 65


Waluyo Adi Siswanto ([email protected])
i=3 0.16 3
y i+ 1 − 2 yi + y i−1 + 0.04 x i y i=0.04 x i −
3
xi
3 3
y 4 − 2 y 3 + y 2+ 0.04 (1.6) y 3=0.04(1.6 )−0.16/(1.6 )
y 2−1.936 y 3 + y 4 =0.1248
i=4 0.16 3
y i+ 1 − 2 yi + y i−1 + 0.04 x i y i=0.04 x i −
3
xi
3 3
y 5 − 2 y 4 + y 3 + 0.04(1.8) y 4=0.04(1.8 )−0.16 /(1.8 )
y 3 −1.928 y 4 + y 5 =0.2058
i=5 0.16 3
y i+ 1 − 2 yi + y i−1 + 0.04 x i y i=0.04 x i −
3
xi
3 3
y 6 − 2 y 5 + y 4 + 0.04(2) y 5 =0.04(2 )−0.16 /(2 )
3 3
y 4 + 0.4 y 5 + 0.6− 2 y 5 + y 4 + 0.04(2) y 5=0.04(2 )−0.16/(2 )

2 y 4 −1.52 y 5=−0.3

BDA34003 Engineering Mathematics IV 66


Waluyo Adi Siswanto ([email protected])
[ ]{ } { }
−1.96 2 0 0 0 0 y0 1.48
1 −1.952 1 0 0 0 y1 −0.0235
0 1 −1.944 1 0 0 y 2 = 0.0515
0 0 1 −1.936 1 0 y3 0.1248
0 0 0 1 −1.928 1 y4 0.2058
0 0 0 0 2 −1.52 y5 −0.3

BDA34003 Engineering Mathematics IV 67


Waluyo Adi Siswanto ([email protected])
See Lecture Module 3 to solve simultaneous equations
x 0 =1 y 0=−0.9739
x 1 =1.2 y 1 =−0.2144
x 2 =1.4 y 2=0.5319
x 3 =1.6 y 3 =1.3000
x 4=1.8 y 4 =2.1095
x 5 =2 y 5 =2.9730

BDA34003 Engineering Mathematics IV 68


Waluyo Adi Siswanto ([email protected])
Student Activity

BDA34003 Engineering Mathematics IV 69


Waluyo Adi Siswanto ([email protected])
Problem 8-A1

A system has a differential equation y ' =4 e 0.8 x −0.5 y

The system is disturb at initial, y(0) =2

Approximate the solution at x=2 with step size h=1,

a. Using the clasical fourth order Runge-Kutta


b. Using Second order Runge-Kutta : Mid-point

Verify your results with freemat results.

BDA34003 Engineering Mathematics IV 70


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 71
Waluyo Adi Siswanto ([email protected])

You might also like