# Finite difference method

Not to be confused with "finite difference method based on variation principle", the first name of finite element method[citation needed].

In mathematics, finite-difference methods (FDM) are numerical methods for approximating the solutions to differential equations using finite difference equations to approximate derivatives.

## Derivation from Taylor's polynomial

First, assuming the function whose derivatives are to be approximated is properly-behaved, by Taylor's theorem, we can create a Taylor Series expansion

$f(x_0 + h) = f(x_0) + \frac{f'(x_0)}{1!}h + \frac{f^{(2)}(x_0)}{2!}h^2 + \cdots + \frac{f^{(n)}(x_0)}{n!}h^n + R_n(x),$

where n! denotes the factorial of n, and Rn(x) is a remainder term, denoting the difference between the Taylor polynomial of degree n and the original function. We will derive an approximation for the first derivative of the function "f" by first truncating the Taylor polynomial:

$f(x_0 + h) = f(x_0) + f'(x_0)h + R_1(x),$

Setting, x0=a we have,

$f(a+h) = f(a) + f'(a)h + R_1(x),$

Dividing across by h gives:

${f(a+h)\over h} = {f(a)\over h} + f'(a)+{R_1(x)\over h}$

Solving for f'(a):

$f'(a) = {f(a+h)-f(a)\over h} - {R_1(x)\over h}$

Assuming that $R_1(x)$ is sufficiently small, the approximation of the first derivative of "f" is:

$f'(a)\approx {f(a+h)-f(a)\over h}.$

## Accuracy and order

The error in a method's solution is defined as the difference between its approximation and the exact analytical solution. The two sources of error in finite difference methods are round-off error, the loss of precision due to computer rounding of decimal quantities, and truncation error or discretization error, the difference between the exact solution of the finite difference equation and the exact quantity assuming perfect arithmetic (that is, assuming no round-off).

The finite difference method relies on discretizing a function on a grid.

To use a finite difference method to attempt to solve (or, more generally, approximate the solution to) a problem, one must first discretize the problem's domain. This is usually done by dividing the domain into a uniform grid (see image to the right). Note that this means that finite-difference methods produce sets of discrete numerical approximations to the derivative, often in a "time-stepping" manner.

An expression of general interest is the local truncation error of a method. Typically expressed using Big-O notation, local truncation error refers to the error from a single application of a method. That is, it is the quantity $f'(x_i) - f'_i$ if $f'(x_i)$ refers to the exact value and $f'_i$ to the numerical approximation. The remainder term of a Taylor polynomial is convenient for analyzing the local truncation error. Using the Lagrange form of the remainder from the Taylor polynomial for $f(x_0 + h)$, which is

$R_n(x_0 + h) = \frac{f^{(n+1)}(\xi)}{(n+1)!} (h)^{n+1}$, where $x_0 < \xi < x_0 + h$,

the dominant term of the local truncation error can be discovered. For example, again using the forward-difference formula for the first derivative, knowing that $f(x_i)=f(x_0+i h)$,

$f(x_0 + i h) = f(x_0) + f'(x_0)i h + \frac{f''(\xi)}{2!} (i h)^{2},$

and with some algebraic manipulation, this leads to

$\frac{f(x_0 + i h) - f(x_0)}{i h} = f'(x_0) + \frac{f''(\xi)}{2!} i h,$

and further noting that the quantity on the left is the approximation from the finite difference method and that the quantity on the right is the exact quantity of interest plus a remainder, clearly that remainder is the local truncation error. A final expression of this example and its order is:

$\frac{f(x_0 + i h) - f(x_0)}{i h} = f'(x_0) + O(h).$

This means that, in this case, the local truncation error is proportional to the step size.

## Example: ordinary differential equation

For example, consider the ordinary differential equation

$u'(x) = 3u(x) + 2. \,$

The Euler method for solving this equation uses the finite difference quotient

$\frac{u(x+h) - u(x)}{h} \approx u'(x)$

to approximate the differential equation by first substituting in for u'(x) then applying a little algebra (multiplying both sides by h, and then adding u(x) to both sides) to get

$u(x+h) = u(x) + h(3u(x)+2). \,$

The last equation is a finite-difference equation, and solving this equation gives an approximate solution to the differential equation.

## Example: The heat equation

Consider the normalized heat equation in one dimension, with homogeneous Dirichlet boundary conditions

$U_t=U_{xx} \,$
$U(0,t)=U(1,t)=0 \,$ (boundary condition)
$U(x,0) =U_0(x) \,$ (initial condition)

One way to numerically solve this equation is to approximate all the derivatives by finite differences. We partition the domain in space using a mesh $x_0, ..., x_J$ and in time using a mesh $t_0, ...., t_N$. We assume a uniform partition both in space and in time, so the difference between two consecutive space points will be h and between two consecutive time points will be k. The points

$u(x_j,t_n) = u_{j}^n$

will represent the numerical approximation of $u(x_j, t_n).$

### Explicit method

The stencil for the most common explicit method for the heat equation.

Using a forward difference at time $t_n$ and a second-order central difference for the space derivative at position $x_j$ (FTCS) we get the recurrence equation:

$\frac{u_{j}^{n+1} - u_{j}^{n}}{k} = \frac{u_{j+1}^n - 2u_{j}^n + u_{j-1}^n}{h^2}. \,$

This is an explicit method for solving the one-dimensional heat equation.

We can obtain $u_j^{n+1}$ from the other values this way:

$u_{j}^{n+1} = (1-2r)u_{j}^{n} + ru_{j-1}^{n} + ru_{j+1}^{n}$

where $r=k/h^2.$

So, with this recurrence relation, and knowing the values at time n, one can obtain the corresponding values at time n+1. $u_0^n$ and $u_J^n$ must be replaced by the boundary conditions, in this example they are both 0.

This explicit method is known to be numerically stable and convergent whenever $r\le 1/2$.[1] The numerical errors are proportional to the time step and the square of the space step:

$\Delta u = O(k)+O(h^2) \,$

### Implicit method

The implicit method stencil.

If we use the backward difference at time $t_{n+1}$ and a second-order central difference for the space derivative at position $x_j$ (The Backward Time, Centered Space Method "BTCS") we get the recurrence equation:

$\frac{u_{j}^{n+1} - u_{j}^{n}}{k} =\frac{u_{j+1}^{n+1} - 2u_{j}^{n+1} + u_{j-1}^{n+1}}{h^2}. \,$

This is an implicit method for solving the one-dimensional heat equation.

We can obtain $u_j^{n+1}$ from solving a system of linear equations:

$(1+2r)u_j^{n+1} - ru_{j-1}^{n+1} - ru_{j+1}^{n+1}= u_{j}^{n}$

The scheme is always numerically stable and convergent but usually more numerically intensive than the explicit method as it requires solving a system of numerical equations on each time step. The errors are linear over the time step and quadratic over the space step:

$\Delta u = O(k)+O(h^2). \,$

### Crank–Nicolson method

Finally if we use the central difference at time $t_{n+1/2}$ and a second-order central difference for the space derivative at position $x_j$ ("CTCS") we get the recurrence equation:

$\frac{u_j^{n+1} - u_j^{n}}{k} = \frac{1}{2} \left(\frac{u_{j+1}^{n+1} - 2u_j^{n+1} + u_{j-1}^{n+1}}{h^2}+\frac{u_{j+1}^{n} - 2u_j^{n} + u_{j-1}^{n}}{h^2}\right).\,$

This formula is known as the Crank–Nicolson method.

The Crank–Nicolson stencil.

We can obtain $u_j^{n+1}$ from solving a system of linear equations:

$(2+2r)u_j^{n+1} - ru_{j-1}^{n+1} - ru_{j+1}^{n+1}= (2-2r)u_j^n + ru_{j-1}^n + ru_{j+1}^n$

The scheme is always numerically stable and convergent but usually more numerically intensive as it requires solving a system of numerical equations on each time step. The errors are quadratic over both the time step and the space step:

$\Delta u = O(k^2)+O(h^2). \,$

Usually the Crank–Nicolson scheme is the most accurate scheme for small time steps. The explicit scheme is the least accurate and can be unstable, but is also the easiest to implement and the least numerically intensive. The implicit scheme works the best for large time steps.