Exercise 2: Finite Difference Method
Date of exercise: 15th May 2019
Last update: 20th May 2019 by Lukas Babor (Lukas.Babor@tuwien.ac.at)
2.1 Derivation of finite difference scheme from Taylor expansion
Consider a continuous function
and a grid of N discrete points
with uniform spacing
, such that
where
and
are the boundaries of a domain
. "Discretization" of the function
on the grid defines a column vector
containig the values of the function
evaluated at the points
, i.e.:
With finite difference method we approximate the derivative of the function
at some point
using the values of the function evaluated at grid points in the neighbourhood of the point
. For example, the second-order centered finite-difference approximation to a first derivative is
Finite-difference schemes can be derived from Taylor expansion of the function u around the point
. This also provides the expresion for the error term
where n is the order of accuracy of the scheme. The derivation of a finite-difference scheme procedes as follows:
We start by assuming a certain form of the scheme, where we select which points we want to use. Taking the example above we have
Next we expand the values of the function
with Taylor series from the point
:
and substitute the expansions into the expected form
Now we have to form a system of two algebraic equations to determine the coefficients a and b. This can be done by matching the derivatives on the left and right hand side of equation (4):
which leads to
. Substituting
into (2) provides the finite difference scheme (1). We still have to determine the order of accuracy through the error term ε. For that we substitute a and b into (4):
thus
which has two important consequences. Firstly, the scheme is second-order accurate since the leading error term is proportional to
. Secondly, the leading error term does not depend on
, so it does not contribute to artificial diffusivity.
In the same way one can derive finite-difference schemes for a non-uniform grid spacing
. Although the schemes for non-uniform grids have often lower order of accuracy, they allow localised refinement of the function which can be a significant advantage. Using sensible non-uniform grids is therefore generally more efficient.
2.2 Matrix representation of finite difference schemes
The finite difference scheme (1) defines a vector of approximate first derivatives of
at grid points
. For simplicity we will use the notation
Our aim is to represent the finite difference derivative
with a discretization matrix
such that
where the superscript 1 denotes the first derivative, while the subscript x denotes the direction of the derivative (for the case of partial derivatives). Compilation of the matrix from (1) at interior grid nodes
is straightforward. Its only non-zero entries are the first lower and upper diagonal corresponding to the coefficients
and
respectively. The centered scheme is, however, not defined for boundary nodes
(the first and the last row of the matrix). We can approximate the derivatives on boundaries by one-sided differences, e.g.:
The discretization matrix then reads
In MatLab we can construct such matrices with the command spdiags
N = 4; % Number of grid points
dx = 1/(N-1); % Grid spacing
a = -1/(2*dx) ; % Coefficients of FD scheme
b = 1/(2*dx);
e = ones(N,1); % Column vector of ones
D_x = spdiags([a*e b*e], [-1 1], N,N ); % Matrix with D_x(i,i-1)=a; D_x(i,i+1)=b
D_x(1,: ) = 0; % Replaces first row of the matrix
D_x(1,1 ) = -1/dx;
D_x(1,2 ) = 1/dx;
D_x(N,: ) = 0; % Replaces last row of the matrix
D_x(N,N-1) = -1/dx;
D_x(N,N ) = 1/dx;
spy(D_x); % Plots the sparsity pattern of the matrix