E. Finite Difference Method
Crank-Nicolson Summary
Neumann Boundary Conditions
When Neumann boundary conditions \(\beta_L^\prime, \beta_R^\prime\) are given instead of Dirichlet, we set \begin{align*} &-\frac{3}{2\Delta x} U_0 + \frac{4}{2\Delta x} U_1 -\frac{1}{2\Delta x} U_2 = \beta_L^\prime, \\ &\frac{3}{2\Delta x} U_m - \frac{4}{2\Delta x} U_{m-1} +\frac{1}{2\Delta x} U_{m-2} = \beta_R^\prime, \end{align*} where the left hand side are 2nd order approximation of the \(u_x\) values at both boundaries, derived from the method of undetermined coefficients. This can be computed by, for example, the Finite Difference Coefficients Calculator. Equivalently, we can write \begin{align*} U_0 &= -\frac{2\Delta x}{3}\beta_L^\prime + \frac{4}{3} U_1 - \frac{1}{3} U_2, \\ U_m &= \frac{2\Delta x}{3}\beta_R^\prime + \frac{4}{3} U_{m-1} - \frac{1}{3} U_{m-2}. \end{align*} Replacing all \(\beta_L\) and \(\beta_R\) in the above linear system by these new expressions of \(U_0\) and \(U_m\), we obtain the following linear system with Neumann BCs: \begin{align*} & \begin{pmatrix} 1+2\alpha-\frac{4}{3}\alpha & -\alpha+\frac{1}{3}\alpha&&&&\\ -\alpha & 1+2\alpha & -\alpha&&&\\ & -\alpha & 1+2\alpha & -\alpha&&\\ &&& \ddots &\\ &&& -\alpha & 1+2\alpha & -\alpha \\ &&&& -\alpha+\frac{1}{3}\alpha & 1+2\alpha-\frac{4}{3}\alpha \end{pmatrix}U^{n+1} + \begin{pmatrix} \frac{2\alpha\Delta x}{3}(\beta_L^\prime)^{n+1}\\ 0\\ 0\\ \vdots\\ 0\\ -\frac{2\alpha\Delta x}{3}(\beta_R^\prime)^{n+1}\\ \end{pmatrix} \\ &= \begin{pmatrix} 1-2\alpha +\frac{4}{3}\alpha & \alpha-\frac{1}{3}\alpha&&&&\\ \alpha & 1-2\alpha & \alpha&&&\\ & \alpha & 1-2\alpha & \alpha&&\\ &&& \ddots &\\ &&& \alpha & 1-2\alpha & \alpha \\ &&&& \alpha-\frac{1}{3}\alpha & 1-2\alpha+\frac{4}{3}\alpha \end{pmatrix}U^{n} + \begin{pmatrix} -\frac{2\alpha\Delta x}{3}(\beta_L^\prime)^{n}\\ 0\\ 0\\ \vdots\\ 0\\ \frac{2\alpha\Delta x}{3}(\beta_R^\prime)^{n}\\ \end{pmatrix}. \end{align*}
Nonlinear Term
When there is a nonlinear term in the equation, say the PDE is \(u_t = Lu + N(u)\), then the corresponding Crank-Nicolson method is \begin{align*} U^{n+1} &= U^{n} + \int_{t_n}^{t_{n+1}} \dot U\,dt\\ &= U^{n} + \int_{t_n}^{t_{n+1}} L_h U + N(U)\,dt\\ &\approx U^{n} + \Delta t\left(\frac{L_h U^{n} + N(U^{n}) + L_h U^{n+1} + N(U^{n+1})}{2}\right). \end{align*} Thus \begin{align*} \left(I - \frac{\Delta t}{2}L_h \right)U^{n+1} - \frac{\Delta t}{2} N(U^{n+1}) = \left(I + \frac{\Delta t}{2}L_h \right)U^{n} + \frac{\Delta t}{2} N(U^{n}). \end{align*} Now on the right hand side apply the 2nd order approximation (in \(t\)) \begin{align*} N(U^{n+1}) = N(U^{n}) + (U^{n+1} - U^{n})\circ N^\prime(U^n), \end{align*} where \(\circ\) denotes the element-wise product. We obtain \begin{align*} &\left(I - \frac{\Delta t}{2}L_h \right)U^{n+1} - \frac{\Delta t}{2} \left(N(U^{n}) + (U^{n+1} - U^{n})\circ N^\prime(U^n)\right) = \left(I + \frac{\Delta t}{2}L_h \right)U^{n} + \frac{\Delta t}{2} N(U^{n})\\ \implies\quad&\left(I - \frac{\Delta t}{2}L_h \right)U^{n+1} - \frac{\Delta t}{2} N^\prime(U^n)\circ U^{n+1} = \left(I + \frac{\Delta t}{2}L_h\right)U^{n} - \frac{\Delta t}{2} N^\prime(U^n)\circ U^{n} + \Delta t N(U^n)\\ \implies\quad&\left(I - \frac{\Delta t}{2}L_h - \frac{\Delta t}{2} N^\prime(U^n)\right)U^{n+1} = \left(I + \frac{\Delta t}{2}L_h - \frac{\Delta t}{2} N^\prime(U^n)\right)U^{n} + \Delta t N(U^n). \end{align*} In most cases this requires reconstruction of the matrix at each time step. Note that the above argument is not specific to finite difference method. Same argument works for spectral methods too.
Alternating-Direction Implicit Method (No Cross Term)
ADI With Cross Term
To solve \(u_t = u_{xx} + u_{yy} + u_{xy}\), first discretize in space to get the system of ODEs
where \begin{align*} \delta_x^0 &= \frac{S_x-S_x^{-1}}{2\Delta x}, \\ \delta_y^0 &= \frac{S_y-S_y^{-1}}{2\Delta y}. \end{align*} No way to split the operator into separate ones in different directions, we treat the mixed derivative fully explicitly and write \begin{align*} \left(I - \frac{\Delta t}{2}(\delta_x^+\delta_x^- + \delta_y^+\delta_y^-) \right)U^{n+1} = \left(I + \frac{\Delta t}{2}(\delta_x^+\delta_x^- + \delta_y^+\delta_y^-) + \Delta t\delta_x^0\delta_y^0\right)U^{n}. \end{align*} By splitting the opertor only on the left, we obtain \begin{align*} \left(I - \frac{\Delta t}{2}\delta_x^+\delta_x^- \right)\left(I - \frac{\Delta t}{2}\delta_y^+\delta_y^- \right)U^{n+1} = \left(I + \frac{\Delta t}{2}(\delta_x^+\delta_x^- + \delta_y^+\delta_y^-) + \Delta t\delta_x^0\delta_y^0\right)U^{n}. \end{align*} A variant of this scheme is the Douglas-Rachford scheme \begin{align*} &\left(I - \frac{\Delta t}{2}\delta_x^+\delta_x^- \right)V^n = \left(I + \frac{\Delta t}{2}\delta_x^+\delta_x^- + \Delta t\delta_y^+\delta_y^- + \Delta t\delta_x^0\delta_y^0\right)U^{n}, \\ &\left(I - \frac{\Delta t}{2}\delta_y^+\delta_y^- \right)U^{n+1} = V^n - \frac{\Delta t}{2}\delta_y^+\delta_y^- U^n. \end{align*} Since the cross term is not integrated with the trapezoidal rule, the convergence order in time of the Douglas-Rachford reduces to 1. To recover 2nd order convergence in time, apply a predictor-corrector scheme:
Predictor: \begin{align*} \left(I - \frac{\Delta t}{2}\delta_x^+\delta_x^- \right)V_1^{n} &= \left(I + \frac{\Delta t}{2}\delta_x^+\delta_x^- + \Delta t\delta_y^+\delta_y^- + \Delta t\delta_x^0\delta_y^0\right)U^{n}, \\ \left(I - \frac{\Delta t}{2}\delta_y^+\delta_y^- \right)V_2^{n} &= V_1^n - \frac{\Delta t}{2}\delta_y^+\delta_y^- U^n. \end{align*}
Corrector: \begin{align*} &\left(I - \frac{\Delta t}{2}\delta_x^+\delta_x^- \right)V_3^{n} = \left(I + \frac{\Delta t}{2}\delta_x^+\delta_x^- + \Delta t \delta_y^+\delta_y^- + \frac{\Delta t}{2}\delta_x^0\delta_y^0\right)U^{n} + \frac{\Delta t}{2} V_2^{n}, \\ &\left(I - \frac{\Delta t}{2}\delta_y^+\delta_y^- \right)U^{n+1} = V_3^n - \frac{\Delta t}{2}\delta_y^+\delta_y^- U^n. \end{align*}
Here the Douglas-Rachford scheme is first applied and the result \(V_2^{n}\) is a predictor of the solution at time \(t_{n+1}\), to be corrected. This scheme has 2nd order convergence both in time and space.