P a g e | 1
Partial Differential Equations (PDEs) and the Finite Difference Method (FDM). An introduction
Original: May 7, 2015
Revised: Dec 19, 2016, Feb 20, 2017
The FDM
The main feature of the finite difference method is to obtain discrete equations by replacing derivatives
and other elements within the equation with appropriate finite divided differences and discrete
approximations. We derive and solve a finite difference system for the PDE in five steps.
1. Discretization of the domain of the problem
2. Discretization of the differential equation at the interior nodes
3. Discretization of the boundary conditions.
4. Construction of the system of equations and/or the matrix form.
5. Solving the equations
Many times the system of linear equations is tridiagonal and the solution of tridiagonal linear systems is
a very well-studied problem. Other times we do not need to solve a matrix.
The Heat Conduction Equation in One-Dimension
One of the simplest PDEs to learn the numerical solution process is the problem posed by the heat
conduction equation in one dimension:
Eq-1
The โToyโ Problem Description-A Quick-Example
For the Heat Equation above, a parabolic PDE governing the temperature distribution of a thin rod
insulated at all points, except at its ends with the following specifications:
โข L = 3 cm (rod length)
โข โx = 1 cm, โt = 0.1 s, k = 0.835 cm2/s (thermal diffusivity), and
โข ฮป = ๐โ๐ก/โ๐ฅ2=0.835(0.1)/(1)2 = 0.0835
P a g e | 2
At t = 0, the temperature of interior nodes of the rod is zero and the boundary conditions are fixed for
all times. These can be expressed as
โข U(x,0) = 0 oC for 0<x<3
โข U(0,t) = 5 oC for t>0
โข U(3,t) = 10 oC for t>0
All following methods below refer to the same problem description above
Discretize the domain of the problem. For two independent variables use a mesh grid, each axis
representing one of them. Use as running indices, i for space and k for time, e.g., i=0,1,2,โฆ ix, and
k=0,1,2,โฆ,kx.
A more detailed discretization showing the computational molecule (dashed shape) for the FTCS
method (to be discussed later):
P a g e | 3
Discuss: A good discretization should include a cartoon representation of the number of grid nodes,
the step size, indexed nodes, and the computational molecule.
At a given time point, how many times the computational molecule fits into the grid?
How many unknowns must be solved at each given time point? How many equations do you
need?
Regarding the discrete equations is there any difference if the running index starts at 0 or at 1?
What is the range of the running index?
What specific indexed variables correspond to boundary conditions (BCs)? How the indices
change if they start at 0 or at 1?
Next is the description of the three main methods used for numerical solution of the heat equation
Forward in time, centered in space (FTCS) Solution. This method is also called forward Euler method.
Using the domain discretization described above, the steps toward the solution are:
(1) Discretize the differential equation
a. Approximate the 1st order time derivative with forward finite difference approximation
of ๐(โ๐ก) at the grid point (i,k):
๐๐
๐๐ก|
๐,๐=
๐๐๐พ+1 โ ๐๐
๐พ
โ๐ก
Notice the presence of indices (i,k) at the LHS derivative operator. These means the
discretization is at the node (i,k).
b. Approximate the 2nd order space derivative with central difference approximation of
๐(โ๐ฅ2) at the grid point (i,k):
๐2๐
๐๐ฅ2|
๐,๐
=๐๐+1
๐พ โ 2๐๐๐พ + ๐๐โ1
๐พ
โ๐ฅ2
P a g e | 4
c. Plug (a) and (b) results in the original equation (Eq-1) to find the recurrence formula:
๐๐๐พ+1 โ ๐๐
๐พ
โ๐ก= ๐
๐๐+1๐พ โ 2๐๐
๐พ + ๐๐โ1๐พ
โ๐ฅ2
which yields the recurrence formula:
๐๐๐พ+1 = ๐๐
๐พ + ๐(๐๐+1๐พ โ 2๐๐
๐พ + ๐๐โ1๐พ ), where ๐ = ๐โ๐ก/โ๐ฅ2 Eq #4a
Grouping variables with same indices offer another way to express the recurrence formula which is
more suitable for a computer program:
๐๐๐พ+1 = ๐๐๐โ1
๐พ + (1 โ 2๐)๐๐๐พ + ๐๐๐+1
๐พ Eq #4b
The recurrence formula is written for all the interior nodes on the rod by properly setting values of i and
K. It then provides an explicit means to compute values at each node for a future time (K+1) based on
the present values (K) at the node and its neighbors. Notice that this approach is actually an extension
of Eulerโs method for solving the ODE at several nodes in space. That is, if we know the temperature
distribution as a function of position at an initial time, we can compute the distribution at a future time
based on Equation #4.
(2) Discretize the initial and boundary conditions (i.e., express the discrete variables involved with
right indices):
Original Mathematical Expression Discretized
๐(0, ๐ก) = 5 ๐๐๐ = 5 for ๐ โฅ 0 (๐. ๐., ๐ = 0,1,2,3, โฆ )
๐(3, ๐ก) = 10 ๐3๐ = 10 for ๐ โฅ 0 (๐. ๐., ๐ = 0,1,2,3, โฆ)
๐(๐ฅ, 0) = 0 ๐๐0 = 0 for 0 < ๐ < 3 (๐. ๐, ๐ = 1,2)
(3) Show the FTCS computational molecule within the grid of your discretization (see discretization
above)
(4) Develop the equations to be solved According to the problem discretization and the number of
unknowns, and using the recurrence formulation (Eq #4b), find the appropriate number of
equations for the solutions at the first and second time steps. Please use algebraic expressions
with indixed variables, NO numbers yet:
For the first time step [t1=0.1]:
i=1, k=0:
๐11 = ๐1
0 + ๐(๐20 โ 2๐1
0 + ๐00),
i=2, k=0:
๐21 = ๐2
0 + ๐(๐30 โ 2๐2
0 + ๐10),
For the second time step [t2=0.2]:
P a g e | 5
i=1, k=1:
๐12 = ๐1
1 + ๐(๐21 โ 2๐1
1 + ๐01)
i=2, k=1:
๐22 = ๐2
1 + ๐(๐31 โ 2๐2
1 + ๐11)
Quiz:
Locate each ๐๐๐ in the above equations within its computational molecule and the mesh grid.
How do you give specificic values to i- and K-indices in the recurrence formula to arrive to the
above equations. Realize, that after given the correct values for i and K, each ๐๐๐พin the equation
is represented in the computational molecule (cm) ; each element is bounded, i.e., no ๐๐๐พ is
outside the cm. For instance, study the following figure for the first equation above (i.e., ๐11):
(5) Plugging numbers in the equations developed in (4), solve for two time steps (i.e.,[ t1 ,U(xi, t1 ),
and [t2 ,U(xi, t2 )]:
For the first time step [t1=0.1]:
๐11 = ๐1
0 + ๐(๐20 โ 2๐1
0 + ๐00) = 0 + 0.0835(0 โ 2(0) + 5) = 0.4175
๐21 = ๐2
0 + ๐(๐30 โ 2๐2
0 + ๐10) = 0 + 0.0835(10 โ 2(0) + 0) = 0.835
For the second time step [t2=0.2]:
P a g e | 6
๐12 = ๐1
1 + ๐(๐21 โ 2๐1
1 + ๐01) = 0.4175 + 0.0835(0.835 โ 2(0.4175) + 5) = 0.835
๐22 = ๐2
1 + ๐(๐31 โ 2๐2
1 + ๐11) = 0.835 + 0.0835(10 โ 2(0.835) + 0.4175) = 1.565416
(6) Express the results on a table (critical step). For example for the FTCS computation we can use
Excel (see the file: FDM with Excel):
Due to โtop to downโ custom calculations, notice the computational molecule โup-side-downโ
when compared to the mesh grid.
(7) Life Saver Matlab code. A better tool is a matlab code (see Appendix one).
(8) Convergence & Stability: Convergence means that as โx and โt approach zero, the results of the
finite-difference technique approach the true solution. Stability means that errors at any stage
of the computation are not amplified but are attenuated as the computations progress.
It can be shown that the explicit method is both convergent and stable if: ๐ = ๐ ฮ๐ก/ฮ๐ฅ2 โค 1/2
or โ๐ก โค1
2
โ๐ฅ2
๐ (this means you canโt march the solution in a time faster than
1
2
โ๐ฅ2
๐ )
Crank Nicolson Method (CNM). Now we deal with the CNM as the more accurate and popular method
used to solve this type of equation. Use the same problem domain discretization as above.
(1) Discretization of the Differential equation. Evaluation of the ๐2๐
๐๐ฅ2 at both time steps ๐ก๐+1 and
time step ๐ก๐ and doing a weighted average with the ๐ factor yields the following:
๐๐๐+1 โ ๐๐
๐
โ๐ก= ๐๐ [
๐๐โ1๐+1 โ 2๐๐
๐+1 + ๐๐+1๐+1
โ๐ฅ2] + (1 โ ๐)๐ [
๐๐โ1๐ โ 2๐๐
๐ + ๐๐+1๐
โ๐ฅ2]
a. The recurrence formula with ๐ = 1/2 is:
โ (1
2) ๐๐๐โ1
๐+1 + (1 + ๐)๐๐๐+1 โ (
1
2) ๐๐๐+1
๐+1 = (1
2) ๐๐๐โ1
๐ + (1 โ ๐)๐๐๐ + (
1
2) ๐๐๐+1
๐
the above equation times 2, simplifies to:
โ๐๐๐โ1๐+1 + 2(1 + ๐)๐๐
๐+1 โ ๐๐๐+1๐+1 = ๐๐๐โ1
๐ + 2(1 โ ๐)๐๐๐ + ๐๐๐+1
๐
NOTE: You donโt need to rename coefficients as they are simple enough.
x(0)=0 x(1)=1 x(2)=2 x(3)=3
K\i ==> 0 1 2 3
t(0)=0 0 5 0 0 10
t(1)=0.1 1 5 0.4175 0.835 10
t(2)=0.2 2 5 0.835 1.565416 10
t(3)=0.3 3 5 1.243767 2.208714 10
t(4)=0.4 4 5 1.637986 2.778714 10
P a g e | 7
Where the coefficients for the current problem are:
2(1 + 0.0835) = 2.167
2(1 โ 0.0835) = 1.833
๐ = 0.0835
(2) Discretize the initial and boundary conditions (This is the same as FTCS method).
(3) Show the CN computational molecule within the grid of your discretization.
(4) According to your problem discretization and the number of unknowns find the appropriate
number of equations to solve for the first time step. We use indexed-variable algebraic
equationsโNot numbers yet:
For the time ๐ก1, there are two unknowns, ๐ข11 and ๐ข2
1, therefore, we need two equations to solve
them:
Set i=1 and k=0 in the recurrence formula:
โ๐๐01 + 2(1 + ๐)๐1
1 โ ๐๐21 = ๐๐0
0 + 2(1 โ ๐)๐10 + ๐๐2
0
Notice that with these two indices values i=1 and k=0 we produced the two unknowns ๐11
and ๐21of the computational molecule.
Set i=2 and k=0 in the recurrence formula:
โ๐๐11 + 2(1 + ๐)๐2
1 โ ๐๐31 = ๐๐1
0 + 2(1 โ ๐)๐20 + ๐๐3
0
Notice again that with these two indices values i=2 and k=0 we produced the two unknowns ๐11
and ๐21of the computational molecule, while the rest of ๐๐
๐พare all known.
As the two terms ๐01 = 5 and ๐3
1 = 10 in the first and second equations above are knowns and
constant along the BCs, you must jump them to the RHS of the equation:
P a g e | 8
2(1 + ๐)๐11 โ ๐๐2
1 = ๐๐00 + 2(1 โ ๐)๐1
0 + ๐๐20 + ๐๐0
1
โ๐๐11 + 2(1 + ๐)๐2
1 = ๐๐10 + 2(1 โ ๐)๐2
0 + ๐๐30 + ๐๐3
1
(5) Express the problem with numbers for a clean system of linear equations and also in matrix
form:
2.167๐11 โ 0.0835๐2
1 = 0.0835(5) + 1.833(0) + 0.0835(0) + 0.0835(5) = 0.835
โ0.0835๐11 + 2.167๐2
1 = 0.0835(0) + 1.833(0) + 0.0835(10) + 0.0835(10) = 1.67
The system of equations:
2.167๐11 โ 0.0835๐2
1 = 0.835
โ0.0835๐11 + 2.167๐2
1 = 1.67
The matrix equation:
[2.167 โ0.0835
โ0.0835 2.167] [
๐11
๐21] = [
0.8351.67
]
(6) Solve the system of linear equations.
The current 2x2 system of equations, can be solved by many methods. For instance, by substitution.
Solve for ๐11 from the first equation and replaced it into the second equation, next solve by ๐2
1.
However, this is a โtoyโ problem and in real life problems matrices are many times larger. We are doing
this example to learn the procedure and be able to apply it to larger matrices, therefore be smarter and
run a simpler MATLAB script:
% CN-solution with matlab % File
clc, clear A=[2.167,-0.0835;-0.0835,2.167]; b=[0.835;1.67]; u=A\b
OUTPUT u =
0.4156
0.7867
IMPORTANT: The solution above represents only the interior nodes ๐11and ๐2
1 Adding the constant
BCs, the solution vector is then, u = [5, 0.4156, 0.7867, 10]. This last step is a critical one. Many
times students forget to express the whole solution, i.e., one that includes the known BCs.
(9) As engineers likes to organize their results, then express the results on a table.
P a g e | 9
We have just solved the PDE for one time step. This process should be repeated more more time steps
until satisfying the requested time range (problem domain).
(10) Please review another example on the document: CRANK-Example with MATLAB code
(DOC)
Backward in time, centered in space (BTCS) method
(1) Discretization of the Differential equation.
a. Approximate the 1st order time derivative with backward difference approximation of
๐(โ๐ก) at the grid point (i,k+1):
๐ข๐๐+1 โ ๐ข๐
๐
โ๐ก
b. Approximate the space derivative with central difference approximation of ๐(โ๐ฅ2) at
the grid point (i,k+1):
๐ [๐ข๐โ1
๐+1 โ 2๐ข๐๐+1 + ๐ข๐+1
๐+1
โ๐ฅ2]
c. Plug results in (a) and (b) in the original equation heat equation (Eq-1)
๐ข๐๐+1 โ ๐ข๐
๐
โ๐ก= ๐ [
๐ข๐โ1๐+1 โ 2๐ข๐
๐+1 + ๐ข๐+1๐+1
โ๐ฅ2]
d. The recurrence formula
โ๐๐ข๐โ1๐+1 + (1 + 2๐)๐ข๐
๐+1 โ ๐๐ข๐+1๐+1 = ๐ข๐
๐
(2) Discretize the initial and boundary conditions (same as FTCS method)
(3) Show the BTCS computational molecule within the grid of your discretization
k\ii 0 1 2 3
0 5 0 0 10
1 5 0.4156 0.7867 10
2 5 10
3 5 10
... ... ...
P a g e | 10
(4) According to the problem discretization and the number of unknowns we should find the
appropriate number of equations using the recurrence formula. Equations with Indexed-
variable are usedโNO numbers yet:
Set i=1, and k=0 in the recurrence formula
โ๐๐ข01 + (1 + 2๐)๐ข1
1 โ ๐๐ข21 = ๐ข1
0
Set i=2, and k=0 in the recurrence formula
โ๐๐ข11 + (1 + 2๐)๐ข2
1 โ ๐๐ข31 = ๐ข2
0
As ๐ข01 and ๐ข3
1 are known values the above equations simplify to:
(1 + 2๐)๐ข11 โ ๐๐ข2
1 = ๐ข10 + ๐๐ข0
1
โ๐๐ข11 + (1 + 2๐)๐ข2
1 = ๐ข20 + ๐๐ข3
1
(5) Plugging numbers in equations in (4) and expressing the problem with numbers in a clean
system of linear equations and also in matrix form to solve it for the first step in time.
(1 + 2(0.0835))๐ข11 โ 0.0835๐ข2
1 = 0 + (0.0835)5
โ0.0835๐ข11 + (1 + 2(0.0835))๐ข2
1 = 0 + (0.0835)10
Simplifying, yields the clean system:
1.167๐ข11 โ 0.0835๐ข2
1 = 0.4175
โ0.0835๐ข11 + 1.167๐ข2
1 = 0.835
And the matrix form:
[1.167 โ0.0835
โ0.0835 1.167] [
๐11
๐21] = [
0.41750.835
]
Although in this toy problem we got a 2x2 matrix which does not display tridiagonal characteristics due
to โsmallnessโ be aware that in real problems these matrices are quite larger and tridiagonal.
(6) Solve the matrix by similar methods as the previous CN method.
P a g e | 11
For diffusion equations (and many other equations), it can be shown the CrankโNicolson method is
unconditionally stable. However, the approximate solutions can still contain (decaying) spurious
oscillations if the ratio of time step ฮt times the thermal diffusivity to the square of space step, ฮx2, is
large (typically larger than 1/2 per Von Neumann stability analysis) [i.e., if ๐ =๐ ฮ๐ก
ฮ๐ฅ2 > 1/2]. For this
reason, whenever large time steps or high spatial resolution is necessary, the less accurate BTCS (also
called backward Euler method) is often used, which is both stable and immune to oscillations.
EXERCISES
1- Using FTCS method and matlab rework the toy problem with โx = 0.5 cm, โt = 0.1 s, ฮป =
kโt/โx2=0.835(0.1)/(0.5)2 = 0.334 in the range of 0 โค ๐ก โค 1
2- Discuss the error order of each method: FTSC, CN and BTCS
3- Classify the FTCS, CN, BTCS in implicit and explicit methods
REFERENCES AND EXTERNAL LINKS
1. Numerical PDF Techniques for Scientists and Engineers
http://www3.nd.edu/~dbalsara/Numerical-PDE-Course/
P a g e | 12
APPENDIX ONE
Next is a matlab code, results and graph.
% FTCS Finite Difference Method and the Heat Equation
% file: heatConductionPDE3.m (Feb 20, 2017) % Use the explicit FTCS method to solve for the temperature distribution % of a thin rod insulated at all points, except at its ends with a % length L (or a SEMIinfinite slab-โlosa, planchaโ- with a width L) % L = 10 cm (rod length) % Assume: dx = 2 cm, dt = 0.1 s, k = 0.835 cm2/s, and % lambda = kdt/dx^2=0.835(0.1)/(2)^2 = 0.020875
% IC & BCs: % U(x,O)=0 C for 0<x<10
% U(0,t) = 100 C for t>0
% U(10,t) = 50 C for t>0 clc, clear, close % Define constants
L = 10; % length of domain in x direction
tmax = 12; % end time
dx = 2; dt = 0.1; ix = L/dx + 1; % ix = 6;number of nodes in x direction for current problem
kt = tmax/dt + 1; % if t(1)=0, kt = 121; %number of total time steps for current problem
% dx = L/(ix-1); step size in x dt = tmax/(kt-1); step size in t
k=0.835; % thermal diffusivity r = k*dt/dx^2; r2 = 1 - 2*r; % r = lambda % Nodes coordinates
ii=[1:ix]; kk=[1:kt]; % Table title (this section can be totally eliminated to erase headers) x=([1:ix]-1)*dx; fprintf(' x ==> '); fprintf('%10d ',x); fprintf('\n'); fprintf(' t, below'); % Initial condition & BCs
t(1)= 0; uold(1)=100; uold(2:1:ix-1)=0; uold(ix)=50;
P a g e | 13
uu=zeros(kt,ix); uu(1,ii)=uold; fprintf('\n'); % --- Loop over time steps
for m=2:kt
% uold = u; % prepare for next step
t(m) = t(m-1) + dt; for i=2:ix-1 % only interior nodes
uold(i) = r*uold(i-1) + r2*uold(i) + r*uold(i+1); end uu(m,ii)=uold; end % Output
t=t'; % from row to column uuu=[t,uu]; % uuu stores whole solution: t plus u for jj=1:30:kt % Output selected results
for ii=1:ix+1 fprintf('%10.3f ', uuu(jj,ii)); end
fprintf('\n'); end
fprintf('\n'); % For the graph tPlot= 0:3:12; %up to t(kt) plot(x,uuu(31,2:7), '.b','markers',20) hold on
plot(x,uuu(61,2:7), '.r','markers',20) hold on
plot(x,uuu(91,2:7), '.g','markers',20) hold on
plot(x,uuu(121,2:7), '.m','markers',20) hold on line(x,uuu(31,2:7),'Color','b') line(x,uuu(61,2:7),'Color','r')
P a g e | 14
line(x,uuu(91,2:7),'Color','g') line(x,uuu(121,2:7),'Color','m') title('U, Temperature Distribution'); ylabel('U'); xlabel('x'); legend('t = 3 s','t = 6 s','t = 9 s','t = 12 s'); grid on
OUTPUT
Selected Results are shown: First column is time, next six columns are temperatures
GRAPH OUTPUT