0% found this document useful (0 votes)
21 views52 pages

Lecture 2

Uploaded by

Huy
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)
21 views52 pages

Lecture 2

Uploaded by

Huy
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/ 52

Computation in physics and

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).

 A partial differential equation (PDE) involves two or more


independent variables.
Ordinary Differential Equations

Differential equations are also classified as to their order:


1. A first order equation includes a first derivative as its highest derivative.

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

2. A second order equation includes a second derivative.

d2y dy
Linear 2nd order ODE 2
 p  Qy  f (x )
dx dx

Non-Linear 2nd order ODE d2y dy


2
 p( x, y )  Q ( x, y )  y  f ( x )
dx dx
Higher order equations can be reduced to a system of first order equations, by
redefining a variable.
Numerical Method

 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

Tính đạo hàm bằng phương pháp số

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

Chúng ta có thể tính gần đúng như sau:

yx0
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

plot(x(1:n), y(1:n), '-*','Linewidth',


1.5 ); 10
s1=num2str(0.1); s2=num2str(0.2);
legend(s1, s2); hold on
0
end 0 0.5 1 1.5 2 2.5 3 3.5 4
local and global errors
Euler’s method: local and global errors
Improvements of Euler’s method
A fundamental source of error in Euler’s method is that the
derivative at the beginning of the interval is assumed to apply across
the entire interval.
Simple modifications are available:
– Euler-Cromer method
– Heun’s Method
– The Midpoint Method
– Ralston’s Method
Euler-Cromer
2 2
d r/dt =a => dr/dt=v; dv/dt=a
Improvements of Euler’s method
Three of the most commonly used methods are:
yi 1  yi  ( a1k1  a 2 k 2 ) h k1  f ( xi , yi )
• Huen Methodwith a Single Corrector (a 1 2
=1/2)i i
 yi  ( k1  k 2 )  h k 2  f ( xi  h ), ( yi  k1h ) 
1 1
yi 1
2 2
• The Midpoint Method (a2= 1)
1 1
yi  1  yi  k 2  h k 2  f ( xi  h, yi  k1h )
2 2
k  f (x , y )
• Raltson’s Method (a2= 2/3)
1 2 3 3
yi  1  yi  ( k1  k 2 )  h k 2  f ( xi  h, yi  k1h )
3 3 4 4
Heun’s Method:
Heun’s Method:
Involves the determination of two derivatives
for the interval at the initial point and the end
point.

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:

Uses Euler’s method to predict a value


of y at the midpoint of the interval:

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

a=0; b=6;n=(b-a)/h; k 2  f ( xi  h ), ( yi  k1h ) 


for i=1:n
x(i)=(i-1)*h;k1=f(x(i)); k2=f(x(i)+h);
y(i+1)=y(i)+0.5*(k1+k2)*h;
end
plot(x(1:n), y(1:n), '-Og','Linewidth', 1.5 );
text(x(n),y(n)-2,sprintf('y\047(0)=%s','Heun'),
'FontSize',15);
end
Mid_point method
% function for solving dy/dx=x.^3
function I1=Mid_point(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)+h/2);
y(i+1)=y(i)+k2*h;
end
plot(x(1:n), y(1:n), '-sb','Linewidth', 1.5 );
text(x(n)-2,y(n),sprintf('y\047(0)=%s','Midpoint'),
'FontSize',15);
end
Ralston-method
% function for solving dy/dx=x.^3

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

The Midpoint Method


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

y3= RK3(F, a, b, y0, h)';


6.5
plot(y3(1,:), y3(2,:), 'o-r');
y2= RK2(F, a, b, y0, h)'; 6
plot(y2(1,:), y2(2,:), '+-g');
5.5

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

2011 Computational Physics


Third order Runge-Kutta method: local and global errors

51
Fourth order Runge-Kutta method: local and global errors

52

You might also like