SlideShare a Scribd company logo
1
Computational Method to Solve
the Partial Differential
Equations (PDEs)
By
Dr. Khurram Mehboob
1
Mehboob, K
NE: 402 computation Method in Nuclear
Engineering
Department of Nuclear Engineering,
KAU, Jeddah, KSA
2017. 5. 1
How to solve 1st Order PDEs
using MATLAB
2
Contents
• Solving PDEs using MATLAB (Explicit method)
- FTCS method
- Lax method
- Crank Nicolson method
- Jacobi’s method
- Simultaneous-over-relaxation (SOR) method
• Solving PDEs using MATLAB
- Examples of PDEs
3
PDE (Partial Differential Equation)
PDEs are used as mathematical models for phenomena in
all branches of engineering and science.
2 2 2
2 2
2 ( , , , , )
u u u u u
A B C D x t u
t xt x t x
    
  
    
u u
and
t x
 
 
linear in
- Three Types of PDEs:
1) Elliptic:
 Steady heat transfer, flow and diffusion
2) Parabolic:
 Transient heat transfer, flow and diffusion
3) Hyperbolic:
 Transient wave equation
2
0B AC 
2 2
2 2 0u u
x y
  
 
Laplace equations
2
0B AC 
2
2
u uD
t x
 
 
Diffusion Equation
2
0B AC  2 2
2 2 2
1u u
x c t
 
 
Wave Equation
4
Similarity method to solve PDE
often the solution of a PDE together wit its initial and boundary problem depends upon its
variables. In such cases the problem can be reduce to the combination ODEs with its
independent variables
The key to the similarity solution is the diffusing equation is that both the equations and
initial and boundary conditions are under the scaling x→lx and t →l2x.
Therefore, x/√t is independent transformation. In general the function should be of
form
The u(x,t)=U(z)
2
2
u u
t x
 
 
, 0t x 
( , ) 1 0
( , ) 0 0
u x t t
u x t x
 
 
( / )u t f x t 

x
t
 
2
2
1 0
2
U U
 
  
 
(0) 1
( ) 0
U
U

 
2
/4
0
( ) s
U C e ds D

 
 
2
/41( ) s
U e ds





 
2
/4
/
1( , ) s
x t
u x t e ds



 
5
FTCS method for Advection Eq
Advection equation
The Forward time and central space (FTCS)
method implementation
,
0,1........
0,1........
( , )
n o n
j o n
n j n j
x x n x n N
t t j x j t
u u x t
   
   

, 1 , 1, 1,
2
n j n j n j n j
u u u u
c
t x
  
 
 
 
0u uc
t x
  
 
FTCS is explicit scheme
, 1 , 1, 1,
( )
2n j n j n j n j
c tu u u u
x  
  

6
Implementation in MATLAB
FTCS is explicit scheme
Square-wave Test for the Explicit Method to solve the Advection Equation
clear;
% Parameters to define the advection equation and the range in space and time
Lmax = 1.0; % Maximum length
Tmax = 1.0; % Maximum time
c = 1.0; % Advection velocity
% Parameters needed to solve the equation within the explicit method
maxt = 3000; % Number of time steps
dt = Tmax/maxt;
n = 30; % Number of space steps
nint=15; % The wave-front: intermediate point from which u=0
dx = Lmax/n;
b = c*dt/(2.*dx);
% Initial value of the function u (amplitude of the wave)
for i = 1:(n+1)
if i < nint
u(i,1)=1.;
else
u(i,1)=0.;
end
x(i) =(i-1)*dx;
end
% Value of the amplitude at the boundary
for k=1:maxt+1
u(1,k) = 1.;
u(n+1,k) = 0.;
time(k) = (k-1)*dt;
end
% Implementation of the explicit method
for k=1:maxt % Time loop
for i=2:n % Space loop
u(i,k+1) =u(i,k)-b*(u(i+1,k)-u(i-1,k));
end
end
% Graphical representation of the wave at different selected times
plot(x,u(:,1),'-',x,u(:,10),'-',x,u(:,50),'-',x,u(:,100),'-')
title('Square-wave test within the Explicit Method I')
xlabel('X')
ylabel('Amplitude(X)')
0u uc
t x
  
 
( , ) 1 0
( , ) 0 0
u x t t
u x t x
 
 
7
0.025
2
c t
x
 

FTSC Lax method for Advection Eq
Advection equation
To increase the stability of FTCS method simply
talke the average of term u(n,j) ,
0,1........
0,1........
( , )
n o n
j o n
n j n j
x x n x n N
t t j x j t
u u x t
   
   

0u uc
t x
  
 
FTCS is explicit scheme, 1 , 1, 1,
( )
2n j n j n j n j
c tu u u u
x  
  

1, 1,
,
( )
2
n j n j
n j
u u
u  


1, 1,
, 1 1, 1,
( )
( )
2 2
n j n j
n j n j n j
u u c tu u u
x
 
  
   

LEX method (explicit)
8
Implementation in MATLAB
LEX method is explicit scheme
0u uc
t x
  
 
( , ) 0 0 ( , ) 1 0u x t t u x t x   
wave Test for the Lax method to solve the Advection Equation
clear;
% Parameters to define the advection equation and the range in space and time
Lmax = 1.0; % Maximum length
Tmax = 1.; % Maximum time
c = 1.0; % Advection velocity
% Parameters needed to solve the equation within the Lax method
maxt = 350; % Number of time steps
dt = Tmax/maxt;
n = 300; % Number of space steps
nint=50; % The wave-front: intermediate point from which u=0 (nint<n)!!
dx = Lmax/n;
b = c*dt/(2.*dx);
% The Lax method is stable for abs(b)=< 1/2 but it gets difussed unless abs(b)= 1/2
% Initial value of the function u (amplitude of the wave)
for i = 1:(n+1)
if i < nint
u(i,1)=1.;
else
u(i,1)=0.;
end
x(i) =(i-1)*dx;
end
% Value of the amplitude at the boundary at any time
for k=1:maxt+1
u(1,k) = 1.;
u(n+1,k) = 0.;
time(k) = (k-1)*dt;
end
% Implementation of the Lax method
for k=1:maxt % Time loop
for i=2:n % Space loop
u(i,k+1) =0.5*(u(i+1,k)+u(i-1,k))-b*(u(i+1,k)-u(i-1,k));
end
end
% Graphical representations of the evolution of the wave
figure(1)
mesh(x,time,u')
title('Square-wave test within the Lax Method'); xlabel('X'); ylabel('T')
figure(2)
plot(x,u(:,1),'-',x,u(:,20),'-',x,u(:,50),'-',x,u(:,100),'-')
title('Square-wave test within the Lax Method')
xlabel('X')
ylabel('Amplitude(X)')
9
1.01
2
c t
x
 

1.0
2
c t
x
 

0.5
2
c t
x
 

CTCS method for Advection
Advection equation
The Forward time and central space (FTCS)
method implementation
,
0,1........
0,1........
( , )
n o n
j o n
n j n j
x x n x n N
t t j x j t
u u x t
   
   

0u uc
t x
  
 
Center point in time
2 2, 1 , 1 1, 1,
( ) ( )
2 2
n j n j n j n j
u u u uu uo t o x
t t x x
   
       
   
Center point in Space
, 1 , 1 1, 1,
2 2
n j n j n j n ju u
t
u u
c
x
   


 

, 1 , 1 1, 1,
2 ( )
2n j n j n j n j
c tu u u u
x   
  

, 1 , 1 1, 1,
( )n j n j n j n j
c tu u u u
x   
  

CTCS is also an explicit scheme
10
CTCS Lax method for Advection Eq
Advection equation
To increase the stability of CTCS method simply
take the average of term u(n,j-1)
,
0,1........
0,1........
( , )
n o n
j o n
n j n j
x x n x n N
t t j x j t
u u x t
   
   

0u uc
t x
  
 
CTCS explicit scheme, 1 , 1 1, 1,
( )n j n j n j n j
c tu u u u
x   
  

1, 1 1, 1
, 1
( )
2
n j n j
n j
u u
u    



1, 1 1, 1
, 1 1, 1,
( )
( )
2
n j n j
n j n j n j
u u c tu u u
x
   
  
   

LEX CTCS method (explicit)
, 1 , 1 1, 1,
( )n j n j n j n j
c tu u u u
x   
  

11
Implementation in MATLAB (CTCS)
0u uc
t x
  
 
( , ) 1 0
( , ) 0 0
u x t t
u x t x
 
 
% Matlab Program : Square-wave Test for the Explicit Method to solve the advection Equation
clear;
% Parameters to define the advection equation and the range in space and % time
Lmax = 1.0; % Maximum length
Tmax = 1.; % Maximum time
c = 1.0; % Advection velocity
% Parameters needed to solve the equation within the explicit method
maxt = 100; % Number of time steps
dt = Tmax/maxt;
n = 100; % Number of space steps
nint=15; % The wave-front: intermediate point from which u=0
dx = Lmax/n;
b = c*dt/(dx)
% Initial value of the function u (amplitude of the wave)
for i = 1:(n+1)
if i < nint
u(i,1)=1.;else
u(i,1)=0.;
end
x(i) =(i-1)*dx;
end
% Value of the amplitude at the boundary
for k=2:maxt+1
u(1,k) = 1.;
u(n+1,k) = 0.;
time(k) = (k-1)*dt;
end
% Implementation of the explicit method
for k=2:maxt % Time loop
for i=2:n % Space loop
u(i,k+1) =u(i,k-1)-b*(u(i+1,k)-u(i-1,k));
end
end
% Graphical representation of the wave at different selected times
plot(x,u(:,10),'-',x,u(:,50),'-',x,u(:,100),'-',x,u(:,maxt),'-')
title('Square-wave test within the Explicit Method')
xlabel('X')
ylabel('Amplitude(X)')
1c t
x
 

0.5c t
x
 

12
Implementation in MATLAB (CTCS)
0u uc
t x
  
 
( , ) 1 0
( , ) 0 0
u x t t
u x t x
 
 
% Matlab Program : Square-wave Test for the Explicit Method to solve the advection Equation
clear;
% Parameters to define the advection equation and the range in space and % time
Lmax = 1.0; % Maximum length
Tmax = 1.; % Maximum time
c = 1.0; % Advection velocity
% Parameters needed to solve the equation within the explicit method
maxt = 100; % Number of time steps
dt = Tmax/maxt;
n = 100; % Number of space steps
nint=15; % The wave-front: intermediate point from which u=0
dx = Lmax/n;
b = c*dt/(dx)
% Initial value of the function u (amplitude of the wave)
for i = 1:(n+1)
if i < nint
u(i,1)=1.;else
u(i,1)=0.;
end
x(i) =(i-1)*dx;
end
% Value of the amplitude at the boundary
for k=2:maxt+1
u(1,k) = 1.;
u(n+1,k) = 0.;
time(k) = (k-1)*dt;
end
% Implementation of the explicit method
for k=2:maxt % Time loop
for i=2:n % Space loop
u(i,k+1) =0.5*(u(i+1,k-1)+u(i-1,k-1))-b*(u(i+1,k)-u(i-1,k));
end
end
% Graphical representation of the wave at different selected times
plot(x,u(:,10),'-',x,u(:,50),'-',x,u(:,100),'-',x,u(:,maxt),'-')
title('Square-wave test within the Explicit Method')
xlabel('X')
ylabel('Amplitude(X)')
0.5c t
x
 

1c t
x
 

13
Mehboob, K
NE: 402 computation Method in Nuclear
Engineering
Department of Nuclear Engineering,
KAU, Jeddah, KSA
2017. 5. 3
How to solve 2nd Order PDEs
using MATLAB
14
Diffusion Problem
),(
),(),(
2
2
txS
x
txu
D
t
txu






L
x
xu
conditionsinitial
tutLututu
conditionsBoundry
2
cos100)0,(
),(),(),(),0( 21



ydiffusivitThermalD :
SourceHeattxS :),(
),(
),(),(
txS
x
txu
D
xt
txu













15
FTCS method for the heat equation
FTCS ( Forward Euler in Time and Central difference in Space )
):),(),(),(),0((
),(),(),(
21
2
2
ydiffusivitthermalDtutLututu
txStxu
x
Dtxu
t







Heat equation in a slab
),( 1
t
uu
t
txu n
j
n
jnj




 
2
11
2
2
2),(
x
uuu
x
txu n
j
n
j
n
jnj




 
n
jnj utxu ),(Let
Forward Euler in Time
2nd order Central difference in Space
1
2 1 1
( 2 )n n n n n n
j j j j j j
D tu u tS u u u
x

 
    

1
1 1
2
2
( , )
n n n n n
j j j j j
u u u u u
D s x t
t x

 
  
 
 
FTCS explicit scheme for diffusion Equation
16
Implementation in MATLAB (FTCS heat equation)
% Heat Diffusion in one dimensional wire within the % Explicit Method
clear;
% Parameters to define the heat equation and the range in space and time
L = 1.; % Length of the wire
T =1.; % Final time
% Parameters needed to solve the equation within the explicit method
maxk = 2500; % Number of time steps
dt = T/maxk;
n = 50; % Number of space steps
dx = L/n;
cond = 1/4; % Conductivity
b = cond*dt/(dx*dx); % Stability parameter (b=<1)
% Initial temperature of the wire: a sinus.
for i = 1:n+1
x(i) =(i-1)*dx;
u(i,1) =sin(pi*x(i));
end
% Temperature at the boundary (T=0)
for k=1:maxk+1
u(1,k) = 0.;
u(n+1,k) = 0.;
time(k) = (k-1)*dt;
end
% Implementation of the explicit method
for k=1:maxk % Time Loop
for i=2:n; % Space Loop
u(i,k+1) =u(i,k) + b*(u(i-1,k)+u(i+1,k)-2.*u(i,k));
end
end
% Graphical representation of the temperature at different selected times
figure(1)
plot(x,u(:,1),'-',x,u(:,100),'-',x,u(:,300),'-',x,u(:,600),'-')
title('Temperature within the explicit method')
xlabel('X')
ylabel('T')
figure(2)
mesh(x,time,u')
title('Temperature within the explicit method')
xlabel('X')
ylabel('Temperature')
),(
),(),(
2
2
txS
x
txu
D
t
txu






)sin()0,(
0),(,0),(,0),0(
xxu
conditionsinitial
txstLutu
conditionsBoundry


17
Implementation in MATLAB (FTCS heat equation)
Courant condition for
FTCS
1
2
2



x
tD
2
2 1.01D t
x
 
2
2 1
2
D t
x
 

18
CTCS method for the heat equation
FTCS ( Forward Euler in Time and Central difference in Space )
):),(),(),(),0((
),(),(),(
21
2
2
ydiffusivitthermalDtutLututu
txStxu
x
Dtxu
t







Heat equation in a slab
2
),( 11
t
uu
t
txu n
j
n
jnj




 
2
11
2
2
2),(
x
uuu
x
txu n
j
n
j
n
jnj




 
n
jnj utxu ),(Let
Center difference in Time
2nd order Central difference in Space
1 1
2 1 1
22 ( 2 )n n n n n n
j j j j j j
D tu u tS u u u
x
 
 
     

1 1
1 1
2
2
( , )
2
n n n n n
j j j j j
u u u u u
D s x t
t x
 
 
  
 
 
FTCS explicit scheme for diffusion Equation
19
CTCS method for the heat equation% Heat Diffusion in one dimensional wire within the % Explicit Method
clear; clc;
% Parameters to define the heat equation and the range in space and time
L = 1.; % Length of the wire
T =1.; % Final time
% Parameters needed to solve the equation within the explicit method
maxk = 600; % Number of time steps
dt = T/maxk;
n = 20; % Number of space steps
dx = L/n;
cond = 1/132; % Conductivity
b =2.*cond*dt/(dx*dx) % Stability parameter (b=<0.01)
% Initial temperature of the wire: a sinus.
for i = 1:n+1
x(i) =(i-1)*dx;
u(i,1) =sin(pi*x(i));
end
% Temperature at the boundary (T=0)
for k=1:maxk+1
u(1,k) = 0.;
u(n+1,k) = 0.;
time(k) = (k-1)*dt;
end
% Implementation of the explicit method
for k=2:maxk % Time Loop
for i=2:n; % Space Loop
u(i,k+1) =u(i,k-1) + 0.5*2*b*(u(i-1,k)+u(i+1,k)-2.*u(i,k));
end
end
% Graphical representation of the temperature at different selected times
figure(1)
plot(x,u(:,1),'-',x,u(:,10),'-',x,u(:,30),'-',x,u(:,60),'-')
title('Temperature within the explicit method')
xlabel('X')
ylabel('T')
figure(2)
mesh(x,time,u')
title('Temperature within the explicit method')
xlabel('X')
ylabel('Temperature')
),(
),(),(
2
2
txS
x
txu
D
t
txu






)sin()0,(
0),(,0),(,0),0(
xxu
conditionsinitial
txstLutu
conditionsBoundry


0126.0
2
2



x
tD
20
Stability of FTCS and CTCS
FTCS is first-order accuracy in time and second-order
accuracy in space.
So small time steps are required to achieve reasonable
accuracy.
Courant condition for FTCS
2
1
2



x
tD
)2(
2
2 112
11 n
j
n
j
n
j
n
j
n
j
n
j uuu
x
tD
tSuu 





CTCS method for heat equation
(Both the time and space derivatives are
center-differenced.)
However, CTCS method is unstable for
any time step size.
2
2 1
2
D t
x
 

2
2 0.0126D t
x
 

CTCS (unstable)
FTCS (Stable)
21
Lax method (FTCS)
Simple modification to the FTCS method
In the differenced time derivative,
)2()( 112112
11 n
j
n
j
n
j
n
j
n
j
n
j
n
j uuu
x
tD
tSuuu 





t
uuu
t
uu n
j
n
j
n
j
n
j
n
j




 

)]([)( 112
111
Replacement by average value from surrounding grid points
The resulting difference equation is
Courant condition for Lax method
4
1
2



x
tD
( Second-order accuracy in both time and space )
22
Lax method(CTCS)
Simple modification to the CTCS method
In the differenced time derivative,
)2(
2
2)( 112
1
1
1
12
11 n
j
n
j
n
j
n
j
n
j
n
j
n
j uuu
x
tD
tSuuu 









t
uuu
t
uu n
j
n
j
n
j
n
j
n
j




 




2
)]([
2
)( 1
1
1
12
1111
Replacement by average value from surrounding grid points
The resulting difference equation is
Courant condition for Lax method
4
1
2



x
tD
( Second-order accuracy in both time and space )
23
Implementation (FTCS-Lax) Method
% Initial temperature of the wire: a
sinus.
for i = 1:n+1
x(i) =(i-1)*dx;
u(i,1) =sin(pi*x(i));
end
% Temperature at the boundary (T=0)
for k=1:maxk+1
u(1,k) = 0.;
u(n+1,k) = 0.;
time(k) = (k-1)*dt;
end
% Implementation of the explicit
method
for k=2:maxk % Time Loop
for i=2:n; % Space Loop
u(i,k+1) =0.5*(u(i+1,k)+u(i-1,k)) +
0.5*b*(u(i-1,k)+u(i+1,k)-2.*u(i,k));
end
end
2
2 0.1786D t
x
 

2
2 1
2
D t
x
 

Stability condition
2
2 0.0047D t
x
 

24
Implementation Lax method(CTCS)
2
2 0.3571D t
x
 

2
2 1
2
D t
x
 

Stability condition
% Initial temperature of the wire:
a sinus.
for i = 1:n+1
x(i) =(i-1)*dx;
u(i,1) =sin(pi*x(i));
end
% Temperature at the boundary (T=0)
for k=1:maxk+1
u(1,k) = 0.;
u(n+1,k) = 0.;
time(k) = (k-1)*dt;
end
% Implementation of the explicit
method
for k=2:maxk % Time Loop
for i=2:n; % Space Loop
u(i,k+1) =0.5*(u(i+1,k-1)+u(i-1,k-
1))+ 0.5*2*b*(u(i-1,k)+u(i+1,k)-
2.*u(i,k));
end
end
25
Implementation Lax method(CTCS)
26
Crank Nicolson Algorithm ( Implicit Method )
BTCS ( Backward time, centered space ) method for heat equation
)2( 11
1
2
n
j
n
j
n
jx
tn
j
n
j
n
j TTTtSTT 

 
( This is stable for any choice of time steps,
however it is first-order accurate in time. )
Crank-Nicolson scheme for heat equation
taking the average between time steps n-1 and n,

))22(
)(
1
1
11
111
1
2
11
2








n
j
n
j
n
j
n
j
n
j
n
jx
t
n
j
n
j
n
j
n
j
TTTTTT
SStTT

( This is stable for any choice of time steps and second-order accurate in time. )
a set of coupled linear equations for n
jT
27
Crank Nicolson Algorithm
Initial
conditions
Plot
Crank-
Nicolson
scheme
)/sin(),(
22
/
LxetxT Lt


Exact solution
28
Crank Nicolson Algorithm
29
Multiple Spatial Dimensions
),,(),,(),,( 2
2
2
2
tyxStyxT
yx
tyxT
t















FTCS for 2D heat equation
)2()2( 112112
1 n
jk
n
jk
n
jk
n
kj
n
jk
n
kj
n
jk
n
jk
n
jk TTT
y
t
TTT
x
t
tSTT 









Courant condition for this scheme
22
22
2
1
yx
yx
t



( Other schemes such as CTCS and Lax can be easily extended to multiple dimensions. )
30
Wave equation with nonuniform wave speed
),,(),(),,( 2
2
2
2
2
2
2
tyxz
yx
yxctyxz
t 













2D wave equation
0
),()0,,(),,()0,,(
00
00


vz
yxvyxzyxzyxz 
txxtxz
txztyztyz
2sin)1(),0,(
,0),1,(),,1(),,0(


Initial condition :
Boundary
condition :
 ])5.0(5)5.0(2exp[7.01),( 22
10
1
 yxyxcWave speed :
)2()2(2 112
22
112
22
11 n
jk
n
jk
n
jk
jkn
kj
n
jk
n
kj
jkn
jk
n
jk
n
jk TTz
y
tc
zzz
x
tc
zzz 








CTCS method for the wave equation :
)(
2
1
2
22



yxfor
tc
Courant condition :
31
Wave equation with nonuniform wave speed
Since evaluation of the nth timestep
refers back to the n-2nd step,
for the first step, a trick is employed.
jk
t
jkjkjk atvzz 20
01 2


)2()2( 112
2
0
1
00
12
2
n
jk
n
jk
n
jk
jk
kjjkkj
jk
jk TTz
y
c
zzz
x
c
a  




Since initial velocity and value,
0
0
1
0
0


jk
jkjk
z
zv
32
Wave equation with nonuniform wave speed
33
Wave equation with nonuniform wave speed
34
2D Poisson’s equation
),(),()( 2
2
2
2
yxyxyx
  



Poisson’s equation
Direct Solution for Poisson’s equation
jk
jkjkjkkjjkkj
yx







 
2
11
2
11 22
Centered-difference the spatial derivatives
35
Jacobi’s method ( Relaxation method )
Direct solution can be difficult to program efficiently.
Relaxation methods are relatively simple to code,
however, they are not as fast as the direct methods.
Idea :
I. Poisson’s equation can be thought of as the equilibrium solution to the heat
equation with source.
II. Starting with any initial condition, the heat equation solution will eventually
relax to a solution of Poisson’s equation.













 

)2(
1
)2(
1
2
1
11211222
22
1 n
jk
n
jk
n
jk
n
kj
n
jk
n
kjjk
n
jk
n
jk
yxyx
yx














  )(
1
)(
1
2
1
11211222
22
n
jk
n
jk
n
kj
n
kjjk
yxyx
yx

),(),(),,(
t
),(),( 22
yxyxtyxyxyx  


 )
2
1
( 22
22
yx
yx
t



FTCS
(Maximum time step satisfying Courant condition)
36
Jacobi method
37
Simultaneous OverRelaxation (SOR)
The convergence of the Jacobi method is quite slow.
Furthermore, the larger the system, the slower the convergence.
Simultaneous OverRelaxation (SOR) :
the Jacobi method is modified in two ways,



















)(
1
)(
1
2
)1(
1
112
1
112
22
22
1
n
jk
n
jk
n
kj
n
kjjk
n
jk
n
jk
yx
yx
yx



1. Improved values are used as soon as they become available.
2. Relaxation parameter ω tries to overshoot for going to the final result.
( 1<ω<2)
38
Simultaneous OverRelaxation (SOR)
39
MATLAB The Language of Technical Computing MATLAB PDE
Run: dftcs.m >> dftcs
dftcs - Program to solve the diffusion equation using the Forward Time Centered Space scheme.
Enter time step: 0.0001
Enter the number of grid points: 51
Solution is expected to be stable
2
2
),(),(
x
txT
t
txT






40
MATLAB The Language of Technical Computing MATLAB PDE
Run: dftcs.m >> dftcs
dftcs - Program to solve the diffusion equation using the Forward Time Centered Space scheme.
Enter time step: 0.00015
Enter the number of grid points: 61
WARNING: Solution is expected to be unstable
41
MATLAB The Language of Technical Computing MATLAB PDE
Run: neutrn.m >> neutrn Program to solve the neutron diffusion equation using the FTCS.
Enter time step: 0.0005
Enter the number of grid points: 61
Enter system length: 2 => System length is subcritical
Solution is expected to be stable
Enter number of time steps: 12000
),(
),(),(
2
2
txn
x
txn
t
txn
 





42
MATLAB The Language of Technical Computing MATLAB PDE
Run: neutrn.m >> neutrn Program to solve the neutron diffusion equation using the FTCS.
Enter time step: 0.0005
Enter the number of grid points: 61
Enter system length: 4 => System length is supercritical
Solution is expected to be stable
Enter number of time steps: 12000
43
MATLAB The Language of Technical Computing MATLAB PDE
Run: advect.m >> advect
advect - Program to solve the advection equation using the various hyperbolic PDE schemes:
FTCS, Lax, Lax-Wendorf
Enter number of grid points: 50
Time for wave to move one grid spacing is 0.02
Enter time step: 0.002
Wave circles system in 500 steps
Enter number of steps: 500
FTCS FTCS
0
),(),(






x
txu
t
txu

44
MATLAB The Language of Technical Computing MATLAB PDE
Run: advect.m >> advect
advect - Program to solve the advection equation using the various hyperbolic PDE schemes:
FTCS, Lax, Lax-Wendorf
Enter number of grid points: 50
Time for wave to move one grid spacing is 0.02
Enter time step: 0.02
Wave circles system in 50 steps
Enter number of steps: 50
Lax Lax
45
MATLAB The Language of Technical Computing MATLAB PDE
Run: relax.m >> relax
relax - Program to solve the Laplace equation using Jacobi, Gauss-Seidel and SOR methods on a
square grid
Enter number of grid points on a side: 50
Theoretical optimum omega = 1.88184
Enter desired omega: 1.8
Potential at y=L equals 1
Potential is zero on all other boundaries
Desired fractional change = 0.0001
0
),(),(
2
2
2
2






y
yx
x
yx 
46
47
The END
47

More Related Content

What's hot

Bessel equation
Bessel equationBessel equation
Bessel equation
Ni'am Fathonah
 
Runge Kutta Method
Runge Kutta MethodRunge Kutta Method
Runge Kutta Method
ch macharaverriyya naidu
 
Jacobi method
Jacobi methodJacobi method
Jacobi method
Grishma Maravia
 
Project on CFD using MATLAB
Project on CFD using MATLABProject on CFD using MATLAB
Project on CFD using MATLAB
Rohit Avadhani
 
Cubic Spline Interpolation
Cubic Spline InterpolationCubic Spline Interpolation
Cubic Spline Interpolation
VARUN KUMAR
 
Numerical Analysis and Its application to Boundary Value Problems
Numerical Analysis and Its application to Boundary Value ProblemsNumerical Analysis and Its application to Boundary Value Problems
Numerical Analysis and Its application to Boundary Value Problems
Gobinda Debnath
 
Introduction to tensor calculus
Introduction to tensor calculusIntroduction to tensor calculus
Introduction to tensor calculus
martin pomares calero
 
vector and tensor.pptx
vector and tensor.pptxvector and tensor.pptx
vector and tensor.pptx
Sourav Poddar
 
Fundamentals of Finite Difference Methods
Fundamentals of Finite Difference MethodsFundamentals of Finite Difference Methods
Fundamentals of Finite Difference Methods
1rj
 
Solve ODE - BVP through the Least Squares Method
Solve ODE - BVP through the Least Squares MethodSolve ODE - BVP through the Least Squares Method
Solve ODE - BVP through the Least Squares Method
Suddhasheel GHOSH, PhD
 
Finite DIfference Methods Mathematica
Finite DIfference Methods MathematicaFinite DIfference Methods Mathematica
Finite DIfference Methods Mathematica
guest56708a
 
NUMERICAL METHODS IN STEADY STATE, 1D and 2D HEAT CONDUCTION- Part-II
NUMERICAL METHODS IN STEADY STATE, 1D and 2D HEAT CONDUCTION- Part-IINUMERICAL METHODS IN STEADY STATE, 1D and 2D HEAT CONDUCTION- Part-II
NUMERICAL METHODS IN STEADY STATE, 1D and 2D HEAT CONDUCTION- Part-II
tmuliya
 
Numerical solution of ordinary differential equations GTU CVNM PPT
Numerical solution of ordinary differential equations GTU CVNM PPTNumerical solution of ordinary differential equations GTU CVNM PPT
Numerical solution of ordinary differential equations GTU CVNM PPT
Panchal Anand
 
Euler and runge kutta method
Euler and runge kutta methodEuler and runge kutta method
Euler and runge kutta method
ch macharaverriyya naidu
 
Numerical Solution of Diffusion Equation by Finite Difference Method
Numerical Solution of Diffusion Equation by Finite Difference MethodNumerical Solution of Diffusion Equation by Finite Difference Method
Numerical Solution of Diffusion Equation by Finite Difference Method
iosrjce
 
Gaussian quadratures
Gaussian quadraturesGaussian quadratures
Gaussian quadratures
Tarun Gehlot
 
Partial diferential good
Partial diferential goodPartial diferential good
Partial diferential good
genntmbr
 
Introduction to differential equation
Introduction to differential equationIntroduction to differential equation
Introduction to differential equation
Islamic University, Kushtia
 

What's hot (20)

Bessel equation
Bessel equationBessel equation
Bessel equation
 
Runge Kutta Method
Runge Kutta MethodRunge Kutta Method
Runge Kutta Method
 
Jacobi method
Jacobi methodJacobi method
Jacobi method
 
Project on CFD using MATLAB
Project on CFD using MATLABProject on CFD using MATLAB
Project on CFD using MATLAB
 
Cubic Spline Interpolation
Cubic Spline InterpolationCubic Spline Interpolation
Cubic Spline Interpolation
 
Numerical Analysis and Its application to Boundary Value Problems
Numerical Analysis and Its application to Boundary Value ProblemsNumerical Analysis and Its application to Boundary Value Problems
Numerical Analysis and Its application to Boundary Value Problems
 
Introduction to tensor calculus
Introduction to tensor calculusIntroduction to tensor calculus
Introduction to tensor calculus
 
vector and tensor.pptx
vector and tensor.pptxvector and tensor.pptx
vector and tensor.pptx
 
Fundamentals of Finite Difference Methods
Fundamentals of Finite Difference MethodsFundamentals of Finite Difference Methods
Fundamentals of Finite Difference Methods
 
Solve ODE - BVP through the Least Squares Method
Solve ODE - BVP through the Least Squares MethodSolve ODE - BVP through the Least Squares Method
Solve ODE - BVP through the Least Squares Method
 
Finite DIfference Methods Mathematica
Finite DIfference Methods MathematicaFinite DIfference Methods Mathematica
Finite DIfference Methods Mathematica
 
NUMERICAL METHODS IN STEADY STATE, 1D and 2D HEAT CONDUCTION- Part-II
NUMERICAL METHODS IN STEADY STATE, 1D and 2D HEAT CONDUCTION- Part-IINUMERICAL METHODS IN STEADY STATE, 1D and 2D HEAT CONDUCTION- Part-II
NUMERICAL METHODS IN STEADY STATE, 1D and 2D HEAT CONDUCTION- Part-II
 
Numerical solution of ordinary differential equations GTU CVNM PPT
Numerical solution of ordinary differential equations GTU CVNM PPTNumerical solution of ordinary differential equations GTU CVNM PPT
Numerical solution of ordinary differential equations GTU CVNM PPT
 
Euler and runge kutta method
Euler and runge kutta methodEuler and runge kutta method
Euler and runge kutta method
 
Numerical Solution of Diffusion Equation by Finite Difference Method
Numerical Solution of Diffusion Equation by Finite Difference MethodNumerical Solution of Diffusion Equation by Finite Difference Method
Numerical Solution of Diffusion Equation by Finite Difference Method
 
Gaussian quadratures
Gaussian quadraturesGaussian quadratures
Gaussian quadratures
 
Partial diferential good
Partial diferential goodPartial diferential good
Partial diferential good
 
Lagrange
LagrangeLagrange
Lagrange
 
Introduction to differential equation
Introduction to differential equationIntroduction to differential equation
Introduction to differential equation
 
Numerical Method 2
Numerical Method 2Numerical Method 2
Numerical Method 2
 

Similar to Computational Method to Solve the Partial Differential Equations (PDEs)

Numerical Methods
Numerical MethodsNumerical Methods
Numerical Methods
Teja Ande
 
Dsp Lab Record
Dsp Lab RecordDsp Lab Record
Dsp Lab Record
Aleena Varghese
 
Ray : modeling dynamic systems
Ray : modeling dynamic systemsRay : modeling dynamic systems
Ray : modeling dynamic systems
Houw Liong The
 
002 ray modeling dynamic systems
002 ray modeling dynamic systems002 ray modeling dynamic systems
002 ray modeling dynamic systems
Institute of Technology Telkom
 
002 ray modeling dynamic systems
002 ray modeling dynamic systems002 ray modeling dynamic systems
002 ray modeling dynamic systems
Institute of Technology Telkom
 
Ph 101-9 QUANTUM MACHANICS
Ph 101-9 QUANTUM MACHANICSPh 101-9 QUANTUM MACHANICS
Ph 101-9 QUANTUM MACHANICS
Chandan Singh
 
Low rank tensor approximation of probability density and characteristic funct...
Low rank tensor approximation of probability density and characteristic funct...Low rank tensor approximation of probability density and characteristic funct...
Low rank tensor approximation of probability density and characteristic funct...
Alexander Litvinenko
 
Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...
Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...
Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...
Alexander Litvinenko
 
Mechanical Engineering Assignment Help
Mechanical Engineering Assignment HelpMechanical Engineering Assignment Help
Mechanical Engineering Assignment Help
Matlab Assignment Experts
 
Wave function
Wave functionWave function
Wave function
Hassan Yousaf
 
SURF 2012 Final Report(1)
SURF 2012 Final Report(1)SURF 2012 Final Report(1)
SURF 2012 Final Report(1)Eric Zhang
 
KAUST_talk_short.pdf
KAUST_talk_short.pdfKAUST_talk_short.pdf
KAUST_talk_short.pdf
Chiheb Ben Hammouda
 
Introduction to Diffusion Monte Carlo
Introduction to Diffusion Monte CarloIntroduction to Diffusion Monte Carlo
Introduction to Diffusion Monte Carlo
Claudio Attaccalite
 
Mit2 092 f09_lec20
Mit2 092 f09_lec20Mit2 092 f09_lec20
Mit2 092 f09_lec20
Rahman Hakim
 
Fourier Specturm via MATLAB
Fourier Specturm via MATLABFourier Specturm via MATLAB
Fourier Specturm via MATLABZunAib Ali
 

Similar to Computational Method to Solve the Partial Differential Equations (PDEs) (20)

Numerical Methods
Numerical MethodsNumerical Methods
Numerical Methods
 
assignment_2
assignment_2assignment_2
assignment_2
 
Dsp Lab Record
Dsp Lab RecordDsp Lab Record
Dsp Lab Record
 
residue
residueresidue
residue
 
Ray : modeling dynamic systems
Ray : modeling dynamic systemsRay : modeling dynamic systems
Ray : modeling dynamic systems
 
002 ray modeling dynamic systems
002 ray modeling dynamic systems002 ray modeling dynamic systems
002 ray modeling dynamic systems
 
002 ray modeling dynamic systems
002 ray modeling dynamic systems002 ray modeling dynamic systems
002 ray modeling dynamic systems
 
Ph 101-9 QUANTUM MACHANICS
Ph 101-9 QUANTUM MACHANICSPh 101-9 QUANTUM MACHANICS
Ph 101-9 QUANTUM MACHANICS
 
wave_equation
wave_equationwave_equation
wave_equation
 
Low rank tensor approximation of probability density and characteristic funct...
Low rank tensor approximation of probability density and characteristic funct...Low rank tensor approximation of probability density and characteristic funct...
Low rank tensor approximation of probability density and characteristic funct...
 
Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...
Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...
Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...
 
Mechanical Engineering Assignment Help
Mechanical Engineering Assignment HelpMechanical Engineering Assignment Help
Mechanical Engineering Assignment Help
 
Wave function
Wave functionWave function
Wave function
 
Solution a ph o 3
Solution a ph o 3Solution a ph o 3
Solution a ph o 3
 
SURF 2012 Final Report(1)
SURF 2012 Final Report(1)SURF 2012 Final Report(1)
SURF 2012 Final Report(1)
 
05_AJMS_332_21.pdf
05_AJMS_332_21.pdf05_AJMS_332_21.pdf
05_AJMS_332_21.pdf
 
KAUST_talk_short.pdf
KAUST_talk_short.pdfKAUST_talk_short.pdf
KAUST_talk_short.pdf
 
Introduction to Diffusion Monte Carlo
Introduction to Diffusion Monte CarloIntroduction to Diffusion Monte Carlo
Introduction to Diffusion Monte Carlo
 
Mit2 092 f09_lec20
Mit2 092 f09_lec20Mit2 092 f09_lec20
Mit2 092 f09_lec20
 
Fourier Specturm via MATLAB
Fourier Specturm via MATLABFourier Specturm via MATLAB
Fourier Specturm via MATLAB
 

Recently uploaded

COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 

Recently uploaded (20)

COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 

Computational Method to Solve the Partial Differential Equations (PDEs)

  • 1. 1 Computational Method to Solve the Partial Differential Equations (PDEs) By Dr. Khurram Mehboob 1
  • 2. Mehboob, K NE: 402 computation Method in Nuclear Engineering Department of Nuclear Engineering, KAU, Jeddah, KSA 2017. 5. 1 How to solve 1st Order PDEs using MATLAB 2
  • 3. Contents • Solving PDEs using MATLAB (Explicit method) - FTCS method - Lax method - Crank Nicolson method - Jacobi’s method - Simultaneous-over-relaxation (SOR) method • Solving PDEs using MATLAB - Examples of PDEs 3
  • 4. PDE (Partial Differential Equation) PDEs are used as mathematical models for phenomena in all branches of engineering and science. 2 2 2 2 2 2 ( , , , , ) u u u u u A B C D x t u t xt x t x              u u and t x     linear in - Three Types of PDEs: 1) Elliptic:  Steady heat transfer, flow and diffusion 2) Parabolic:  Transient heat transfer, flow and diffusion 3) Hyperbolic:  Transient wave equation 2 0B AC  2 2 2 2 0u u x y      Laplace equations 2 0B AC  2 2 u uD t x     Diffusion Equation 2 0B AC  2 2 2 2 2 1u u x c t     Wave Equation 4
  • 5. Similarity method to solve PDE often the solution of a PDE together wit its initial and boundary problem depends upon its variables. In such cases the problem can be reduce to the combination ODEs with its independent variables The key to the similarity solution is the diffusing equation is that both the equations and initial and boundary conditions are under the scaling x→lx and t →l2x. Therefore, x/√t is independent transformation. In general the function should be of form The u(x,t)=U(z) 2 2 u u t x     , 0t x  ( , ) 1 0 ( , ) 0 0 u x t t u x t x     ( / )u t f x t   x t   2 2 1 0 2 U U        (0) 1 ( ) 0 U U    2 /4 0 ( ) s U C e ds D      2 /41( ) s U e ds        2 /4 / 1( , ) s x t u x t e ds      5
  • 6. FTCS method for Advection Eq Advection equation The Forward time and central space (FTCS) method implementation , 0,1........ 0,1........ ( , ) n o n j o n n j n j x x n x n N t t j x j t u u x t          , 1 , 1, 1, 2 n j n j n j n j u u u u c t x          0u uc t x      FTCS is explicit scheme , 1 , 1, 1, ( ) 2n j n j n j n j c tu u u u x       6
  • 7. Implementation in MATLAB FTCS is explicit scheme Square-wave Test for the Explicit Method to solve the Advection Equation clear; % Parameters to define the advection equation and the range in space and time Lmax = 1.0; % Maximum length Tmax = 1.0; % Maximum time c = 1.0; % Advection velocity % Parameters needed to solve the equation within the explicit method maxt = 3000; % Number of time steps dt = Tmax/maxt; n = 30; % Number of space steps nint=15; % The wave-front: intermediate point from which u=0 dx = Lmax/n; b = c*dt/(2.*dx); % Initial value of the function u (amplitude of the wave) for i = 1:(n+1) if i < nint u(i,1)=1.; else u(i,1)=0.; end x(i) =(i-1)*dx; end % Value of the amplitude at the boundary for k=1:maxt+1 u(1,k) = 1.; u(n+1,k) = 0.; time(k) = (k-1)*dt; end % Implementation of the explicit method for k=1:maxt % Time loop for i=2:n % Space loop u(i,k+1) =u(i,k)-b*(u(i+1,k)-u(i-1,k)); end end % Graphical representation of the wave at different selected times plot(x,u(:,1),'-',x,u(:,10),'-',x,u(:,50),'-',x,u(:,100),'-') title('Square-wave test within the Explicit Method I') xlabel('X') ylabel('Amplitude(X)') 0u uc t x      ( , ) 1 0 ( , ) 0 0 u x t t u x t x     7 0.025 2 c t x   
  • 8. FTSC Lax method for Advection Eq Advection equation To increase the stability of FTCS method simply talke the average of term u(n,j) , 0,1........ 0,1........ ( , ) n o n j o n n j n j x x n x n N t t j x j t u u x t          0u uc t x      FTCS is explicit scheme, 1 , 1, 1, ( ) 2n j n j n j n j c tu u u u x       1, 1, , ( ) 2 n j n j n j u u u     1, 1, , 1 1, 1, ( ) ( ) 2 2 n j n j n j n j n j u u c tu u u x           LEX method (explicit) 8
  • 9. Implementation in MATLAB LEX method is explicit scheme 0u uc t x      ( , ) 0 0 ( , ) 1 0u x t t u x t x    wave Test for the Lax method to solve the Advection Equation clear; % Parameters to define the advection equation and the range in space and time Lmax = 1.0; % Maximum length Tmax = 1.; % Maximum time c = 1.0; % Advection velocity % Parameters needed to solve the equation within the Lax method maxt = 350; % Number of time steps dt = Tmax/maxt; n = 300; % Number of space steps nint=50; % The wave-front: intermediate point from which u=0 (nint<n)!! dx = Lmax/n; b = c*dt/(2.*dx); % The Lax method is stable for abs(b)=< 1/2 but it gets difussed unless abs(b)= 1/2 % Initial value of the function u (amplitude of the wave) for i = 1:(n+1) if i < nint u(i,1)=1.; else u(i,1)=0.; end x(i) =(i-1)*dx; end % Value of the amplitude at the boundary at any time for k=1:maxt+1 u(1,k) = 1.; u(n+1,k) = 0.; time(k) = (k-1)*dt; end % Implementation of the Lax method for k=1:maxt % Time loop for i=2:n % Space loop u(i,k+1) =0.5*(u(i+1,k)+u(i-1,k))-b*(u(i+1,k)-u(i-1,k)); end end % Graphical representations of the evolution of the wave figure(1) mesh(x,time,u') title('Square-wave test within the Lax Method'); xlabel('X'); ylabel('T') figure(2) plot(x,u(:,1),'-',x,u(:,20),'-',x,u(:,50),'-',x,u(:,100),'-') title('Square-wave test within the Lax Method') xlabel('X') ylabel('Amplitude(X)') 9 1.01 2 c t x    1.0 2 c t x    0.5 2 c t x   
  • 10. CTCS method for Advection Advection equation The Forward time and central space (FTCS) method implementation , 0,1........ 0,1........ ( , ) n o n j o n n j n j x x n x n N t t j x j t u u x t          0u uc t x      Center point in time 2 2, 1 , 1 1, 1, ( ) ( ) 2 2 n j n j n j n j u u u uu uo t o x t t x x                 Center point in Space , 1 , 1 1, 1, 2 2 n j n j n j n ju u t u u c x          , 1 , 1 1, 1, 2 ( ) 2n j n j n j n j c tu u u u x        , 1 , 1 1, 1, ( )n j n j n j n j c tu u u u x        CTCS is also an explicit scheme 10
  • 11. CTCS Lax method for Advection Eq Advection equation To increase the stability of CTCS method simply take the average of term u(n,j-1) , 0,1........ 0,1........ ( , ) n o n j o n n j n j x x n x n N t t j x j t u u x t          0u uc t x      CTCS explicit scheme, 1 , 1 1, 1, ( )n j n j n j n j c tu u u u x        1, 1 1, 1 , 1 ( ) 2 n j n j n j u u u        1, 1 1, 1 , 1 1, 1, ( ) ( ) 2 n j n j n j n j n j u u c tu u u x             LEX CTCS method (explicit) , 1 , 1 1, 1, ( )n j n j n j n j c tu u u u x        11
  • 12. Implementation in MATLAB (CTCS) 0u uc t x      ( , ) 1 0 ( , ) 0 0 u x t t u x t x     % Matlab Program : Square-wave Test for the Explicit Method to solve the advection Equation clear; % Parameters to define the advection equation and the range in space and % time Lmax = 1.0; % Maximum length Tmax = 1.; % Maximum time c = 1.0; % Advection velocity % Parameters needed to solve the equation within the explicit method maxt = 100; % Number of time steps dt = Tmax/maxt; n = 100; % Number of space steps nint=15; % The wave-front: intermediate point from which u=0 dx = Lmax/n; b = c*dt/(dx) % Initial value of the function u (amplitude of the wave) for i = 1:(n+1) if i < nint u(i,1)=1.;else u(i,1)=0.; end x(i) =(i-1)*dx; end % Value of the amplitude at the boundary for k=2:maxt+1 u(1,k) = 1.; u(n+1,k) = 0.; time(k) = (k-1)*dt; end % Implementation of the explicit method for k=2:maxt % Time loop for i=2:n % Space loop u(i,k+1) =u(i,k-1)-b*(u(i+1,k)-u(i-1,k)); end end % Graphical representation of the wave at different selected times plot(x,u(:,10),'-',x,u(:,50),'-',x,u(:,100),'-',x,u(:,maxt),'-') title('Square-wave test within the Explicit Method') xlabel('X') ylabel('Amplitude(X)') 1c t x    0.5c t x    12
  • 13. Implementation in MATLAB (CTCS) 0u uc t x      ( , ) 1 0 ( , ) 0 0 u x t t u x t x     % Matlab Program : Square-wave Test for the Explicit Method to solve the advection Equation clear; % Parameters to define the advection equation and the range in space and % time Lmax = 1.0; % Maximum length Tmax = 1.; % Maximum time c = 1.0; % Advection velocity % Parameters needed to solve the equation within the explicit method maxt = 100; % Number of time steps dt = Tmax/maxt; n = 100; % Number of space steps nint=15; % The wave-front: intermediate point from which u=0 dx = Lmax/n; b = c*dt/(dx) % Initial value of the function u (amplitude of the wave) for i = 1:(n+1) if i < nint u(i,1)=1.;else u(i,1)=0.; end x(i) =(i-1)*dx; end % Value of the amplitude at the boundary for k=2:maxt+1 u(1,k) = 1.; u(n+1,k) = 0.; time(k) = (k-1)*dt; end % Implementation of the explicit method for k=2:maxt % Time loop for i=2:n % Space loop u(i,k+1) =0.5*(u(i+1,k-1)+u(i-1,k-1))-b*(u(i+1,k)-u(i-1,k)); end end % Graphical representation of the wave at different selected times plot(x,u(:,10),'-',x,u(:,50),'-',x,u(:,100),'-',x,u(:,maxt),'-') title('Square-wave test within the Explicit Method') xlabel('X') ylabel('Amplitude(X)') 0.5c t x    1c t x    13
  • 14. Mehboob, K NE: 402 computation Method in Nuclear Engineering Department of Nuclear Engineering, KAU, Jeddah, KSA 2017. 5. 3 How to solve 2nd Order PDEs using MATLAB 14
  • 16. FTCS method for the heat equation FTCS ( Forward Euler in Time and Central difference in Space ) ):),(),(),(),0(( ),(),(),( 21 2 2 ydiffusivitthermalDtutLututu txStxu x Dtxu t        Heat equation in a slab ),( 1 t uu t txu n j n jnj       2 11 2 2 2),( x uuu x txu n j n j n jnj       n jnj utxu ),(Let Forward Euler in Time 2nd order Central difference in Space 1 2 1 1 ( 2 )n n n n n n j j j j j j D tu u tS u u u x          1 1 1 2 2 ( , ) n n n n n j j j j j u u u u u D s x t t x           FTCS explicit scheme for diffusion Equation 16
  • 17. Implementation in MATLAB (FTCS heat equation) % Heat Diffusion in one dimensional wire within the % Explicit Method clear; % Parameters to define the heat equation and the range in space and time L = 1.; % Length of the wire T =1.; % Final time % Parameters needed to solve the equation within the explicit method maxk = 2500; % Number of time steps dt = T/maxk; n = 50; % Number of space steps dx = L/n; cond = 1/4; % Conductivity b = cond*dt/(dx*dx); % Stability parameter (b=<1) % Initial temperature of the wire: a sinus. for i = 1:n+1 x(i) =(i-1)*dx; u(i,1) =sin(pi*x(i)); end % Temperature at the boundary (T=0) for k=1:maxk+1 u(1,k) = 0.; u(n+1,k) = 0.; time(k) = (k-1)*dt; end % Implementation of the explicit method for k=1:maxk % Time Loop for i=2:n; % Space Loop u(i,k+1) =u(i,k) + b*(u(i-1,k)+u(i+1,k)-2.*u(i,k)); end end % Graphical representation of the temperature at different selected times figure(1) plot(x,u(:,1),'-',x,u(:,100),'-',x,u(:,300),'-',x,u(:,600),'-') title('Temperature within the explicit method') xlabel('X') ylabel('T') figure(2) mesh(x,time,u') title('Temperature within the explicit method') xlabel('X') ylabel('Temperature') ),( ),(),( 2 2 txS x txu D t txu       )sin()0,( 0),(,0),(,0),0( xxu conditionsinitial txstLutu conditionsBoundry   17
  • 18. Implementation in MATLAB (FTCS heat equation) Courant condition for FTCS 1 2 2    x tD 2 2 1.01D t x   2 2 1 2 D t x    18
  • 19. CTCS method for the heat equation FTCS ( Forward Euler in Time and Central difference in Space ) ):),(),(),(),0(( ),(),(),( 21 2 2 ydiffusivitthermalDtutLututu txStxu x Dtxu t        Heat equation in a slab 2 ),( 11 t uu t txu n j n jnj       2 11 2 2 2),( x uuu x txu n j n j n jnj       n jnj utxu ),(Let Center difference in Time 2nd order Central difference in Space 1 1 2 1 1 22 ( 2 )n n n n n n j j j j j j D tu u tS u u u x            1 1 1 1 2 2 ( , ) 2 n n n n n j j j j j u u u u u D s x t t x            FTCS explicit scheme for diffusion Equation 19
  • 20. CTCS method for the heat equation% Heat Diffusion in one dimensional wire within the % Explicit Method clear; clc; % Parameters to define the heat equation and the range in space and time L = 1.; % Length of the wire T =1.; % Final time % Parameters needed to solve the equation within the explicit method maxk = 600; % Number of time steps dt = T/maxk; n = 20; % Number of space steps dx = L/n; cond = 1/132; % Conductivity b =2.*cond*dt/(dx*dx) % Stability parameter (b=<0.01) % Initial temperature of the wire: a sinus. for i = 1:n+1 x(i) =(i-1)*dx; u(i,1) =sin(pi*x(i)); end % Temperature at the boundary (T=0) for k=1:maxk+1 u(1,k) = 0.; u(n+1,k) = 0.; time(k) = (k-1)*dt; end % Implementation of the explicit method for k=2:maxk % Time Loop for i=2:n; % Space Loop u(i,k+1) =u(i,k-1) + 0.5*2*b*(u(i-1,k)+u(i+1,k)-2.*u(i,k)); end end % Graphical representation of the temperature at different selected times figure(1) plot(x,u(:,1),'-',x,u(:,10),'-',x,u(:,30),'-',x,u(:,60),'-') title('Temperature within the explicit method') xlabel('X') ylabel('T') figure(2) mesh(x,time,u') title('Temperature within the explicit method') xlabel('X') ylabel('Temperature') ),( ),(),( 2 2 txS x txu D t txu       )sin()0,( 0),(,0),(,0),0( xxu conditionsinitial txstLutu conditionsBoundry   0126.0 2 2    x tD 20
  • 21. Stability of FTCS and CTCS FTCS is first-order accuracy in time and second-order accuracy in space. So small time steps are required to achieve reasonable accuracy. Courant condition for FTCS 2 1 2    x tD )2( 2 2 112 11 n j n j n j n j n j n j uuu x tD tSuu       CTCS method for heat equation (Both the time and space derivatives are center-differenced.) However, CTCS method is unstable for any time step size. 2 2 1 2 D t x    2 2 0.0126D t x    CTCS (unstable) FTCS (Stable) 21
  • 22. Lax method (FTCS) Simple modification to the FTCS method In the differenced time derivative, )2()( 112112 11 n j n j n j n j n j n j n j uuu x tD tSuuu       t uuu t uu n j n j n j n j n j        )]([)( 112 111 Replacement by average value from surrounding grid points The resulting difference equation is Courant condition for Lax method 4 1 2    x tD ( Second-order accuracy in both time and space ) 22
  • 23. Lax method(CTCS) Simple modification to the CTCS method In the differenced time derivative, )2( 2 2)( 112 1 1 1 12 11 n j n j n j n j n j n j n j uuu x tD tSuuu           t uuu t uu n j n j n j n j n j           2 )]([ 2 )( 1 1 1 12 1111 Replacement by average value from surrounding grid points The resulting difference equation is Courant condition for Lax method 4 1 2    x tD ( Second-order accuracy in both time and space ) 23
  • 24. Implementation (FTCS-Lax) Method % Initial temperature of the wire: a sinus. for i = 1:n+1 x(i) =(i-1)*dx; u(i,1) =sin(pi*x(i)); end % Temperature at the boundary (T=0) for k=1:maxk+1 u(1,k) = 0.; u(n+1,k) = 0.; time(k) = (k-1)*dt; end % Implementation of the explicit method for k=2:maxk % Time Loop for i=2:n; % Space Loop u(i,k+1) =0.5*(u(i+1,k)+u(i-1,k)) + 0.5*b*(u(i-1,k)+u(i+1,k)-2.*u(i,k)); end end 2 2 0.1786D t x    2 2 1 2 D t x    Stability condition 2 2 0.0047D t x    24
  • 25. Implementation Lax method(CTCS) 2 2 0.3571D t x    2 2 1 2 D t x    Stability condition % Initial temperature of the wire: a sinus. for i = 1:n+1 x(i) =(i-1)*dx; u(i,1) =sin(pi*x(i)); end % Temperature at the boundary (T=0) for k=1:maxk+1 u(1,k) = 0.; u(n+1,k) = 0.; time(k) = (k-1)*dt; end % Implementation of the explicit method for k=2:maxk % Time Loop for i=2:n; % Space Loop u(i,k+1) =0.5*(u(i+1,k-1)+u(i-1,k- 1))+ 0.5*2*b*(u(i-1,k)+u(i+1,k)- 2.*u(i,k)); end end 25
  • 27. Crank Nicolson Algorithm ( Implicit Method ) BTCS ( Backward time, centered space ) method for heat equation )2( 11 1 2 n j n j n jx tn j n j n j TTTtSTT     ( This is stable for any choice of time steps, however it is first-order accurate in time. ) Crank-Nicolson scheme for heat equation taking the average between time steps n-1 and n,  ))22( )( 1 1 11 111 1 2 11 2         n j n j n j n j n j n jx t n j n j n j n j TTTTTT SStTT  ( This is stable for any choice of time steps and second-order accurate in time. ) a set of coupled linear equations for n jT 27
  • 30. Multiple Spatial Dimensions ),,(),,(),,( 2 2 2 2 tyxStyxT yx tyxT t                FTCS for 2D heat equation )2()2( 112112 1 n jk n jk n jk n kj n jk n kj n jk n jk n jk TTT y t TTT x t tSTT           Courant condition for this scheme 22 22 2 1 yx yx t    ( Other schemes such as CTCS and Lax can be easily extended to multiple dimensions. ) 30
  • 31. Wave equation with nonuniform wave speed ),,(),(),,( 2 2 2 2 2 2 2 tyxz yx yxctyxz t               2D wave equation 0 ),()0,,(),,()0,,( 00 00   vz yxvyxzyxzyxz  txxtxz txztyztyz 2sin)1(),0,( ,0),1,(),,1(),,0(   Initial condition : Boundary condition :  ])5.0(5)5.0(2exp[7.01),( 22 10 1  yxyxcWave speed : )2()2(2 112 22 112 22 11 n jk n jk n jk jkn kj n jk n kj jkn jk n jk n jk TTz y tc zzz x tc zzz          CTCS method for the wave equation : )( 2 1 2 22    yxfor tc Courant condition : 31
  • 32. Wave equation with nonuniform wave speed Since evaluation of the nth timestep refers back to the n-2nd step, for the first step, a trick is employed. jk t jkjkjk atvzz 20 01 2   )2()2( 112 2 0 1 00 12 2 n jk n jk n jk jk kjjkkj jk jk TTz y c zzz x c a       Since initial velocity and value, 0 0 1 0 0   jk jkjk z zv 32
  • 33. Wave equation with nonuniform wave speed 33
  • 34. Wave equation with nonuniform wave speed 34
  • 35. 2D Poisson’s equation ),(),()( 2 2 2 2 yxyxyx       Poisson’s equation Direct Solution for Poisson’s equation jk jkjkjkkjjkkj yx          2 11 2 11 22 Centered-difference the spatial derivatives 35
  • 36. Jacobi’s method ( Relaxation method ) Direct solution can be difficult to program efficiently. Relaxation methods are relatively simple to code, however, they are not as fast as the direct methods. Idea : I. Poisson’s equation can be thought of as the equilibrium solution to the heat equation with source. II. Starting with any initial condition, the heat equation solution will eventually relax to a solution of Poisson’s equation.                 )2( 1 )2( 1 2 1 11211222 22 1 n jk n jk n jk n kj n jk n kjjk n jk n jk yxyx yx                 )( 1 )( 1 2 1 11211222 22 n jk n jk n kj n kjjk yxyx yx  ),(),(),,( t ),(),( 22 yxyxtyxyxyx      ) 2 1 ( 22 22 yx yx t    FTCS (Maximum time step satisfying Courant condition) 36
  • 38. Simultaneous OverRelaxation (SOR) The convergence of the Jacobi method is quite slow. Furthermore, the larger the system, the slower the convergence. Simultaneous OverRelaxation (SOR) : the Jacobi method is modified in two ways,                    )( 1 )( 1 2 )1( 1 112 1 112 22 22 1 n jk n jk n kj n kjjk n jk n jk yx yx yx    1. Improved values are used as soon as they become available. 2. Relaxation parameter ω tries to overshoot for going to the final result. ( 1<ω<2) 38
  • 40. MATLAB The Language of Technical Computing MATLAB PDE Run: dftcs.m >> dftcs dftcs - Program to solve the diffusion equation using the Forward Time Centered Space scheme. Enter time step: 0.0001 Enter the number of grid points: 51 Solution is expected to be stable 2 2 ),(),( x txT t txT       40
  • 41. MATLAB The Language of Technical Computing MATLAB PDE Run: dftcs.m >> dftcs dftcs - Program to solve the diffusion equation using the Forward Time Centered Space scheme. Enter time step: 0.00015 Enter the number of grid points: 61 WARNING: Solution is expected to be unstable 41
  • 42. MATLAB The Language of Technical Computing MATLAB PDE Run: neutrn.m >> neutrn Program to solve the neutron diffusion equation using the FTCS. Enter time step: 0.0005 Enter the number of grid points: 61 Enter system length: 2 => System length is subcritical Solution is expected to be stable Enter number of time steps: 12000 ),( ),(),( 2 2 txn x txn t txn        42
  • 43. MATLAB The Language of Technical Computing MATLAB PDE Run: neutrn.m >> neutrn Program to solve the neutron diffusion equation using the FTCS. Enter time step: 0.0005 Enter the number of grid points: 61 Enter system length: 4 => System length is supercritical Solution is expected to be stable Enter number of time steps: 12000 43
  • 44. MATLAB The Language of Technical Computing MATLAB PDE Run: advect.m >> advect advect - Program to solve the advection equation using the various hyperbolic PDE schemes: FTCS, Lax, Lax-Wendorf Enter number of grid points: 50 Time for wave to move one grid spacing is 0.02 Enter time step: 0.002 Wave circles system in 500 steps Enter number of steps: 500 FTCS FTCS 0 ),(),(       x txu t txu  44
  • 45. MATLAB The Language of Technical Computing MATLAB PDE Run: advect.m >> advect advect - Program to solve the advection equation using the various hyperbolic PDE schemes: FTCS, Lax, Lax-Wendorf Enter number of grid points: 50 Time for wave to move one grid spacing is 0.02 Enter time step: 0.02 Wave circles system in 50 steps Enter number of steps: 50 Lax Lax 45
  • 46. MATLAB The Language of Technical Computing MATLAB PDE Run: relax.m >> relax relax - Program to solve the Laplace equation using Jacobi, Gauss-Seidel and SOR methods on a square grid Enter number of grid points on a side: 50 Theoretical optimum omega = 1.88184 Enter desired omega: 1.8 Potential at y=L equals 1 Potential is zero on all other boundaries Desired fractional change = 0.0001 0 ),(),( 2 2 2 2       y yx x yx  46