Numerical Methods for ODEs in Engineering
Numerical Methods for ODEs in Engineering
MATHEMATICS
in MECHANICAL
ENGINEERING
Lecture notes
Ha Le Nhu Ngoc Thanh, Ph.D.
1. Introduction
1. Introduction
2. Euler’s method
3. Modified Euler’s method
4. Runge – Kutta’s method
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 2 Mechanical Engineering
Introduction
Euler’s Method
Modified Euler’s Method 1. Pendulum problem
Runge – Kutta 2.
INTRODUCTION
Pendulum problem
Given a pendulum system as shown in Figure. Initial condition of the system (0)= 6 rad , (0)=0
Let determine a moving equation (t) of the mass m=1kg, L=2m.
Solution:
nd d2
Newton s 2 law for moment: O I I ,
2
dt
: torque
where : angle
mg sin
I : inertial moment, I mL2
Torque atO due to force: mg sin only mgL sin mg cos
O
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 3 Mechanical Engineering
Introduction
Euler’s Method
Modified Euler’s Method 1. Pendulum problem
Runge – Kutta 2.
INTRODUCTION
Pendulum problem
2 d2 d2 g
mgL sin mL sin 0 *
dt 2
dt 2 L
2
In the case of small oscillations sin
2 g 9.8 g
* 0, 4.9, T 2 2
L 2 L mg sin
find (t ), with (0) 6, 0 0 mg cos
2
L 0 s 2 (s ) s (0) (0) 2
(s ) 0
s
(s ) s 2 2
s (s )
6 6 2
2
s 4.9
1
(t ) L (s ) cos 4.9t 0.52 cos 4.9t
6
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 4 Mechanical Engineering
Introduction
Euler’s Method
Modified Euler’s Method 1. Pendulum problem
Runge – Kutta 2.
INTRODUCTION
Pendulum problem
g d2
Pendulum equation: sin 0 , where (0)= 6 rad , (0)=0 *
2 L
dt
In the case of small oscillations sin (t ) 0.52 cos 4.9t continuous curve
Normal method and Laplace can find out a continuous curve solution
In the case of large oscillations sin It is imposible to directly deal with
the ODE (*) by normal method and Laplace also.
Thus, we have to use a Numerical Method (Euler, Rung-Kutta)
to obtain an approximation ˆ(t) of solution (t) of ODEs,
Note: (t )
ˆ(t)are specific values of approximation (discrete)
ˆ(t )
An approximation of solution of ODEs only obtained in [a,b]
and there exists an error.
[a,b]large error
* so called : Initial Value Problem (IVP)
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 5 Mechanical Engineering
Introduction
Euler’s Method
Modified Euler’s Method 1. Concept of Euler’s method
Runge – Kutta 2. Euler’s method
EULER’S METHOD
Concept of Euler’s method
Suppose that we have a function f (x ), and intial value x 0 , f (x 0 ) , and f (x 0 ).
Now, we want to approximate the value of f at a
point close to x 0 to be x1 x0 h, the function f x 0 h
f x0 h f (x 0 ) hf (x 0 )
Of course, as h gets larger, our approximation
becomes less and less accurate, and vice versa.
Similar we can repeat with:
f x0 2h f (x 0 h) hf (x 0 h ),
f x0 3h f (x 0 2h ) hf (x 0 2h ), mesh point
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 6 Mechanical Engineering
Introduction
Euler’s Method
Modified Euler’s Method 1. Concept of Euler’s method
Runge – Kutta 2. Euler’s method
EULER’S METHOD
Euler’s method for IVP (Initial Value Problem)
Consider the Initial Value Problem given by:
dy
f t, y , a t b, y(a )
dt
A continuous solution y(t) will not be obtained;
instead, approximation to y will be generated at
a discrete values, called mesh point,
in th interval [a, b ].
The common distance between the point:
h h
b a
h ti 1 ti
N
h : Step size, N: amount of segment on the interval [a,b]
ti a ih, for each i 0, N
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 7 Mechanical Engineering
Introduction w0 y t0
Euler’s Method
Modified Euler’s Method 1. Concept of Euler’s method wi 1 wi hf ti , wi , i 0,1,...N 1
Runge – Kutta 2. Euler’s method
EULER’S METHOD
Euler’s method for IVP (Initial Value Problem)
Euler's method : h h
Let w 0 y t0
wi 1 wi hf ti , wi , i 0,1,...N 1
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 8 Mechanical Engineering
Introduction w0 y t0
Euler’s Method
Modified Euler’s Method 1. Concept of Euler’s method wi 1 wi hf ti , wi , i 0,1,...N 1
Runge – Kutta 2. Euler’s method
EULER’S METHOD
Example 1:
y(2) w4 4.4375
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 9 Mechanical Engineering
Introduction w0 y t0
Euler’s Method
Modified Euler’s Method 1. Concept of Euler’s method wi 1 wi hf ti , wi , i 0,1,...N 1
Runge – Kutta 2. Euler’s method
EULER’S METHOD
Example 1:
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 10 Mechanical Engineering
Introduction w0 y t0
Euler’s Method
Modified Euler’s Method 1. Concept of Euler’s method wi 1 wi hf ti , wi , i 0,1,...N 1
Runge – Kutta 2. Euler’s method
EULER’S METHOD
Example 2:
Approximate the solution of y y t 2 1, (*) where 0 t 2, y(0) 0.5
a ) N 10
𝑖 𝒕𝒊 𝐰𝒊
b N 20
0 0 0.5
c) Solve the equation by using Laplace
and compare with the approximate when N=10, N=20 1 0.2 0.8
Solution: 2 0.4 1.152
2 0 3 0.6 1.155
a) with N 10, h 0.2
10 4 0.8 1.98848
ti 0.2i, w 0 0.5
5 1.0 2.458176
w0 y0 0.5
6 1.2 2.9498112
wi 1 wi 0.2 wi ti2 1
7 1.4 3.4517734
w0 y0 0.5
8 1.6 3.9501281
2
wi 1.2wi 0.008i 0.2, i 0, 9
1 9 1.8 4.4281537
10 2.0 4.8657845
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 11 Mechanical Engineering
Introduction w0 y t0
Euler’s Method
Modified Euler’s Method 1. Concept of Euler’s method wi 1 wi hf ti , wi , i 0,1,...N 1
Runge – Kutta 2. Euler’s method
EULER’S METHOD
Example 2:
𝑖 𝒕𝒊 𝐰𝒊 𝑖 𝒕𝒊 𝐰𝒊
2 0
b) with N 20, h 0.1 0 0 0.5 11 1.1 2.79813
20
ti 0.1i, w 0 0.5 1 0.1 0.65 12 1.2 3.05694
w0 0.5 2 0.2 0.814 13 1.3 3.31863
3 0.3 0.9914 14 1.4 3.58150
wi 1 wi 0.1 wi ti2 1
4 0.4 1.18154 15 1.5 3.84365
w0 y0 0.5
5 0.5 1.38369 16 1.6 4.10301
2
wi 1 1.1wi 0.001i 0.1, i 0, 19 6 0.6 1.59706 17 1.7 4.35731
7 0.7 1.82077 18 1.8 4.60404
8 0.8 2.05384 19 1.9 4.84045
9 0.9 2.29523 20 2.0 5.06350
10 1.0 2.54375
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 12 Mechanical Engineering
Introduction w0 y t0
Euler’s Method
Modified Euler’s Method 1. Concept of Euler’s method wi 1 wi hf ti , wi , i 0,1,...N 1
Runge – Kutta 2. Euler’s method
EULER’S METHOD
Example 2:
c) y y t2 1, (*) where 0 t 2, y(0) 0.5
2 1
L y L y t2 1 sY (s ) 0.5 Y (s )
s3 s
s3 2s 1 s2 2 1 1
Y (s ) s 1 Y (s )
s4 2 s3 s 1 2 s 1
s2 2 As 2 Bs C D A D s3 A B s2 B C s C
s3 s 1 s3 s 1 s3 s 1
C 2 C 2
B C 0 B 2 s2 2 1 2 2 1
A B 1 A 1 s3 s 1 s s2 s3 s 1
A D 0 D 1
1 2 2 1 1
Y (s )
s s2 s3 2 s 1
1 1 t
y(t ) L Y (s ) 1 2t et2
2
2
y(t ) t 1 0.5et Exactly solution
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 13 Mechanical Engineering
Introduction w0 y t0
Euler’s Method
Modified Euler’s Method 1. Concept of Euler’s method wi 1 wi hf ti , wi , i 0,1,...N 1
Runge – Kutta 2. Euler’s method
EULER’S METHOD
Example 2: Using MATLAB to solve problem and compare
function [t,w] = func1(seg,N,t0,w0)
a=seg(1); % a
b=seg(2); % b
h=(b-a)/N; % step size
w(1)=w0+h*(w0-t0^2+1);
for i=1:N-1
t(i)=h*i;
w(i+1)=w(i)+h*(w(i)-(t(i))^2+1);
end
w=[w0 w]; % update w
t=0:h:(N*h); % pdate t
end
clc; clear all; close all;
% Approximation
t0=0;
w0=0.5;
seg=[0,2];
[t1,w1]=func1(seg,4,t0,w0);
[t2,w2]=func1(seg,10,t0,w0);
[t3,w3]=func1(seg,20,t0,w0);
% exactly solution
dt=0.1;
t=0:dt:2;
y=(t+1).^2-0.5*exp(t);
% plot and compare
plot(t,y,'-', t1,w1,'*-',t2,w2,'*-',t3,w3,'*-')
legend('Exact solution','N=4','N=10','N=20')
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 14 Mechanical Engineering
Introduction w0 y t0
Euler’s Method
Modified Euler’s Method 1. Concept of Euler’s method wi 1 wi hf ti , wi , i 0,1,...N 1
Runge – Kutta 2. Euler’s method
EULER’S METHOD
Example 3:
Use Euler’s method to approximate the solution:
y te 3t 2y, 0 t 2, y(0) 0, N 10
Solver :
a b 2
f (t, y ) te 3t 2y, 0 t 2, y(0) 0, N 10, h 0.2, ti 0.2i
N 10
Euler ' s method w0 y t0 0
function [t,w] = func2(seg,N,t0,w0)
wi 1 wi 0.2 f ti , wi , i 0, 9 a=seg(1); % a
b=seg(2); % b
w0 0 w0 0 h=(b-a)/N; % step size
w(1)=w0+h*(0-2*w0);
for i=1:N-1
3ti
wi 1 wi 0.2 tie 2wi wi 1 wi 0.2 0.2ie 0.6i 2wi t(i)=h*i;
w(i+1)=w(i)+h*(t(i).*exp(3*t(i))-2*w(i));
end
w0 0 w=[w0 w]; % update w
t=0:h:(N*h); % pdate t
0.04ie 0.6i
end
wi 1 0.6wi
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 15 Mechanical Engineering
Introduction w0 y t0
Euler’s Method
Modified Euler’s Method 1. Concept of Euler’s method wi 1 wi hf ti , wi , i 0,1,...N 1
Runge – Kutta 2. Euler’s method
EULER’S METHOD
Example 3: w0 0
y te 3t 2y wi 0.6wi 0.04ie 0.6i
1
1 As B C (A C )s 2 (2A B 6C )s 2B 9C
Y (s )
s 3
2
s 2 s2 6s 9 s 2 s 3
2
s 2 𝑖 𝒕𝒊 𝐰𝒊
(A C ) 0 A 0.04 0 0 0
0.04 0.2 0.04
(2A B 6C ) 0 B 0.32 Y (s )
s 3 s 3
2 s 2 1 0.2 0
2B 9C 1 C 0.04
Exact solution : y(t ) 0.04e 3t 0.2te 3t 0.04e 2t 2 0.4 0.07288
3 0.6 0.30934
clc; clear all; close all;
% Approximation 4 0.8 0.91156
t0=0;
w0=0;
seg=[0,2];
5 1.0 2.31064
[t1,w1]=func2(seg,10,t0,w0)
[t2,w2]=func2(seg,50,t0,w0); 6 1.2 5.40349
% exactly solution
dt=0.01; 7 1.4 12.02567
t=0:dt:2;
y=-0.04*exp(3*t)+0.2*t.*exp(3*t)+0.04*exp(-2*t);
% plot and compare 8 1.6 25.88757
plot(t,y,'-', t1,w1,'-.',t2,w2,'--')
legend('Exact solution','N=10','N=50') 9 1.8 54.41587
xlabel('time (t)')
ylabel('y')
10 2.0 112.3558
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 16 Mechanical Engineering
Introduction w0 y t0
Euler’s Method
Modified Euler’s Method 1. Concept of Euler’s method wi 1 wi hf ti , wi , i 0,1,...N 1
Runge – Kutta 2. Euler’s method
EULER’S METHOD
Exercises:
Use Euler’s method to approximate the solution:
a) y cos 2t sin 3t, 0 t 2, y(0) 1, N 10 compare with exact solution
2
b) y y t 2et , 1 t 2, y(1) 0, N 10, compare with exact solution : y(t ) t 2 e t e
t
1 y
c) y y 2 , 1 t 2, y(1) 1, h 0.05, compare with exact solution : y(t ) 1t
2 t
t
t
d) y y t 1, 0 t 5, y(0) 1, h 0.05, and h 0.1 compare with exact solution : y(t ) e t
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 17 Mechanical Engineering
Introduction
Euler’s Method 1. Modified Euler’s method
Modified Euler’s Method
Runge – Kutta
w0 0.5
h
wi
1 wi
f (ti , wi ) f (ti 1, wi hf (ti , wi ))
2
where f (ti 1, wi hf (ti , wi )) 1.2wi 0.048i 2 0.08i 1.16
w0 0.5
wi 1 wi 0.1 wi 0.04i 2 1 1.2wi 0.048i 2 0.08i 1.16
w0 0.5
wi 1 1.22wi 0.0088i 2 0.008i 0.216
2
Compare with exact solution : y(t ) t 1 0.5et
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 19 Mechanical Engineering
Introduction
Euler’s Method 1. Modified Euler’s method
Modified Euler’s Method
Runge – Kutta
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 21 Mechanical Engineering
Introduction
Euler’s Method 1. Runge – Kutta’s method
Modified Euler’s Method
Runge – Kutta
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 22 Mechanical Engineering
Introduction
Euler’s Method 1. Runge – Kutta’s method
Modified Euler’s Method
Runge – Kutta
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 23 Mechanical Engineering
Introduction
Euler’s Method 1. Runge – Kutta’s method
Modified Euler’s Method
Runge – Kutta
Example 1. (using CASIO Fx580) 0 0.0 0.5 0.3 0.328 0.3308 0.35816
1 0.2 0.8292933 0.35785 0.38364 0.38622 0.411103
f (t, y ) y t2
1, w 0 y 0 0.5, 2 0.4 1.2140762 0.41081 0.43389 0.43620 0.458056
2 0 3 0.6 1.6489220 0.45778 0.47756 0.47954 0.497692
N 10 h 0.2, t 0.2i
10 4 0.8 2.1272027 0.49744 0.51318 0.51476 0.528392
w0 , ti a ih 5 1.0 2.64082269 0.528164 0.538980 0.540062 0.5481770
k1 hf ti , wi 6 1.2 3.17989417 0.547978 0.552776 0.553256 0.5546301
7 1.4 3.73234007 0.554680 0.551914 0.551659 0.5447999
h 1
k2 hf ti , wi k1 8 1.6 4.28340949 0.544681 0.533150 0.531997 0.5150812
2 2 9 1.8 4.81508569 0.515017 0.492518 0.490269 0.4610709
h 1 10 2.0 5.30536300 0.461072 0.425179 0.421590 0.3773907
k3 hf ti , wi k2
2 2 A 0.2 y 0.04x 2 1
1 2
k4 hf ti 1 , wi k3 k1 A
k2 B
B 0.2 y
2
1
A 0.2x 0.1
2
1
6 wi y x
y
x
y
1
1
A 2B 2C D
6
2 1 2 1 2 2 1
A 0.2 y 0.04x 1 :B 0.2 y A 0.2x 0.1 1 :C 0.2 y B 0.2x 0.1 1 :D 0.2 y C 0.2 x 1 1 :x x 1:y y A 2B 2C D
2 2 6
CALC , input y 0.5, x 0,
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 24 Mechanical Engineering
Introduction
Euler’s Method 1. Runge – Kutta’s method
Modified Euler’s Method
Runge – Kutta
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 25 Mechanical Engineering
Introduction w0 y t0
Euler’s Method
Modified Euler’s Method 1. Concept of Euler’s method wi 1 wi hf ti , wi , i 0,1,...N 1
Runge – Kutta 2. Euler’s method
Applied Mathematics in
Ph.D. Ha Le Nhu Ngoc Thanh 26 Mechanical Engineering