We have 2D heat equation of the form
$$ v_t = \frac{1}{2-x^2-y^2} (v_{xx}+v_{yy}), \; \; \; \; (x,y) \in (-1/2,1/2) \times (-1/2,1/2) $$
We can solve this equation for example using separation of variables and we obtain exact solution
$$ v(x,y,t) = e^{-t} e^{-(x^2+y^2)/2} $$
Im trying to implement the Crank-nicolson and the Peaceman-Rachford ADI scheme for this problem using MATLAB. Can someone help me out how can we do this using matlab?
Best Answer
Let us consider the equation $v_t = \frac{1}{2-x^2-y^2} (v_{xx}+v_{yy})$ with initial and boundary conditions $$v|_{t=0} = e^{-(x^2+y^2)/2}, \quad v|_{y=\pm1/2} = e^{-(t+x^2/2+1/8)}, \quad v|_{x=\pm1/2} = e^{-(t+y^2/2+1/8)}.$$ For the Crank-Nicolson part, we implement the scheme $$ \frac{v_{i,j}^{n+1} - v_{i,j}^n}{k} = \frac{1}{2-{x_i}^2-{y_j}^2} \frac{1}{2 h^2}\left((\delta^2_x + \delta^2_y)v_{i,j}^{n} + (\delta^2_x + \delta^2_y)v_{i,j}^{n+1}\right) , $$ where $v_{i,j}^n \simeq v (x_i,y_j, n k)$ denotes the numerical solution, $(x_i,y_j) = \big(i-\frac{N+1}{2}, j-\frac{N+1}{2}\big) h$ are the grid-node coordinates, $h=\frac{1}{N-1}$ is the mesh size, and $k$ is the time step. The second-order finite difference operators are defined by $$\delta^2_x v_{i,j}^{n} = v_{i+1,j}^n-2v_{i,j}^n+v_{i-1,j}^n, \qquad \delta^2_y v_{i,j}^{n} = v_{i,j+1}^n-2v_{i,j}^n+v_{i,j-1}^n\, .$$ Separating implicit and explicit parts, we rewrite the method as $$ \left(1 - \frac{\lambda_{i,j}}{2} (\delta^2_x + \delta^2_y)\right) v_{i,j}^{n+1} = \left(1 + \frac{\lambda_{i,j}}{2} (\delta^2_x + \delta^2_y)\right) v_{i,j}^{n}, \qquad \lambda_{i,j} = \frac{k/h^2}{2-{x_i}^2-{y_j}^2} $$ for $1< i,j < N$, where $\lambda_{i,j}$ is the mesh Fourier number. Sometimes, the modified method $$ \left(1 - \frac{\lambda_{i,j}}{2} \delta^2_x\right) \left(1 - \frac{\lambda_{i,j}}{2} \delta^2_y\right) v_{i,j}^{n+1} = \left(1 + \frac{\lambda_{i,j}}{2} \delta^2_x\right) \left(1 + \frac{\lambda_{i,j}}{2} \delta^2_y\right) v_{i,j}^{n} $$ is preferred, which approximates the original Crank-Nicolson method up to negligible terms. This method rewrites as the two-step ADI method, which intermediate step ${\bf v}^*$ satisfies \begin{aligned} \left(1 - \frac{\lambda_{i,j}}{2} \delta^2_x\right) v_{i,j}^{*} &= \left(1 + \frac{\lambda_{i,j}}{2} \delta^2_y\right) v_{i,j}^{n} \\ \left(1 - \frac{\lambda_{i,j}}{2} \delta^2_y\right) v_{i,j}^{n+1} &= \left(1 + \frac{\lambda_{i,j}}{2} \delta^2_x\right) v_{i,j}^{*} \, . \end{aligned} A Matlab implementation of Crank-Nicolson is shown below, along with numerical results.