1. Differences Among Difference Schemes
An Exploration of Numerical Methods for Partial Differential Equations
An Honors College Course Enhancement under Prof. Amitabha Bose
William L. Ruys
Department of Mathematical Sciences
New Jersey Insitute of Technology
May 3, 2015
Abstract – In the study of partial differential equations, there are ex-
ceedingly few cases in which one can find an exact analytical solution to
a given problem, and even fewer cases where such solutions are computa-
tionally useful. Therefore, numerical methods are often used in practice
to find approximate solutions and to analyze the behavior of a system.
Expanding on techniques developed in numerical methods and linear alge-
bra, I have conducted a more in-depth study of the finite difference methods
used in finding numerical solutions to second order linear partial differential
equations of the parabolic and elliptic kind.
1. Introduction
Partial differential equations are equations containing partial derivatives. This slightly
cyclical and simple definition leaves the reader saying "Well, yeah" and at worst, largely
uninterested.
But the choice to form and solve PDEs is natural, useful, and leads to incred-
ibly interesting results. It is simpler, generality preserving, and often necessary when
describing restrictions and relationships among the derivatives of a function instead of
writing that function out explicitly. Ordinary differential equations can tell us about
the behavior of single packets of information such as particles moving in N dimensions,
but to model curves in time, surfaces, volumes, or any higher dimensional objects, PDEs
must be used. A derivative of one dimension just isn’t enough. The ability to accurately
solve and understand the behavior of PDEs quickly is a fundamental skill in modeling a
wide class of patterns, from the vibrating sub-atomic world to the equally volatile world
of option pricing.
1
2. Unfortunately, explicit and efficiently computable solutions are a rare sight in
the study of PDEs. In this paper we’ll be examining simple numerical methods to
approximate solutions to some of the simplest problems of the second order linear type.
This will serve as an introduction to the topic.
1.1. Second Order Linear PDEs
The most general form for a linear partial differential equation of two independent vari-
ables and constant coefficients is:
A
∂2u
∂x2
+ B
∂2u
∂x∂y
+ C
∂2u
∂y2
+ D
∂u
∂x
+ E
∂u
∂y
+ G = 0
Depending on the selection of the constants A...G, the PDE can take on very different
behaviors and initial boundary conditions. When mathematicians first investigated this
form they were immediately reminded of another structure with a rich and long history,
the conic sections. Second order linear PDEs are classified in a way analogous to the
equation:
Ax2
+ Bxy + Cy2
+ Dx + Ey + G = 0
For simplicity, all examples in this paper are in the case where B, the coefficient for the
mixed derivative term, is zero.
1.1.1. Parabolic PDEs
When B − 4AC = 0 the equation is said to be a parabolic partial differential equation.
Parabolic PDEs are often found describing ’noisy’ systems with dissipation that evolve
in time. In a boundary value problem, they require information about some value of the
solution (or its derivatives) at the boundary, ∂Ω, and the initial value of the solution
throughout the domain, Ω.[9]
u(x, t) = g(x, t) , x ∈ ∂Ω
u(x, 0) = f(x) , x ∈ Ω
1.1.2. Elliptic PDEs
When B − 4AC < 0 the equation is said to be an elliptic partial differential equation.
Elliptic PDEs are often found describing steady state systems. In a boundary value
problem, they require information about the value of the solution at the boundary, ∂Ω.
[5]
u(x, t) = f(x, t) , x ∈ ∂Ω
2
3. 1.1.3. Hyperbolic PDEs
When B − 4AC > 0 the equation is said to be a hyperbolic partial differential equa-
tion. Hyperbolic PDEs are often found describing systems that evolve in time without
dissipation. In a boundary value problem they require information about some value of
a solution (or its derivatives) at the boundary, ∂Ω, and the initial value of the solution
and its derivative throughout the domain, Ω. [8]
u(x, t) = g(x, t) , x ∈ ∂Ω
u(x, t) = f(x, t) , x ∈ Ω
ut(x, t) = f(x, t) , x ∈ Ω
2. Introduction to Finite Difference Methods
The finite difference method attempts to approximate the solution to a PDE by solving a
discretized version of the problem. The partial derivatives in the PDE are replaced with
finite difference approximations that estimate the derivative at a point by its neighboring
values. We switch from solving a partial derivative equation in continuous space to a
partial difference equation on a grid. This can be seen in the figure below.
Figure 1: Grid showing the stencil of a FTCS scheme for a 1D Heat Equation [2]
Converting the equation into a difference form is done through an application
of Taylor’s Theorem, a good proof of which can be found in SPIVAK[1994].
Taylor’s Theorem 1. Let k ≥ 1 be an integer and let the function f : R → R be k
times differentiable at the point a ∈ R. Then there exists a function Rk : R → R such
that [6] :
f(x) = f(a) + f (a)(x − a) +
f (a)
2!
(x − a)2
+ ... +
f(k)(a)
k!
+ Rk(x)(x − a)k
3
4. and limx→a Rk(a) = 0
Taylor’s Theorem 2. Let f be k + 1 times differentiable on the open interval with f(k)
continuous on the closed interval between a and x then Rk be given by:
Rk(x) =
f(k+1)(ξ)
(k + 1)!
where ξ is a real number between a and x
Note that the remainder term Rk(x) is of order O((x − a)k). This form of
Taylor’s Theorem can be extended to describe movement along a multivariate function
in one direction with only minor changes if we hold the other variables constant. Letting
∆x = x − a for compactness of notation we have the following expansions in the u(x, y)
case:
u(x + ∆x, y) = u(x, y) + ∆x(
∂u
∂x
)(x,y) +
(∆x)2
2!
(
∂2u
∂x2
)(x,y) + O(∆x3
) (1)
u(x − ∆x, y) = u(x, y) − ∆x(
∂u
∂x
)(x,y) +
(∆x)2
2!
(
∂2u
∂x2
)(x,y) + O(∆x3
) (2)
We will now introduce another change in notation to further compact the ex-
pression, simplify our lives, and better fit our picture of a grid. Letting xi = x0 + i∆x
and yi = y0 + i∆y, where x0 and y0 are the coordinates for the bottom right corner of
our rectangular grid, we can write u(x, y) = ui,j and u(x + ∆x, y) = ui+1,j. Superscript
on u, such as un , will denote the nth time-step. Focusing on (1) we can truncate and
rearrange to get a forward difference approximation to
∂u
∂x
.
Forward Difference Approximation 1. When solving on a grid with spacing ∆x, the
partial derivative
∂u
∂x
at point (i, j) is approximated by:
∂ui,j
∂x
=
ui+1,j − ui,j
∆x
with an error of O(∆x)
Proof.
ui+1,j = ui,j + ∆x(
∂ui,j
∂x
) + O(∆x2
)
∆x(
∂ui,j
∂x
) = ui,j − ui+1,j + O(∆x2
)
(
∂ui,j
∂x
) =
ui,j − ui+1,j
∆x
+
O(∆x2)
∆x
(
∂ui,j
∂x
) =
ui,j − ui+1,j
∆x
+ O(∆x)
In a similar manner we can derive a backward difference approximation using
equation (2):
4
5. Backward Partial Difference Approximation 1.
∂ui,j
∂x
=
ui,j − ui−1,j
∆x
with an error of O(∆x)
By subtracting (1) and (2) we can form a more accurate centered difference
approximation:
Centered Partial Difference Approximation 1.
∂ui,j
∂x
=
ui+1,j − ui−1,j
2∆x
with an error of O(∆x2
)
By combining the above or deriving directly from the corresponding Taylor
expansions, we are also able to derive expressions for the second order partial derivatives
of a function with respect to either variable. Note: An expression for the second order
mixed derivative can also be derived but this will not be covered.
Second Order Partial Difference Approximation 1.
∂2ui,j
∂x2
=
ui+1,j − 2ui,j + ui−1,j
∆x2
with an error of O(∆x2
)
2.1. Convergence and Stability
When we replace partial derivatives with partial differences we turn a differential equation
into a difference equation. This introduces truncation error and a different structure to
the problem. It would be exceedingly convenient for a difference equation to converge to
the corresponding differential equation in the limit as ∆x vanishes, however, in general
this is hard to show for a given problem. Thankfully, Lax and Richtmyer showed in
1957 that under certain and common conditions convergence is equivalent to the easier
problem of stability. [4]
Lax-Richtmyer Equivalence Theorem 1. Stability is a necessary and sufficient con-
dition for convergence of consistent finite difference methods for well-posed linear partial
differential equations in complete normed spaces.
These conditions are held throughout the paper unless otherwise stated.
2.1.1. Von-Neumann Stability Analysis
One of the most common used and simplest forms of stability analysis used on finite
difference schemes is known as Von-Neumann (or also called Fourier) analysis. Von-
Neumann analysis looks at how the truncation errors evolve throughout the step of the
algorithm. A difference scheme is said to be stable if the errors remain constant or decay
as more time steps are taken. In effect, this means that the solution will remain bounded.
Likewise, a solution is said to be unstable if the errors grow throughout the execution
and the solution becomes unbounded.
5
6. Von-Neumann analysis has been shown to be a valid method of analyzing the
stability of linear difference equations with constant coefficients and periodic boundary
conditions. We direct the reader to G.D.Smith[1985] or Von-Neumann’s original paper
for a rigorous treatment and foundation of the method.
In Von-Neumann analysis we begin by assuming the solution of the difference
equation can be written as a Fourier series. We decompose the time-steps of the solu-
tion un into their Fourier components, substitute them into the recurrence equation, and
look at the stability by examining the amplification factor, Q, in each mode. In Haber-
man[2013], this method is justified through solving the difference equations through sep-
aration of variables. In the analyses throughout the paper the highest frequency mode
will be looked at as it is typically the source of the most volatile error. We further assume
that the amplification factor is exponential as typical error behavior tends to grow or
decay. The scheme is stable if |Q| ≤ 1. One will expect convergent oscillatory behavior
for −1 < |Q| < 0 and positive convergent behavior for 0 < |Q| < 1.
3. The Heat-Diffusion Equation
The heat-diffusion equation is a parabolic PDE that describes how a distribution of
temperature evolves in a given region over time. In our analysis we will be looking at the
1D Heat Equation with no internal or external heat generation and a constant thermal
coefficient, k, in a rod length of L. This is given by:
∂u
∂t
= k
∂2u
∂x2
A derivation of the heat equation can be found in Haberman[2013]. The two boundary
conditions we will be looking at are:
Dirichlet Boundary Conditions Where u(x, t) is held to be 0 at the boundary.
u(x, 0) = f(x)
u(0, t) = 0
u(L, t) = 0
This PDE and boundary condition can be solved with the infinite series solution:
u(x, t) =
∞
1
Bnsin(
nπx
L
)e−k( nπx
L
)2t
, Bn =
2
L
L
0
f(x)sin(
nπx
L
)
Neumann Boundary Conditions Where ut(x, t) is held to be 0 at the boundary.
u(x, 0) = f(x)
ut(0, t) = 0
6
7. ut(L, t) = 0
This PDE and boundary condition can be solved with the infinite series solution:
u(x, t) =
∞
0
Ancos(
nπx
L
)e−k( nπx
L
)2t
, An =
2
L
L
0
f(x)cos(
nπx
L
)
In practice, the above series are not computationally useful for nontrivial cases.
They will have truncation error when calculated up to a partial sum and exhibit Gibbs
phenomenon. Additionally, for a general f(x), they are computationally expensive. Dif-
ference Schemes provide a better and faster approach that due to their discrete nature
are even naturally suited to handle discontinuous initial conditions.
3.1. Forward Time Centered Space (FTCS) Scheme
We start with the FTCS scheme where the time derivative is replaced by the forward
difference and the space derivative is replaced by the centered difference as seen below:
un+1
i − un
i
∆t
= k
un
i+1 − 2un
i + un
i−1
∆x2
(3)
The error terms of the two finite difference approximations combine giving the scheme
an error of O(∆x2) + O(∆t). The FTCS scheme is an explicit scheme and we can solve
for the solution at the next time step, un+1
j , using information about the previous time
step. This gives the following recurrence equation:
un+1
i = un
i + s(un
i+1 − 2un
i + un
i−1), s =
k∆t
∆x2
(4)
Dirichlet To handle Dirichlet boundary conditions we set un
0 and un
L = 0 for all time-
steps and iterate to find remaining un:
u(1, t) = 0;
u(L, t) = 0;
for i = 2:L-1
u(i, t+1) = u(i, t) + (k*dt/(dx^2))*(u(i-1, t) - 2*u(i, t) + u(i+1, t));
end
Neumann To handle Neumann boundary conditions we examine the first order centered
difference in space equal to zero at the boundaries. Solving sets un
0 = un
−1 and un
L+1 =
un
L for all time-steps and iterates to find all un:
u(1, t+1) = u(1, t) + (k*dt/(dx^2))*(2*u(2, t)- 2*u(1, t));
for i = 2:L-1
u(i, t+1) = u(i, t) + (k*dt/(dx^2))*(u(i-1, t) - 2*u(i, t) + u(i+1, t));
end
u(L, t+1) = u(L, t) + (k*dt/(dx^2))*(2*u(L-1, t) - 2*u(L, t));
7
8. 3.1.1. Von-Neumann Stability Analysis
Result The FTCS scheme is stable for k∆t
∆x2 ≤ 1
2 when solving with Dirichlet bound-
ary conditions. Note that the same results hold for solving with Neumann boundary
conditions. when solving Dirichlet boundary conditions.
Proof. Let un
j = Qmeikj∆x
eiαx
Qm+1
= Qm
(eiαx
+ s(eiαx+iα∆x
− 2eiαx
+ eiαx−iα∆x
))
Q = 1 + s(eiα∆x
+ e−iα∆x
− 2)
= 1 − 2s + s(eiα∆x
+ e−iα∆x
)
= 1 − 2s + 2s cos(α∆x)
|Q| ≤ 1
|1 − 2s + 2s cos(α∆x)| ≤ 1
−1 ≤ 1 − 2s + 2s cos(α∆x) ≤ 1
−2 ≤ 2s(−1 + cos(α∆x)) ≤ 0
s ≤
1
2
≤
1
1 − cos(α∆x)
k∆t
∆x2
≤
1
2
3.2. Backward Time Centered Space (FTCS) Scheme
The BTCS scheme replaces the time derivative by the backward difference and the space
derivative by the centered difference seen below:
un
i − un−1
i
∆t
= k
un
i+1 − 2un
i + un
i−1
∆x2
(5)
As the error of the backward difference is the same as the forward difference
the BTCS scheme also has an error of O(∆x2) + O(∆t). However the BTCS scheme is
an implicit scheme. We cannot solve for u at the next time-step. To overcome this we
are left with two choices. We can apply the iterative methods [such as those found in
Section 4] or use direct matrix solving methods to solve a system of these equations. As
the former will be explored in detail later, we will explore the matrix option. We shift
the index forward by one, n = n + 1, and rearrange (5) to the form:
−(
k∆t
∆x2
)un+1
i−1 + (1 +
2k∆t
∆x2
)un+1
i − (
k∆t
∆x2
)un+1
i+1 = un
i (6)
A system of (6) can be put into the form Ax = b where A is a toeplitz matrix, b is a
vector of un
i and x is a vector at the next time-step, un+1
i .
8
9. Dirichlet To handle Dirichlet boundary conditions we set the first and last elements of
the initial b vector to zero. Additionally, the top right corner and bottom left corner of
A are set to 1 with all other elements of those rows set equal to zero. [1]
A =
1 0 0 · · · · · · · · · · · · 0
−( k∆t
∆x2 ) (1 + 2k∆t
∆x2 ) −( k∆t
∆x2 ) 0
...
0 −( k∆t
∆x2 ) (1 + 2k∆t
∆x2 ) −( k∆t
∆x2 )
...
...
... 0
...
...
...
...
...
...
...
...
...
... 0
...
...
... −( k∆t
∆x2 ) (1 + 2k∆t
∆x2 ) −( k∆t
∆x2 ) 0
... 0 −( k∆t
∆x2 ) (1 + 2k∆t
∆x2 ) −( k∆t
∆x2 )
0 · · · · · · · · · · · · 0 0 1
Neumann To handle Neumann boundary conditions we set the second element of the
first and last rows to −(2 k∆t
∆x2 ). This is by the same reasoning as in the Neumann case
for the FTCS scheme.
3.2.1. Von-Neumann Stability Analysis
Result The BTCS scheme is shown to be unconditionally stable when solving with
Dirichlet boundary conditions. Note that the same results hold for solving with Neumann
boundary conditions.
Proof. Let un
j = Qmeikj∆x in (8)
Qm
eiαx
= Qm+1
((1 +
2k∆t
∆x2
)eiαx
− (
k∆t
∆x2
)(eiαx+iα∆x
+ eiαx−iα∆x
))
1
Q
= 1 +
2k∆t
∆x2
− (
k∆t
∆x2
)(eiα∆x
+ e−iα∆x
)
1
Q
= 1 +
2k∆t
∆x2
−
2k∆t
∆x2
cos(α∆x)
|Q| = |
1
1 + 2k∆t
∆x2 − 2k∆t
∆x2 cos(α∆x)
| ≤ 1
3.3. Experimental Results and Comparison
As the BTCS scheme has to solve a system of equations at each lattice point it is slower
than the FTCS scheme.
9
10. 3.3.1. Constant ∆x = 0.1, Changing ∆t
We ran two simulations of the 1D Heat Equation, k = 1
9, at ∆t = 0.05 and ∆t = 0.005
with both the FTCS and BTCS schemes. The initial conditions for the problem are
u(x, 0) = sin(2πx) with Dirichlet boundary conditions We calculate the error at the time-
step corresponding to t = 0.5 for both the BTCS and FTCS schemes by subtracting the
known solution from the finite difference estimations.
exactSolution = sin(2*pi*(xvec))’ *exp((-k*((2*pi)^2).*tvec))
The results of these tests can be seen in Figures 4 and 5 listed in Appendix A.
Both the FTCS and BTCS schemes have an error of O(∆t) therefore we expect an error
4 times less between ∆t = 0.05 and ∆t = 0.005. As the time spacing is a much larger
source of error than space we take the ratio between these two times. This is what we
observe experimentally in Figure 6.
3.3.2. Constant ∆t = 0.01, Changing ∆x
We ran two simulations of the 1D Heat Equation, k = 1
9, at ∆x = 0.1 and ∆x = 0.05
with both the FTCS and BTCS schemes. The initial conditions for the problem are
u(x, 0) = sin(2πx) with Dirichlet boundary conditions We calculate the error at the time-
step corresponding to t = 0.5 for both the BTCS and FTCS schemes by subtracting the
known solution from the finite difference estimations.
exactSolution = sin(2*pi*(xvec))’ *exp((-k*((2*pi)^2).*tvec))
The results of these tests can be seen in Figures 4 and 5 listed in Appendix A.
Both the FTCS and BTCS schemes have an error of O(∆x2). We expect and observe a
small decrease in error between a ∆x = 0.1 and ∆x = 0.01. We repeat the tests between
∆x = 0.2 and ∆x = 0.1 in Figures 10 and 11. We did not isolate the error reliant on
∆x and as the error is dominated by the order ∆t term the ratio of the errors produces
meaningless results. This will need further analysis to isolate the dependence of ∆x in
future updates to the paper.
3.3.3. Comparison Outside FTCS Region of Stability
We ran a simulation of the 1D Heat Equation, k = 1
9, at ∆t = 0.01 and ∆x = 0.01 with
both the FTCS and BTCS schemes. This is well outside of the region of stability with
s = 100
9 .
Result The FTCS scheme quickly diverged in an oscillatory manner and produced a
meaningless result. The BTCS scheme produced the expected result with an error of
order 103. The results of this can be seen in Figures 12 and 13.
10
11. 4. Laplace’s Equation
Laplace’s equation is an elliptic PDE whose solutions are the harmonic function. We will
be examining the 2D version of Laplace’s equation. This can be viewed as the steady
state solution of a 2D Heat Equation and is given by:
∂2u
∂x2
+
∂2u
∂y2
= 0
The boundary conditions we consider specify the value of u at the boundary. Laplace’s
equation can be discretized by replacing both of the second order x and y partial deriva-
tives with their second order centered difference approximations as below:
ui+1,j − 2ui,j + ui−1,j
∆x2
+
ui,j+1 − 2ui,j + ui,j−1
∆y2
= 0 (7)
We let ∆x = ∆y and rearrange to derive:
ui,j =
ui+1,j + ui−1,j + ui,j+1 + ui,j−1
4
(8)
We cannot solve (8) directly as the neighboring values ui+1,j + ui−1,j + ui,j+1 + ui,j−1
are, away from the boundary, not known. We choose to apply iterative methods that
step and averages through the grid until convergence is reached.
4.1. Jacobi Iteration
In the Jacobi method we make an initial guess of the solution throughout the grid and
then update the solution from there. We form a recurrence equation out of (8) to update
the solution at the next time step using information from the previous time step:
un+1
i,j =
un
i+1,j + un
i−1,j + un
i,j+1 + un
i,j−1
4
Note: The Jacobi method is only stable if the matrix form of a system is diagonally
dominant. (8) creates such a system and is unconditionally stable. This is also seen in
the Section 4.1.1 result.
4.1.1. Convergence Speed: Von-Neumann Analysis
We perform a two spacial dimension Von-Neumann Analysis of the Jacobi method. Let
un
i,j = Qmei(αx+βy) in (8) so,
Qm+1
=
Qm
4
(ei(α∆x)
+ e−i(α∆x)
+ ei(β∆y)
+ e−i(β∆y)
)
Q =
1
2
(cos(α∆x) + cos(β∆y))
11
12. Since |Q| < 1 for all α and β the scheme is stable. Error decreases the slowest when Q
is close to 1.The occurs for the smallest and largest choice of α and β, where N is the
number of lattice points:
|Q| = cos(
π
N
) ≈ 1 −
π2
2N2
We examine how many steps are needed if we require an error half as large.
|Q|m
≈ (1 −
π2
2N2
)m
=
1
2
m = N2 2 log 2
π2
The number of steps needed to reduce the error by half increases proportional to the
number of lattice points squared. [3]
4.2. Gauss-Seidel Iteration
The Gauss-Seidel method takes advantage of the updated lattice points as it iterates
through the grid. Where the Jacobi method waits until a full sweep of the grid to update
to the new points, the Gauss-Seidel method uses them as they become available. The
recurrence equation becomes:
un+1
i,j =
un+1
i+1,j + un
i−1,j + un+1
i,j+1 + un
i,j−1
4
4.2.1. Convergence Speed: Von-Neumann Analysis
Letting un
i,j = Qmei(αx+βy) we can maximize |Q| as:
|Q| =
1
5 − 4 cos π
N
≈ 1 − (
π
N
)2
The Gauss-Seidel method has a |Q| value twice as far from 1 as the Jacobi Method.
Therefore we can conclude that the Gauss-Seidel method takes about half the number of
iterations to halve the error.[3]
For a complete analysis we direct the reader to Haberman[2013] to see an ap-
proach using complex arithmetic or to Trefethen[1987] for an approach by change in
variable. The result of Haberman calculation is the one shown above.
4.3. Successive Over Relaxation (S-O-R)
Successive Over Relaxation is a modification and improvement to the Gauss-Seidel method.
S-O-R weights and combines the Gauss-Seidel iteration and the previous value, un
i,j.
un+1
i,j = (1 − ω) un
i,j + ω
un+1
i+1,j + un
i−1,j + un+1
i,j+1 + un
i,j−1
4
12
13. 4.3.1. Convergence Speed
An analysis of the S-O-R method falls out of the scope of this paper. There has been in-
credible work and fascinating theory developed on how the relaxation factor, ω, dampens
the amplification factor in low frequency modes of the error.
We list the main results:
• For an optimal choice of ω: |Q| ≈ 1 − π
2N [3] [7]
• For a rectangular N by N grid in solving Laplace’s Equation the optimal ω is[10]:
2
1 + sin( π
N+1)
• Errors are reduced by 1
2 linearly!
4.4. Experimental Results and Comparison
We solve Laplace’s equation with Successive Over Relaxation on a 100 by 40 grid with
initial conditions of u(x, t) = 50 along the x axis and zero along every other boundary.
The results are pictured below in Figure 2.
Figure 2: Solution on a 100 by 40 Grid
4.4.1. Convergence as THRESHOLD is Halved
We begin our comparison of the three iterative techniques with an experimental analysis
of how the number of iterations grows as the error threshold is halved. From the above
analysis in Sections 4.1.1, 4.2.1, and 4.3.1 we expect Jacobi and Gauss-Seidel Iterations
to grow quadratically and the S-O-R method to grow linearly. We start with a THRESH-
OLD value of 0.01 and halve it 9 times repeating each iteration method for each halving.
13
14. Figure 3: A comparison of the growth in number of iterations as the error threshold
decreases
The problem is solved on a 100 by 100 grid with initial u(x, t) = 50 along the x axis and
zero along every other boundary. The results are shown in Figure 3 below:
The experimental results line up with the theoretical results. The iterations
needed to converge for the Jacobi and Gauss-Seidel methods grow quadratically and the
SOR method appears to grow linearly. Additionally we note that the number of iterations
in the Gauss-Seidel method approach approximately half of the iterations needed when
solving with the Jacobi method.
5. Conclusion and Future Considerations
In our study we have examined the theoretical foundations and constructed experimental
implementations for finding the approximate solutions to two different PDEs. It has been
an incredible journey in which the author has extended his knowledge from his previous
coursework in a satisfactory manner, while also learning a great deal about how to use
TeXstudio. The author plans to continue the study with topics seen below.
5.1. O(∆x2
) Error in BTCS and FTCS Schemes
In our analysis, the rate of O(∆x2) was not directly observed. Further data analysis
is needed to isolate the necessary terms and will be included in further updates to the
paper.
14
15. 5.2. Laplace’s Equation on a Non-Uniform Grid
During the course of the study the question of solving Laplace’s equation on a general
non-rectangular domain was considered. A scheme using centered differences and non-
uniform spacings was derived and is shown below. However, as the grid spacing are
not uniform it does not satisfy the conditions of the Lax-Equivalence Theorem. A Von-
Neumann analysis of its stability was attempted but remains incomplete at this time of
writing.
5.3. Hyperbolic PDE: 1D Wave Equation
This study did not complete the scope it set out to cover. We have analyzed simple finite
difference methods for the case of a parabolic and an elliptic PDE. However, the case of
a hyperbolic PDE was not covered. In future updates to the paper, we will construct a
finite difference scheme for and an analysis of the 1D Wave Equation.
5.4. Extensions of the Finite Difference and Iterative Methods
Further study would also like to investigate the modern improvements to solving Laplace’s
equation with finite differences. This would include implementations of the Multi-grid
and Scheduled Relaxation Jacobi algorithms.
References
[1] K. Atkinson and W. Han, Elemetary Numerical Analysis, John Wiley & Sons,
Inc., third edition ed., 2004.
[2] W. L. M. L. Goosse H., P.Y. Barriat and V. Zunz, Intro-
duction to Climate Dynamics and Climate Modelling, Online Textbook.
http://www.climate.be/textbook.
[3] R. Haberman, Applied Partial Differential EEquation with Fourier Series and
Boundary Value Problems, Pearson, fifth edition ed., 2013.
[4] R. Richtmyer, "Difference Methods for Initial Value Problems", Interscience, New
York, (1957).
[5] T. Rowland, Elliptic Partial Differential Equation.
http://mathworld.wolfram.com/EllipticPartialDifferentialEquation.html.
[6] M. Spivak, Calculus, Publish or Perish, Houston, TX, 3rd ed., 1994, p. 383.
[7] L. B. Trefethen and R. J. LeVeque, "Fourier Analysis of the SOR Iteration",
IMA Journal of Numerical Analysis, (1988).
[8] E. W. Weisstein, Hyperbolic Partial Differential Equation.
15
16. [9] , Parabolic Partial Differential Equation.
[10] S. Yang and M. K. Gobbert, "The Optimal Relaxation Parameter for the SOR
Method Applied to a Classical Model Problem", (2007).
16
18. B. MATLAB Code
B.1. Heat-Diffusion Equation
The Main File - test.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Test Program for Heat Equation Finite Difference Solutions - William Ruys 3/14/2015
%
%Notes:
%-BTCS fails to display correctly for a constant initinal condition in
% the Neumann case. The solution matrix is correct to visible signifigant
% digits.
%-The nonconvergence of the FTCS solution in the Neumann case was found to
% be a bug in the previous version of the program and is fixed in this
% implementation.
%-The following implementations are optimized and faster than those last
% shown along with being much more user friendly
%-I removed slice of time animations because they are often buggy and oddly
% scaled
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%The following solutions are on the spacial range [0, 1].
%The inital condition, must be a function handle of x. (To be evaluated at x = 0 to 1)
IC = @(x) sin(2*pi*x);
k=1/9; dt=0.01; dx=0.01; MAXTIME=1;
%Set BC to 0 for Neumann, 1 for Dirichlet
BC = 1;
tvec = (0:dt:(MAXTIME));
xvec = (0:dx:(1));
%The exact solution for sin(2*pi*x) with Dirichlet Boundary Conditions
%Set CheckError to 0 to stop error graphs and tables, 1 to display them
exactSolution = sin(2*pi*(xvec))’ * exp((-k*((2*pi)^2).*tvec));
CheckError = 1;
%Generate the solutions with two different finite difference schemes
tic
fprintf(’Working on FTCS Solutionn’)
[FTCSresults] = HeatEquationForward(IC, dx, dt, k, MAXTIME, BC);
toc
18
19. tic
fprintf(’nWorking on BTCS Solutionn’)
[BTCSresults] = HeatEquationBackward(IC, dx, dt, k, MAXTIME, BC);
toc
if(CheckError)
FTCSerror = FTCSresults-exactSolution;
BTCSerror = BTCSresults-exactSolution;
%Print out error tables. At t = 0.25, 0.5, 1 and all x values
fprintf(’n----------------------------------------------------’)
fprintf(’nTHE ERROR TABLESn’)
fprintf(’----------------------------------------------------nn’)
t = 0.25; %First Time
fprintf(’-----------n’)
fprintf(’| t = 0.25 |n’)
fprintf(’-----------n’)
fprintf(’Locationt’)
for i=1:length(xvec)
fprintf(’%.4et’, (i-1)*dx)
end
fprintf(’nFTCSerrort’)
for i=1:length(xvec)
fprintf(’%.4et’, FTCSerror(i, t/dt));
end
fprintf(’nBTCSerrort’)
for i=1:length(xvec)
fprintf(’%.4et’, BTCSerror(i, t/dt));
end
fprintf(’n’)
t=0.50; %Second Time
fprintf(’-----------n’)
fprintf(’| t = 0.50 |n’)
fprintf(’-----------n’)
fprintf(’Locationt’)
for i=1:length(xvec)
fprintf(’%.4et’, (i-1)*dx)
end
fprintf(’nFTCSerrort’)
for i=1:length(xvec)
fprintf(’%.4et’, FTCSerror(i, t/dt));
end
fprintf(’nBTCSerrort’)
19
20. for i=1:length(xvec)
fprintf(’%.4et’, BTCSerror(i, t/dt));
end
fprintf(’n’)
t=1; %Third Time
fprintf(’-----------n’)
fprintf(’| t = 1.00 |n’)
fprintf(’-----------n’)
fprintf(’Locationt’)
for i=1:length(xvec)
fprintf(’%.4et’, (i-1)*dx)
end
fprintf(’nFTCSerrort’)
for i=1:length(xvec)
fprintf(’%.4et’, FTCSerror(i, t/dt));
end
fprintf(’nBTCSerrort’)
for i=1:length(xvec)
fprintf(’%.4et’, BTCSerror(i, t/dt));
end
fprintf(’n’)
end
%Generate Relevent Graphs
mesh(tvec, xvec, FTCSresults)
title ’FTCS Solution’
xlabel ’Time’
ylabel ’Space’
zlabel ’Solution’
figure()
mesh(tvec, xvec, BTCSresults)
title ’BTCS Solution’
xlabel ’Time’
ylabel ’Space’
zlabel ’Solution’
figure()
mesh(tvec, xvec, BTCSresults-FTCSresults)
title ’Comparison of Solutions (BTCS - FTCS)’
xlabel ’Time’
ylabel ’Space’
zlabel ’Difference’
if(CheckError)
figure()
20
21. mesh(tvec, xvec, FTCSerror)
title ’FTCS Error’
xlabel ’Time’
ylabel ’Space’
zlabel ’Error’
figure()
mesh(tvec, xvec, BTCSerror)
title ’BTCS Error’
xlabel ’Time’
ylabel ’Space’
zlabel ’Error’
% figure()
% MUST REMOVE ANY ZERO ROWS OF EXACT SOLUTION TO CALCULATE RELATIVE
% ERROR
% a = FTCSerror./exactSoltion;
% mesh(tvec, xvec, a)
% title ’FTCS Error’
% xlabel ’Time’
% ylabel ’Space’
% zlabel ’Error’
% figure()
% a = BTCSerror./exactSolution;
% mesh(tvec, xvec, a)
% title ’BTCS Error’
% xlabel ’Time’
% ylabel ’Space’
% zlabel ’Error’
end
HeatEquationForward.m
function [u] = HeatEquationForward(IC, dx, dt, k, MAXTIME, BC)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%HEAT EQUATION FORWARD - William Ruys 3/6/2015
%A Forward Finite Difference Approximation of the solution to the
%heat equation on [0, 1] for the given initial conditions (IC) and diffusion
%coeffcient k with mesh discretations of dt and dx up until t=MAXTIME.
%Boundary Conditions (BC) specify Neumann or Dirichlet solution conditions.
%dt and dx.
%
% IC (Initial Conditions) - Function Handle
% dx - IEE 754 Double
% dy - IEE 754 Double
% k - Unsigned Double
21
22. % MAXTIME - Unsigned Double
% BC - 0 for Neumann, 1 for Dirichlet
%
%The solution is known to be stable for: dt/(dx)^2 <= k/2
%This can shown for both the Neumann and Dirichlet through Fourier
%Stability Analysis.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tvec = (0:dt:(MAXTIME))’;
xvec = (0:dx:(1))’;
L = length(xvec);
u = zeros(L, length(tvec));
u(:, 1) = IC(xvec);
if(BC==1)
for t=1:(MAXTIME/dt)
u(1, t) = 0;
u(L, t) = 0;
for i = 2:L-1
u(i, t+1) = u(i, t) + (k*dt/(dx^2))*(u(i-1, t) - 2*u(i, t) + u(i+1, t));
end
end
end
if(BC==0)
for t=1:(MAXTIME/dt )
u(1, t+1) = u(1, t) + (k*dt/(dx^2))*(2*u(2, t)- 2*u(1, t));
for i = 2:L-1
u(i, t+1) = u(i, t) + (k*dt/(dx^2))*(u(i-1, t) - 2*u(i, t) + u(i+1, t));
end
u(L, t+1) = u(L, t) + (k*dt/(dx^2))*(2*u(L-1, t) - 2*u(L, t));
end
end
end
HeatEquationBackward.m
function [results] = HeatEquationBackward(IC, dx, dt, k, MAXTIME, BC)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%HEAT EQUATION BACKWARD - William Ruys 3/6/2015
%A Backward Finite Difference Approximation of the solution to the
%heat equation on [0, 1] for the given initial conditions (IC) and diffusion
%coeffcient k with mesh discretations of dt and dx up until t=MAXTIME.
%Boundary Conditions (BC) specify Neumann or Dirichlet solution conditions.
22
23. %dt and dx.
%
% IC (Initial Conditions) - Function Handle
% dx - IEE 754 Double
% dy - IEE 754 Double
% k - Unsigned Double
% MAXTIME - Unsigned Double
% BC - 0 for Neumann, 1 for Dirichlet
%
%The solution is known to be unconditionally stable.
%This can shown for both the Neumann and Dirichlet BC through Fourier
%Stability Analysis.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tvec = (0:dt:(MAXTIME));
xvec = (0:dx:(1));
L = length(xvec);
results = zeros(L, length(tvec));
results(:, 1) = IC(xvec);
C = -k*dt/(dx^2);
D = 1+((2*k*dt)/(dx^2));
A = toeplitz([D, C, zeros(1, L-2)]);
if(BC==1)
results(1,1) = 0;
results(L,1) = 0;
A(1, 1) = 1;
A(L, L) = 1;
A(1, 2) = 0;
A(L, L-1) = 0;
end
if(BC==0)
A(1, 2) = 2*C;
A(L, L-1) = 2*C;
end
for t=1:(MAXTIME/dt )
results(:, t+1) = linsolve(A, results(:, t));
end
end
23
24. B.2. Laplace’s Equation
Problem Setup
dx = .1;
dy = .1;
H = 10;
L = 10;
THRESHOLD = 0.1;
y = 0:dy:H;
x = 0:dx:L;
LY = length(y);
LX = length(x);
u = ones(LY, LX)*0;
u(1, 1:LX) = ones(1, LX)*100;
uSOR = u;
uGS = u;
uJAC = u;
Jacobi Iteration
uold = zeros(LY, LX);
count = 0;
fprintf(’Jacobi Iteration’)
fprintf(’n=========================================================n’)
tic
while(max(max(abs(uJAC-uold))) >= THRESHOLD)
uold = uJAC;
count=count+1;
for i=2:LY-1
for j=2:LX-1
%using SOR iterations to solve the system
uJAC(i, j) = 0.25*( (uold(i+1, j) + uold(i-1, j)) + (uold(i, j+1) + uold(i, j-1)));
end
end
end
toc
fprintf(’The number of iterations is: %d’, count);
fprintf(’n=========================================================n’);
24
25. Gauss-Seidel Iteration
uold = zeros(LY, LX);
count = 0;
fprintf(’Gauss Siedel Iteration’)
fprintf(’n=========================================================n’)
tic
while(max(max(abs(uGS-uold))) >= THRESHOLD)
uold = uGS;
count=count+1;
for i=2:LY-1
for j=2:LX-1
%using SOR iterations to solve the system
uGS(i, j) = 0.25*( (uGS(i+1, j) + uGS(i-1, j)) + (uGS(i, j+1) + uGS(i, j-1)));
end
end
end
toc
fprintf(’The number of iterations is: %d’, count);
fprintf(’n=========================================================nnn’);
S-O-R
uold = zeros(LY, LX);
count = 0;
omega = 1.53;
fprintf(’THRESHOLD = %d n’, THRESHOLD)
fprintf(’Successive Overrelaxation’)
fprintf(’n=========================================================n’)
tic
while(max(max(abs(uSOR-uold))) >= THRESHOLD)
uold = uSOR;
count=count+1;
for i=2:LY-1
for j=2:LX-1
%using SOR iterations to solve the system
uSOR(i, j) = (1-omega)*uSOR(i, j) + omega * 0.25*( (uSOR(i+1, j) + uSOR(i-1, j)) +
end
end
end
toc
fprintf(’The number of iterations is: %d’, count);
fprintf(’n=========================================================n’);
25
26. Sample Output
H = 2;
L = 2;
Successive Over Relaxation
=========================================================
Elapsed time is 0.002683 seconds.
The number of iterations is: 90
=========================================================
Jacobi Iteration
=========================================================
Elapsed time is 0.008495 seconds.
The number of iterations is: 316
=========================================================
Gauss Siedel Iteration
=========================================================
Elapsed time is 0.004932 seconds.
The number of iterations is: 183
=========================================================
26