Lecture 2
Lecture 2
materials science
Editor by: Nguyen Van Hong
1
Differential Equations
Differential Equations
Equations which are composed of an unknown function and its
derivatives are called differential equations.
dv c v - dependent variable
g v t - independent variable
dt m
Differential equations play a fundamental role in engineering
because many physical phenomena are best formulated
mathematically in terms of their rate of change.
Differential Equations (DE)
DEs have applications in various fields of Science:
Physics (dynamics, thermodynamics, heat, fluid mechanics, and
electromagnetism)
Chemistry (rate of chemical reactions, physical chemistry, and radioactive
decay)
Biology (growth rates of bacteria, plants and other organisms)
Economics (economic growth rate, and population growth rate)
Almost all modern scientific investigations involve differential
equations.
Differential Equations
When a function involves one independent variable, the
equation is called an ordinary differential equation (ODE).
dy
- Linear 1st order ODE y f (x )
dx
dy
1st
- Non-Linear order ODE f ( x, y )
Where f(x,y) is nonlinear dx
Ordinary Differential Equations
d2y dy
Linear 2nd order ODE 2
p Qy f (x )
dx dx
Euler method
Euler–Cromer Method
Heun-Method
Midpoint method
Ralston method
Runge-Kutta method
Use of ode45
Finite Difference Method
8
Ordinary Differential Equations
Phương Pháp Euler
Theo định nghĩa thì đạo hàm của y(x) tại điểm x0 là:
y x0 h y x0
y x0 lim
h 0 h
yx0
y x0 h y x0 Với h có giá trị nhỏ.
h Forward Difference Formula
Phương Pháp Euler
y x0 h y ( x0 )
y x0 Với h nhỏ
h
hy x0 y x0 h y( x0 )
y( x0 ) hy x0 y x0 h
y x0 h y( x0 ) hy x0
Phương Pháp Euler
Solving ODE of the form:
dy
f ( x, y )
dx
dy
Euler’s Method f ( x, y )
dx
solution yi 1 yi f ( xi , yi ) h
Euler Methods
yi 1 yi f ( xi , yi ) h
dy
f ( x, y )
dx
yi 1 yi f ( xi , yi ) h
Euler’s Method: Example
Obtain a solution between x = 0 to x = 4
dy
with a step size of 0.5 for: 2 x 3 12 x 2 20 x 8.5
dx
Initial conditions are: x = 0; y = 1
Solution: yi 1 yi f ( xi , yi ) h
Euler’s Method: Example
Although the computation captures the general trend solution,
the error is considerable.
This error can be reduced by using a smaller step size.
60
50
40
30
20 𝒅𝒚
= 𝒙𝟑
𝒅𝒙
10
dy
2 x 3 12 x 2 20 x 8.5
dx
0
0 0.5 1 1.5 2 2.5 3 3.5 4
70
%program solving eq: y'=x.^3, y(0)=1;
clc; clear all;
Eule(0.1); 60
0.1
Eule(0.2); 0.2
x=linspace(1, 4, 50) data1
50
plot(x, x.^4/4+1, '-','Linewidth',2)
function I1=Eule(h0)
f=@(x) x.^3; h=h0; y(1)=1; 40
a=0; b=4;n=(b-a)/h;
for i=1:n 30
x(i)=(i-1)*h;
y(i+1)=y(i)+f(x(i))*h;
end 20
1 1
yi 1 y i ( k1 k 2 ) h
2 2
k1 f ( xi , yi )
k 2 f ( xi h ), ( yi k1h )
Midpoint Method:
yi 1 yi k 2 h
kk1 f ( xfi ,(yx
i)
,y)
1 1
k 2 f ( xi h, yi k1h )
2 2
Chapter 25
Ralston’s Method:
1 2
yi 1 yi ( k1 k 2 ) h
3 3
k1 f ( xi , yi )
3 3
k 2 f ( xi h, yi k1h )
4 4
Chapter 25
Modified Euler method: local and global errors
%program solving eq: y'=x.^3, y(0)=1;
%program solving eq: y'=x.^3, y(0)=1;
clc, clear all
dx=0.5
Eule(dx); hold on
Heun(dx);
Mid_point(dx);
Ralton(dx); hold on
x=linspace(0, 6, 60); y=x.^4/4+1;
plot(x, y, '-k', 'Linewidth', 3);
text(x(54),y(60),sprintf('y\047=x^3'), 'FontSize',35);
legend('Euler', 'Heun', 'Midpoint', 'Ralton', 'Exact');
set(gca, 'FontSize', 16); % for tick marks
350
y'=x3
300
Euler
Heun
250
Midpoint
Ralton
Exact
200
150
100
50
0
0 1 2 3 4 5 6
Heun-method
% function for solving dy/dx=x.^3
function I1=Heun(h0) k1 f ( xi , yi )
f=@(x) x.^3; h=h0; y(1)=1; 1 i i
function I1=Ralton(h0)
f=@(x) x.^3; h=h0; y(1)=1;
a=0; b=6;n=(b-a)/h;
for i=1:n
x(i)=(i-1)*h;k1=f(x(i)); k2=f(x(i)+3*h/4);
y(i+1)=y(i)+(1/3*k1+2/3*k2)*h;
end
plot(x(1:n), y(1:n), '-sc','Linewidth', 1.5 );
text(x(n)+0.1,y(n)+14,sprintf('y\047(0)=%s','Ralto
n'), 'FontSize',15);
end
Bài tập áp dụng
Sử dụng các phương pháp: Huen Method, Midpoint Method,
Ralston’s Method để giải phương trình sau với ĐK biên
y(x=0)=1:
Chapter 25 35
Bài tập áp dụng
Sử dụng các phương pháp: Huen Method, Midpoint Method,
Ralston’s Method để giải phương trình:
Bài tập áp dụng
Runge-Kutta Methods
Runge-Kutta Methods
for solving
Second-Order Runge-Kutta Methods
Second-Order Runge-Kutta Methods
Ralston’s Method
Third-Order Runge-Kutta Methods
Fourth-Order Runge-Kutta Methods
Higher-Order Runge-Kutta Methods
Range-kutta method
2011
Fourth-Order Runge-Kutta Methods
clc, clear
h=0.1; a=0; b=3; t = a:h:b; y(1) = 5;
F= @(t,y) 3.*exp(-t)-0.4*y;
for i=1:1:numel(t)-1
k1 = F(t(i),y(i));
k2 = F(t(i)+0.5*h,y(i)+0.5*h*k1);
k3 = F((t(i)+0.5*h),(y(i)+0.5*h*k2));
k4 = F((t(i)+h),(y(i)+h*k3));
y(i+1) = y(i) + (h/6)*(k1+2*k2+2*k3+k4);
end
plot(t, y, 'o-r');
title('dy/dt=3.*exp(-t)-0.4*y');
xlabel('t');ylabel('y');
Runge-Kutta Methods: second, third and fourth order
9
clc, clear
h=0.5; a=0; b=4; 8.5 RK4
y0=5; RK3
8 RK2
F=@(t,y) -2*t.^3+12*t.^2-20*t+8.5;
y4= RK4(F, a, b, y0, h)'; 7.5
plot(y4(1,:), y4(2,:), '*-b');
hold on 7
5
0 0.5 1 1.5 2 2.5 3 3.5 4
2023
Functions: RK4
function yy= RK4(F, a, b, y0, h)
t = a:h:b;
y(1) = y0;
A=[]
for i=1:1:length(t)-1
k1 = F(t(i),y(i));
k2 = F(t(i)+0.5*h,y(i)+0.5*h*k1);
k3 = F((t(i)+0.5*h),(y(i)+0.5*h*k2));
k4 = F((t(i)+h),(y(i)+h*k3));
y(i+1) = y(i) + (h/6)*(k1+2*k2+2*k3+k4);
end
t=t'; y=y';yy=[t, y];
return
end
2023 TTVL-KHVL
Functions: RK3
function yy= RK3(F, a, b, y0, h)
t = a:h:b; y(1) = y0;
for i=1:1:length(t)-1
k1 = F(t(i),y(i));
k2 = F(t(i)+0.5*h,y(i)+0.5*h*k1);
k3 = F((t(i)+h),(y(i)-h*k1+2*k2*h));
y(i+1) = y(i) + (h/6)*(k1+4*k2+k3);
end
t=t'; y=y';yy=[t, y];
return
end
Functions: RK2 (Heun method)
function yy= RK2(F, a, b, y0, h) % Heun method
t = a:h:b; y(1) = y0;
for i=1:1:length(t)-1
k1 = F(t(i),y(i));
k2 = F(t(i)+h,y(i)+h*k1);
y(i+1) = y(i) + (h/2)*(k1+k2);
end
t=t'; y=y';yy=[t, y];
return
end
51
Fourth order Runge-Kutta method: local and global errors
52