# 2D Laplace problem with mixed boundary conditions using Conjugate Gradients

I am being asked for one of my classes to solve 2D Laplace equations with mixed boundary conditions using the Conjugate Gradient method. The equations and conditions are given as: $$\frac{\partial^2p}{\partial x^2} + \frac{\partial^2p}{\partial y^2}=0\\ p = 0\text{ at x=0}\qquad p=k\text{ at x=1}\\ \frac{\partial p}{\partial y}=0\text{ at y=0, 1}$$ The problem can be represented as $\textbf{Ap}=\textbf{b}$, and should be solvable by finding matrices $\textbf{A}$ and $\textbf{b}$ and applying the Conjugate Gradient algorithm on them. I know that the general form of the $\textbf{A}$ matrix can be written as: $$\begin{pmatrix} \textbf{J} & \textbf{I} & \cdots & \textbf{0}\\ \textbf{I} & \textbf{J} & \cdots & \textbf{0}\\ \vdots & \vdots & \ddots & \vdots\\ \textbf{0} & \cdots & \textbf{I} & \textbf{J} \end{pmatrix} \text{with }\textbf{J}= \begin{pmatrix} 4&-1&\cdots&0\\ -1 & 4 &\cdots & 0\\ \vdots&\vdots&\ddots&\vdots\\ 0&\cdots&-1&4 \end{pmatrix}$$ where $\textbf{I}$ is the unit matrix.

Translation of the Dirichlet conditions makes the matrix $\textbf{A}$ and vector $\textbf{b}$ become $$\textbf{A}= \begin{pmatrix} \textbf{I}&\textbf{0}&\textbf{0}&\cdots&\textbf{0}&\textbf{0}\\ \textbf{I}&\textbf{J}&\textbf{I}&\cdots&\textbf{0}&\textbf{0}\\ \textbf{0}&\textbf{I}&\textbf{J}&\cdots&\textbf{0}&\textbf{0}\\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots\\ \textbf{0}&\textbf{0}&\textbf{0}&\cdots&\textbf{J}&\textbf{I}\\ \textbf{0}&\textbf{0}&\textbf{0}&\cdots&\textbf{0}&\textbf{I}\\ \end{pmatrix} \textbf{b}= \begin{pmatrix} 0\\0\\\vdots\\k\\k \end{pmatrix}$$ while the Neumann conditions will modify matrix $\textbf{J}$ as $$\textbf{J}= \begin{pmatrix} 1&-1&0&\cdots&0&0\\ -1&4&-1&\cdots&0&0\\ 0&-1&4&\cdots&0&0\\ \vdots&\vdots&\vdots&\ddots&\vdots&\vdots\\ 0&0&0&\cdots&4&-1\\ 0&0&0&\cdots&-1&1 \end{pmatrix}$$

As you can see, this has made my matrix $\textbf{A}$ become asymmetric and indefinite. What makes it not usable for the Conjugate Gradient algorithm...

Do you know how to still apply the conjugate gradient method on this problem, without having to use a preconditioner?

For the matrix $A$ and vector $b$ subtract the first (block) row from the second row, and, analogously, subtract the last row from the one before last. In such way you eliminate the unknowns for Dirichlet boundary condition and the resulting (remaining) matrix $A$ becomes again symmetric and positive definite.
$Ap = b$ $A(p_{interior}+p_{BCs}) = b$ $A p_{interior} = b - Ap_{BCs}$