# 2.4 Analysis of Finite Difference Methods

## 2.4.6 Boundary Conditions for Finite Differences

In this section, we discuss the implementation of finite difference methods at boundaries. This discussion is not meant to be comprehensive, as the issues are many and often subtle. In particular, we only focus on Dirichlet boundary conditions.

A Dirichlet boundary condition is one in which the state is specified at the boundary. For example, in a heat transfer problem, the temperature might be known at the boundary. Dirichlet boundary conditions can be implemented in a relatively straightforward manner. For example, suppose that we are solving a one-dimensional convection-diffusion problem and we want the value of $$U$$ at $$i=0$$, to be $$U_{inlet}$$,

 $U_0 = U_{inlet}.$ (2.87)

To implement this, we fix $$U_0 = U_{inlet}$$ and apply the finite difference discretization only over the interior of the computational domain accounting for the known value of $$U_0$$ at any place where the interior discretization depends on it. For example, at the first interior node (i.e. $$i=1$$), the central difference discretization of 1-D convection-diffusion gives,

 $\frac{{\rm d}U_1}{{\rm d}t} + u_{1}\frac{U_{2} - U_0}{2{\scriptstyle \Delta } x} = \mu \frac{U_2 - 2 U_1 + U_0}{{\scriptstyle \Delta } x^2}.$ (2.88)

Accounting for the known value of $$U_0$$, this becomes,

 $\frac{{\rm d}U_1}{{\rm d}t} + u_{1}\frac{U_{2} - U_{inlet}}{2{\scriptstyle \Delta } x} = \mu \frac{U_2 - 2 U_1 + U_{inlet}}{{\scriptstyle \Delta } x^2}. \label{equ:dirichlet_ example}$ (2.89)

In terms of the vector notation, when a Dirichlet boundary condition is applied we usually remove that state from the vector $$U$$. So, in the situation where $$U_0$$ is known, the state vector is defined as,

 $U = \left( U_{1}, U_{2}, \, \ldots ,\, U_{i-1}, U_{i}, U_{i+1}, \, \ldots , \, U_{N_ x-1}, U_{N_ x}\right)^ T,$ (2.90)

The right-hand side $$b$$ vector then will contain the contributions from the known boundary values. For example, by re-arranging Equation (2.89), the first row of $$b$$ contains,

 $b_1 = u_{2}\frac{U_{inlet}}{2{\scriptstyle \Delta } x} + \mu \frac{U_{inlet}}{{\scriptstyle \Delta } x^2}.$ (2.91)

Since $$U_{inlet}$$ does not enter any of the other node's stencils, the remaining rows of $$b$$ will be zero (unless they are altered by the other boundary).

## Effect of boundary conditions on the number of degrees of freedom for the 1D Laplace equation

The number of degrees of freedom in a set of equations is considered to be the number of unknowns. Consider the 1D Laplace equation defined on a finite domain $$x \in [0, T].$$

 $\frac{\partial ^2 u(x)}{\partial x^2} = 0, \quad u(0) = a_1, u(T) = a_2$ (2.92)

Suppose we discretize $$x$$ into $$N$$ unknowns $$(x_1, \ldots x_ N)$$ and we use a second order central difference scheme to approximate the left side of the Laplace equation. The resulting linear equations become

 $$\displaystyle \frac{1}{(\Delta x)^2} \left[ \begin{array}{cccccc} (\Delta x)^2 & 0 & 0 & 0 & \ldots & 0 \\ 1 & -2 & 1 & 0 & \ldots & 0 \\ 0 & 1 & -2 & 1 & \ldots & 0 \\ \vdots & \ddots & \ddots & \ddots & \ddots & \vdots \\ 0 & \ldots & 0 & 1 & -2 & 1 \\ 0 & \ldots & 0 & 0 & 0 & (\Delta x)^2 \end{array} \right] \left[ \begin{array}{c} u(x_1) \\ u(x_2) \\ \vdots \\ u(x_{N-1}) \\ u(x_{N}) \end{array}\right] = \left[ \begin{array}{c} a_1 \\ 0 \\ \vdots \\ 0 \\ a_2 \end{array} \right]$$ (2.93)

It appears that this equation has $$N$$ unknowns and $$N$$ degrees of freedom.

However if we rewrite the first two equations specified by this system:

 $$\displaystyle u(x_1)$$ $$\displaystyle = a_1$$ (2.94) $$\displaystyle u(x_1) - 2u(x_2) + u(x_3)$$ $$\displaystyle = 0$$ (2.95)

We can now remove $$x_1$$ from this system by converting the second equation into

 $-2 u(x_2) + u(x_3) = -a_1$ (2.96)

We can perform the same trick for the last equation to obtain

 $u(x_{N-2}) - 2u(x_{N-1}) = - a_2$ (2.97)

Finally we obtain the system

 $\frac{1}{(\Delta x^2)}\left[ \begin{array}{cccccc} -2(\Delta x^2) & \Delta x^2 & 0 & \ldots & \ldots & 0 \\ 1 & -2 & 1 & 0 & \ldots & 0 \\ 0 & 1 & -2 & 1 & \ldots & 0 \\ \vdots & \ddots & \ddots & \ddots & \ddots & \vdots \\ 0 & \ldots & 1 & -2 & 1 & 0 \\ 0 & \ldots & \ldots & 0 & \Delta x^2 & -2\Delta x^2 \\ \end{array} \right] \left[ \begin{array}{c} u(x_2) \\ u(x_3) \\ \vdots \\ u(x_{N-1}) \end{array}\right]= \left[ \begin{array}{c} -a_1 \\ 0 \\ \vdots \\ 0 \\ -a_2 \end{array} \right]$ (2.98)

This system has $$N-2$$ degrees of freedom. Thus we see that the specification of the Dirichlet boundary conditions has reduced the number of degrees of freedom of the system compared to the number of discretization points.