In numerical analysis, the Crank–Nicolson method is a finite difference method used for numerically solving the heat equation and similar partial differential equations.[1] It is a second-order method in time. It is implicit in time, can be written as an implicit Runge–Kutta method, and it is numerically stable. The method was developed by John Crank and Phyllis Nicolson in the 1940s.[2]
For diffusion equations (and many other equations), it can be shown the Crank–Nicolson method is unconditionally stable.[3] However, the approximate solutions can still contain (decaying) spurious oscillations if the ratio of time step
\Deltat
\Deltax2
The Crank–Nicolson method is based on the trapezoidal rule, giving second-order convergence in time. For linear equations, the trapezoidal rule is equivalent to the implicit midpoint method—the simplest example of a Gauss–Legendre implicit Runge–Kutta method—which also has the property of being a geometric integrator. For example, in one dimension, suppose the partial differential equation is
Letting
u(i\Deltax,n\Deltat)=
n | |
u | |
i |
n | |
F | |
i |
=F
i,n
n | |
u | |
i |
n
n+1
=
x,t,
,
\right) | forward Euler | ||||||||||||||||||||||||||||||||||||||||||||||
=
\left(u,x,t,
,
\right) | backward Euler | ||||||||||||||||||||||||||||||||||||||||||||||
=
\left[
\left(u,x,t,
,
\right)+
x,t,
,
\right) \right] |
u
l{O}(N)
l{O}(N3)
N
The Crank–Nicolson method is often applied to diffusion problems. As an example, for linear diffusion,
\partialu | |
\partialt |
=a
\partial2u | |
\partialx2 |
,
applying a finite difference spatial discretization for the right-hand side, the Crank–Nicolson discretization is then
| ||||||||||||||||
\Deltat |
=
a | |
2(\Deltax)2 |
\left(
n+1 | |
(u | |
i+1 |
-2
n+1 | |
u | |
i |
+
n+1 | |
u | |
i-1 |
)+
n | |
(u | |
i+1 |
-2
n | |
u | |
i |
+
n) \right) | |
u | |
i-1 |
or, letting
r=
a\Deltat | |
2(\Deltax)2 |
-r
n+1 | |
u | |
i+1 |
+(1+2r)
n+1 | |
u | |
i |
-r
n+1 | |
u | |
i-1 |
=r
n | |
u | |
i+1 |
+(1-2r)
n | |
u | |
i |
+r
n. | |
u | |
i-1 |
Given that the terms on the right-hand side of the equation are known, this is a tridiagonal problem, so that
n+1 | |
u | |
i |
A quasilinear equation, such as (this is a minimalistic example and not general)
\partialu | |
\partialt |
=a(u)
\partial2u | |
\partialx2 |
,
would lead to a nonlinear system of algebraic equations, which could not be easily solved as above; however, it is possible in some cases to linearize the problem by using the old value for
a
n(u) | |
a | |
i |
n+1 | |
a | |
i |
(u)
n+1 | |
a | |
i |
(u)
This is a solution usually employed for many purposes when there is a contamination problem in streams or rivers under steady flow conditions, but information is given in one dimension only. Often the problem can be simplified into a 1-dimensional problem and still yield useful information.
Here we model the concentration of a solute contaminant in water. This problem is composed of three parts: the known diffusion equation (
Dx
Ux
k
where
C
N
M
The Crank–Nicolson method (where
i
j
Now we create the following constants to simplify the algebra:
λ=
Dx\Deltat | |
2\Deltax2 |
,
\alpha=
Ux\Deltat | |
4\Deltax |
,
\beta=
k\Deltat | |
2 |
,
and substitute,,,,,,
\alpha
\beta
λ
j+1
j
-\beta
j+1 | |
C | |
Ni |
-(λ+
j+1 | |
\alpha)C | |
i-1 |
+(1+2λ+
j+1 | |
2\beta)C | |
i |
-(λ-
j+1 | |
\alpha)C | |
i+1 |
-\beta
j+1 | |
C | |
Mi |
={}
\beta
j | |
C | |
Ni |
+(λ+
j | |
\alpha)C | |
i-1 |
+(1-2λ-
j | |
2\beta)C | |
i |
+(λ-
j | |
\alpha)C | |
i+1 |
+\beta
j. | |
C | |
Mi |
To model the first channel, we realize that it can only be in contact with the following channel (
M
-(λ+
j+1 | |
\alpha)C | |
i-1 |
+(1+2λ+
j+1 | |
\beta)C | |
i |
-(λ-
j+1 | |
\alpha)C | |
i+1 |
-\beta
j+1 | |
C | |
Mi |
={}
{}+(λ+
j | |
\alpha)C | |
i-1 |
+(1-2λ-
j | |
\beta)C | |
i |
+(λ-
j | |
\alpha)C | |
i+1 |
+\beta
j. | |
C | |
Mi |
In the same way, to model the last channel, we realize that it can only be in contact with the previous channel (
N
-\beta
j+1 | |
C | |
Ni |
-(λ+
j+1 | |
\alpha)C | |
i-1 |
+(1+2λ+
j+1 | |
\beta)C | |
i |
-(λ-
j+1 | |
\alpha)C | |
i+1 |
={}
\beta
j | |
C | |
Ni |
+(λ+
j | |
\alpha)C | |
i-1 |
+(1-2λ-
j | |
\beta)C | |
i |
+(λ-
j. | |
\alpha)C | |
i+1 |
To solve this linear system of equations, we must now see that boundary conditions must be given first to the beginning of the channels:
j | |
C | |
0 |
j+1 | |
C | |
0 |
j | |
C | |
N0 |
j | |
C | |
M0 |
For the last cell of the channels (
z
\left.
\partialC | |
\partialx |
\right|x=z=
Ci+1-Ci-1 | |
2\Deltax |
=0.
This condition is satisfied if and only if (regardless of a null value)
j+1 | |
C | |
i+1 |
=
j+1 | |
C | |
i-1 |
.
Let us solve this problem (in a matrix form) for the case of 3 channels and 5 nodes (including the initial boundary condition). We express this as a linear system problem:
AACj+1 |
=
BBCj |
+d,
where
Cj+1 |
=\begin{bmatrix}
j+1 | |
C | |
11 |
j+1 | |
\ C | |
12 |
j+1 | |
\ C | |
13 |
j+1 | |
\ C | |
14 |
\\
j+1 | |
C | |
21 |
j+1 | |
\ C | |
22 |
j+1 | |
\ C | |
23 |
j+1 | |
\ C | |
24 |
\\
j+1 | |
C | |
31 |
j+1 | |
\ C | |
32 |
j+1 | |
\ C | |
33 |
j+1 | |
\ C | |
34 |
\end{bmatrix}, Cj |
=\begin{bmatrix}
j | |
C | |
12 |
j | |
\ C | |
13 |
j | |
\ C | |
14 |
\\
j | |
C | |
22 |
j | |
\ C | |
23 |
j | |
\ C | |
24 |
\\
j | |
C | |
32 |
j | |
\ C | |
33 |
j \end{bmatrix}. | |
\ C | |
34 |
Now we must realize that AA and BB should be arrays made of four different subarrays (remember that only three channels are considered for this example, but it covers the main part discussed above):
AA=\begin{bmatrix} AA1&AA3&0\\ AA3&AA2&AA3\\ 0&AA3&AA1 \end{bmatrix}, BB=\begin{bmatrix} BB1&-AA3&0\\ -AA3&BB2&-AA3\\ 0&-AA3&BB1 \end{bmatrix},
where the elements mentioned above correspond to the next arrays, and an additional 4×4 full of zeros. Please note that the sizes of AA and BB are 12×12:
AA1=\begin{bmatrix} (1+2λ+\beta)&-(λ-\alpha)&0&0\\ -(λ+\alpha)&(1+2λ+\beta)&-(λ-\alpha)&0\\ 0&-(λ+\alpha)&(1+2λ+\beta)&-(λ-\alpha)\\ 0&0&-2λ&(1+2λ+\beta) \end{bmatrix},
AA2=\begin{bmatrix} (1+2λ+2\beta)&-(λ-\alpha)&0&0\\ -(λ+\alpha)&(1+2λ+2\beta)&-(λ-\alpha)&0\\ 0&-(λ+\alpha)&(1+2λ+2\beta)&-(λ-\alpha)\\ 0&0&-2λ&(1+2λ+2\beta) \end{bmatrix},
AA3=\begin{bmatrix} -\beta&0&0&0\\ 0&-\beta&0&0\\ 0&0&-\beta&0\\ 0&0&0&-\beta \end{bmatrix},
BB1=\begin{bmatrix} (1-2λ-\beta)&(λ-\alpha)&0&0\\ (λ+\alpha)&(1-2λ-\beta)&(λ-\alpha)&0\\ 0&(λ+\alpha)&(1-2λ-\beta)&(λ-\alpha)\\ 0&0&2λ&(1-2λ-\beta) \end{bmatrix},
BB2=\begin{bmatrix} (1-2λ-2\beta)&(λ-\alpha)&0&0\\ (λ+\alpha)&(1-2λ-2\beta)&(λ-\alpha)&0\\ 0&(λ+\alpha)&(1-2λ-2\beta)&(λ-\alpha)\\ 0&0&2λ&(1-2λ-2\beta) \end{bmatrix}.
The d vector here is used to hold the boundary conditions. In this example it is a 12×1 vector:
d=\begin{bmatrix} (λ+
j+1 | |
\alpha)(C | |
10 |
+
j) | |
C | |
10 |
\ 0\ 0\ 0\\ (λ+
j+1 | |
\alpha)(C | |
20 |
+
j) | |
C | |
20 |
\ 0\ 0\ 0\\ (λ+
j+1 | |
\alpha)(C | |
30 |
+
j) | |
C | |
30 |
\ 0\ 0\ 0 \end{bmatrix}.
To find the concentration at any time, one must iterate the following equation:
Cj+1 |
=AA-1
(BBCj |
+d).
When extending into two dimensions on a uniform Cartesian grid, the derivation is similar and the results may lead to a system of band-diagonal equations rather than tridiagonal ones. The two-dimensional heat equation
\partialu | |
\partialt |
=a\nabla2u,
\partialu | |
\partialt |
=a\left(
\partial2u | |
\partialx2 |
+
\partial2u | |
\partialy2 |
\right)
can be solved with the Crank–Nicolson discretization of
\begin{align}
n+1 | |
u | |
i,j |
={}&
n | |
u | |
i,j |
+
1 | |
2 |
a\Deltat | |
(\Deltax)2 |
n+1 | |
[(u | |
i+1,j |
+
n+1 | |
u | |
i-1,j |
+
n+1 | |
u | |
i,j+1 |
+
n+1 | |
u | |
i,j-1 |
-
n+1 | |
4u | |
i,j |
)\\ &+
n | |
(u | |
i+1,j |
+
n | |
u | |
i-1,j |
+
n | |
u | |
i,j+1 |
+
n | |
u | |
i,j-1 |
-
n)], \end{align} | |
4u | |
i,j |
assuming that a square grid is used, so that
\Deltax=\Deltay
\mu=
a\Deltat | |
(\Deltax)2 |
.
For the Crank–Nicolson numerical scheme, a low CFL number is not required for stability, however, it is required for numerical accuracy. We can now write the scheme as
(1+
n+1 | |
2\mu)u | |
i,j |
-
\mu | |
2 |
n+1 | |
\left(u | |
i+1,j |
+
n+1 | |
u | |
i-1,j |
+
n+1 | |
u | |
i,j+1 |
+
n+1 | |
u | |
i,j-1 |
\right)
=(1-
n | |
2\mu)u | |
i,j |
+
\mu | |
2 |
n | |
\left(u | |
i+1,j |
+
n | |
u | |
i-1,j |
+
n | |
u | |
i,j+1 |
+
n | |
u | |
i,j-1 |
\right).
Solving such a linear system is costly. Hence an alternating-direction implicit method can be implemented to solve the numerical PDE, whereby one dimension is treated implicitly, and other dimension explicitly for half of the assigned time step and conversely for the remainder half of the time step. The benefit of this strategy is that the implicit solver only requires a tridiagonal matrix algorithm to be solved. The difference between the true Crank–Nicolson solution and ADI approximated solution has an order of accuracy of
O(\Deltat4)
Because the Crank–Nicolson method is implicit, it is generally impossible to solve exactly. Instead, an iterative technique should be used to converge to the solution. One option is to use Newton's method to converge on the prediction, but this requires the computation of the Jacobian. For a high-dimensional system like those in computational fluid dynamics or numerical relativity, it may be infeasible to compute this Jacobian.
A Jacobian-free alternative is fixed-point iteration. If
f
\Phi(x)=x0+
h | |
2 |
\left[f(x0)+f(x)\right].
x(i+1)=\Phi(x(i))
\Theta(x,\alpha)=\alphax+(1-\alpha)\Phi(x)
\alpha\in(0,1)
xi+1=\alphaxi+(1-\alpha)\left[x0+
h | |
2 |
\left(f(x0)+f(xi)\right)\right],
where
xi
xi-1
Even for high-dimensional systems, iteration of this map can converge surprisingly quickly.
Because a number of other phenomena can be modeled with the heat equation (often called the diffusion equation in financial mathematics), the Crank–Nicolson method has been applied to those areas as well.[5] Particularly, the Black–Scholes option pricing model's differential equation can be transformed into the heat equation, and thus numerical solutions for option pricing can be obtained with the Crank–Nicolson method.
The importance of this for finance is that option pricing problems, when extended beyond the standard assumptions (e.g. incorporating changing dividends), cannot be solved in closed form, but can be solved using this method. Note however, that for non-smooth final conditions (which happen for most financial instruments), the Crank–Nicolson method is not satisfactory as numerical oscillations are not damped. For vanilla options, this results in oscillation in the gamma value around the strike price. Therefore, special damping initialization steps are necessary (e.g., fully implicit finite difference method).
ut=auxx