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

PPTX
TWO DIMENSIONAL STEADY STATE HEAT CONDUCTION
PDF
10 متشابهات في سورة يونس
PDF
Cac lenh trong matlab
PDF
Heat transfer from extended surfaces (or fins)
PPT
Basic Statistical Concepts and Methods
PPTX
Message digest 5
PDF
MATLAB Programming
TWO DIMENSIONAL STEADY STATE HEAT CONDUCTION
10 متشابهات في سورة يونس
Cac lenh trong matlab
Heat transfer from extended surfaces (or fins)
Basic Statistical Concepts and Methods
Message digest 5
MATLAB Programming

What's hot (20)

PPTX
Finite difference method
PDF
FINITE DIFFERENCE MODELLING FOR HEAT TRANSFER PROBLEMS
TXT
Finite difference Matlab Code
PPTX
NUMERICAL INTEGRATION AND ITS APPLICATIONS
PPT
Fourier series
PPTX
A brief introduction to finite difference method
PDF
NUMERICAL METHODS IN STEADY STATE, 1D and 2D HEAT CONDUCTION- Part-II
PPTX
Finite difference equation
DOCX
Guía 3 Análisis y semejanza dimensional
PPT
Application of Numerical Methods (Finite Difference) in Heat Transfer
PDF
Initial Value Problems
PPT
Application of Gauss,Green and Stokes Theorem
DOCX
Ejercicios raíces de funciones
PPTX
trapezoidal and simpson's 1/3 and 3/8 rule
PDF
Integral table
PPT
Chapter 5 NUMERICAL METHODS IN HEAT CONDUCTION
PDF
Project on CFD using MATLAB
PDF
Fundamentals of Finite Difference Methods
 
PDF
Numerical methods in Transient-heat-conduction
PDF
Gaussian quadratures
Finite difference method
FINITE DIFFERENCE MODELLING FOR HEAT TRANSFER PROBLEMS
Finite difference Matlab Code
NUMERICAL INTEGRATION AND ITS APPLICATIONS
Fourier series
A brief introduction to finite difference method
NUMERICAL METHODS IN STEADY STATE, 1D and 2D HEAT CONDUCTION- Part-II
Finite difference equation
Guía 3 Análisis y semejanza dimensional
Application of Numerical Methods (Finite Difference) in Heat Transfer
Initial Value Problems
Application of Gauss,Green and Stokes Theorem
Ejercicios raíces de funciones
trapezoidal and simpson's 1/3 and 3/8 rule
Integral table
Chapter 5 NUMERICAL METHODS IN HEAT CONDUCTION
Project on CFD using MATLAB
Fundamentals of Finite Difference Methods
 
Numerical methods in Transient-heat-conduction
Gaussian quadratures
Ad

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

PPT
Numerical Methods
PDF
assignment_2
PDF
Dsp Lab Record
PDF
residue
PPT
002 ray modeling dynamic systems
PPT
Ray : modeling dynamic systems
PPT
002 ray modeling dynamic systems
PPT
Ph 101-9 QUANTUM MACHANICS
PDF
wave_equation
PDF
Adv. Digital Signal Processing LAB MANUAL.pdf
PDF
Low rank tensor approximation of probability density and characteristic funct...
PDF
Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...
PPTX
Mechanical Engineering Assignment Help
PPTX
Wave function
DOC
Solution a ph o 3
PDF
SURF 2012 Final Report(1)
PDF
05_AJMS_332_21.pdf
PDF
KAUST_talk_short.pdf
PDF
Presentation Sampling adn Reconstruction.pdf
PDF
Mit2 092 f09_lec20
Numerical Methods
assignment_2
Dsp Lab Record
residue
002 ray modeling dynamic systems
Ray : modeling dynamic systems
002 ray modeling dynamic systems
Ph 101-9 QUANTUM MACHANICS
wave_equation
Adv. Digital Signal Processing LAB MANUAL.pdf
Low rank tensor approximation of probability density and characteristic funct...
Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...
Mechanical Engineering Assignment Help
Wave function
Solution a ph o 3
SURF 2012 Final Report(1)
05_AJMS_332_21.pdf
KAUST_talk_short.pdf
Presentation Sampling adn Reconstruction.pdf
Mit2 092 f09_lec20
Ad

Recently uploaded (20)

PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Geodesy 1.pptx...............................................
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
web development for engineering and engineering
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PPT
Mechanical Engineering MATERIALS Selection
PDF
composite construction of structures.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Construction Project Organization Group 2.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
PPT on Performance Review to get promotions
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Geodesy 1.pptx...............................................
UNIT-1 - COAL BASED THERMAL POWER PLANTS
web development for engineering and engineering
CH1 Production IntroductoryConcepts.pptx
Lecture Notes Electrical Wiring System Components
Mechanical Engineering MATERIALS Selection
composite construction of structures.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Foundation to blockchain - A guide to Blockchain Tech
Embodied AI: Ushering in the Next Era of Intelligent Systems
Internet of Things (IOT) - A guide to understanding
Construction Project Organization Group 2.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Model Code of Practice - Construction Work - 21102022 .pdf
R24 SURVEYING LAB MANUAL for civil enggi
PPT on Performance Review to get promotions
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Automation-in-Manufacturing-Chapter-Introduction.pdf

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