Finite Difference Methods for the Poisson Equation¶
John S Butler john.s.butler@tudublin.ie Course Notes Github¶
Overview¶
This notebook will focus on numerically approximating a homogenous second order Poisson Equation.
The Differential Equation¶
The general two dimensional Poisson Equation is of the form: $∂2u∂y2+∂2u∂x2=f(x,y), (x,y)∈Ω=(0,1)×(0,1),withboundaryconditionsU(x,y)=g(x,y), (x,y)∈δΩ - boundary.$
Homogenous Poisson Equation¶
This notebook will implement a finite difference scheme to approximate the homogenous form of the Poisson Equation f(x,y)=0: $∂2u∂y2+∂2u∂x2=0.withtheBoundaryConditions:u(x,0)=sin(2πx), 0≤x≤1, lower,u(x,1)=sin(2πx), 0≤x≤1, upper,u(0,y)=2sin(2πy), 0≤y≤1, left,u(1,y)=2sin(2πy), 0≤y≤1, right.$
# LIBRARY
# vector manipulation
import numpy as np
# math functions
import math
# THIS IS FOR PLOTTING
%matplotlib inline
import matplotlib.pyplot as plt # side-stepping mpl backend
import warnings
warnings.filterwarnings("ignore")
from IPython.display import HTML
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
Discete Grid¶
The region Ω=(0,1)×(0,1) is discretised into a uniform mesh Ωh. In the x and y directions into N steps giving a stepsize of $h=1−0N,resultinginx[i]=0+ih, i=0,1,...,N,andx[j]=0+jh, j=0,1,...,N,TheFigurebelowshowsthediscretegridpointsforN=10$, the known boundary conditions (green), and the unknown values (red) of the Poisson Equation.
N=10
h=1/N
x=np.arange(0,1.0001,h)
y=np.arange(0,1.0001,h)
X, Y = np.meshgrid(x, y)
fig = plt.figure()
plt.plot(x[1],y[1],'ro',label='unknown');
plt.plot(X,Y,'ro');
plt.plot(np.ones(N+1),y,'go',label='Boundary Condition');
plt.plot(np.zeros(N+1),y,'go');
plt.plot(x,np.zeros(N+1),'go');
plt.plot(x, np.ones(N+1),'go');
plt.xlim((-0.1,1.1))
plt.ylim((-0.1,1.1))
plt.xlabel('x')
plt.ylabel('y')
plt.gca().set_aspect('equal', adjustable='box')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.title(r'Discrete Grid $\Omega_h,$ h= %s'%(h),fontsize=24,y=1.08)
plt.show();

Boundary Conditions¶
The discrete boundary conditions are $wi0=w[i,0]=sin(2πx[i]), for i=0,...,10, upper,$
The Figure below plots the boundary values of w[i,j].
w=np.zeros((N+1,N+1))
for i in range (0,N):
w[i,0]=np.sin(2*np.pi*x[i]) #left Boundary
w[i,N]=np.sin(2*np.pi*x[i]) #Right Boundary
for j in range (0,N):
w[0,j]=2*np.sin(2*np.pi*y[j]) #Lower Boundary
w[N,j]=2*np.sin(2*np.pi*y[j]) #Upper Boundary
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot a basic wireframe.
ax.plot_wireframe(X, Y, w,color='r', rstride=10, cstride=10)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('w')
plt.title(r'Boundary Values',fontsize=24,y=1.08)
plt.show()

Numerical Method¶
The Poisson Equation is discretised using δ2x is the central difference approximation of the second derivative in the x direction $δ2x=1h2(wi+1j−2wij+wi−1j),and\delta_y^2isthecentraldifferenceapproximationofthesecondderivativeintheydirectionδ2y=1h2(wij+1−2wij+wij−1).ThegivesthePoissonDifferenceEquation,−(δ2xwij+δ2ywij)=fij (xi,yj)∈Ωh,wij=gij (xi,yj)∈∂Ωh,wherew_ijisthenumericalapproximationofUatx_iandy_j.ExpandingthethePoissonDifferenceEquationgivesthefivepointmethod,−(wi−1j+wij−1−4wij+wij+1+wi+1j)=h2fijfori=1,…,N-1andj=1,…,N-1$ which can be written
Matrix form¶
This can be written as a system of (N−1)×(N−1) equations can be arranged in matrix form $Aw=r,whereAisan(N-1)^2\times(N-1)^2matrixmadeupofthefollowingblocktridiagonalstructure(TI00...ITI00............0ITI....0IT),whereIdenotesanN-1 \times N-1identitymatrixandTisthetridiagonalmatrixoftheform:T=(−4100...1−4100............01−41....01−4).TheplotbelowshowsthematrixAanditsinverseA^{-1}$ as a colourplot.
N2=(N-1)*(N-1)
A=np.zeros((N2,N2))
## Diagonal
for i in range (0,N-1):
for j in range (0,N-1):
A[i+(N-1)*j,i+(N-1)*j]=-4
# LOWER DIAGONAL
for i in range (1,N-1):
for j in range (0,N-1):
A[i+(N-1)*j,i+(N-1)*j-1]=1
# UPPPER DIAGONAL
for i in range (0,N-2):
for j in range (0,N-1):
A[i+(N-1)*j,i+(N-1)*j+1]=1
# LOWER IDENTITY MATRIX
for i in range (0,N-1):
for j in range (1,N-1):
A[i+(N-1)*j,i+(N-1)*(j-1)]=1
# UPPER IDENTITY MATRIX
for i in range (0,N-1):
for j in range (0,N-2):
A[i+(N-1)*j,i+(N-1)*(j+1)]=1
Ainv=np.linalg.inv(A)
fig = plt.figure(figsize=(12,4));
plt.subplot(121)
plt.imshow(A,interpolation='none');
clb=plt.colorbar();
clb.set_label('Matrix elements values');
plt.title('Matrix A ',fontsize=24)
plt.subplot(122)
plt.imshow(Ainv,interpolation='none');
clb=plt.colorbar();
clb.set_label('Matrix elements values');
plt.title(r'Matrix $A^{-1}$ ',fontsize=24)
fig.tight_layout()
plt.show();

The vector w is of length (N−1)×(N−1) which made up of N−1 subvectors wj of length N−1 of the form $wj=(w1jw2j..wN−2jwN−1j).Thevector\mathbf{r}isoflength(N-1)\times(N-1)whichmadeupofN-1subvectorsoftheform\mathbf{r}_j=-h^2\mathbf{f}j-\mathbf{bx}{j}-\mathbf{by}_j,where\mathbf{bx}_j isthevectorofleftandrightboundaryconditionsbxj=(w0j0..0wNj),$
for j=1,..,N−1, where byj is the vector of the lower boundary condition for j=1,
upper boundary condition for j=N−1
for j=2,...,N−2 $byj=0,andfj=−(00..00)forj=1,…,N-1$.
r=np.zeros(N2)
# vector r
for i in range (0,N-1):
for j in range (0,N-1):
r[i+(N-1)*j]=-h*h*0
# Boundary
b_bottom_top=np.zeros(N2)
for i in range (0,N-1):
b_bottom_top[i]=np.sin(2*np.pi*x[i+1]) #Bottom Boundary
b_bottom_top[i+(N-1)*(N-2)]=np.sin(2*np.pi*x[i+1])# Top Boundary
b_left_right=np.zeros(N2)
for j in range (0,N-1):
b_left_right[(N-1)*j]=2*np.sin(2*np.pi*y[j+1]) # Left Boundary
b_left_right[N-2+(N-1)*j]=2*np.sin(2*np.pi*y[j+1])# Right Boundary
b=b_left_right+b_bottom_top
Results¶
To solve the system for w invert the matrix A $Aw=r,suchthatw=A−1r.Lastly,as\mathbf{w}$ is in vector it has to be reshaped into grid form to plot.
The figure below shows the numerical approximation of the homogeneous Equation.
C=np.dot(Ainv,r-b)
w[1:N,1:N]=C.reshape((N-1,N-1))
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111, projection='3d');
# Plot a basic wireframe.
ax.plot_wireframe(X, Y, w,color='r');
ax.set_xlabel('x');
ax.set_ylabel('y');
ax.set_zlabel('w');
plt.title(r'Numerical Approximation of the Poisson Equation',fontsize=24,y=1.08);
plt.show();

Consistency and Convergence¶
We now ask how well the grid function determined by the five point scheme approximates the exact solution of the Poisson problem.
Consistency¶
Consitency (Definition)¶
Let $∇2h(φ)=−(φi−1j+φij−1−4φij+φij+1+φi+1j)denotethefinitedifferenceapproximationassociatedwiththegrid\Omega_hhavingthemeshsizeh,toapartialdifferentialoperator∇2(φ)=∂2φ∂x2+∂2φ∂y2definedonasimplyconnected,openset\Omega \subset R^2.Foragivenfunction\varphi\in C^{\infty}(\Omega),thetruncationerrorof\nabla^2_hisτh(x)=(∇2−∇2h)φ(x)Theapproximation\nabla^2_hisconsistentwith\nabla^2iflimh→0τh(x)=0,forall\mathbf{x} \in Dandall\varphi \in C^{\infty}(\Omega).Theapproximationisconsistenttoorderpif\tau_h(\mathbf{x})=O(h^p)$.
In other words a method is consistent with the differential equation it is approximating.
Proof of Consistency¶
The five-point difference analog ∇2h is consistent to order 2 with ∇2.
Proof
Pick φ∈C∞(D), and let (x,y)∈Ω be a point such that (x±h,y),(x,y±h)∈Ω⋃∂Ω. By the Taylor Theorem
where ζ±∈(x−h,x+h). Adding this pair of equation together and rearranging , we get $1h2[φ(x+h,y)−2φ(x,y)+φ(x−h,y)]−∂2φ∂x2(x,y)=h24![∂4φ∂x4(ζ+,y)+∂4φ∂x4(ζ−,y)]Bytheintermediatevaluetheorem[∂4φ∂x4(ζ+,y)+∂4φ∂x4(ζ−,y)]=2∂4φ∂x4(ζ,y),forsome\zeta \in (x-h,x+h).Therefore,δ2x(x,y)=∂2φ∂x2(x,y)+h22!∂4φ∂x4(ζ,y)Similarreasoningshowsthatδ2y(x,y)=∂2φ∂y2(x,y)+h22!∂4φ∂y4(x,η)forsome\eta \in (y-h,y+h).Weconcludethat\tau_h(x,y)=(\nabla-\nabla_h)\varphi(x,y)=O(h^2).$
Convergence¶
Definition¶
Let ∇2hw(xj)=f(xj) be a finite difference approximation, defined on a grid mesh size h, to a PDE ∇2U(x)=f(x) on a simply connected set D⊂Rn. Assume that w(x,y)=U(x,y) at all points (x,y) on the boundary ∂Ω. The finite difference scheme converges (or is convergent) if $maxj|U(xj)−w(xj)|→0 as h→0.$
Theorem (DISCRETE MAXIMUM PRINCIPLE).¶
If ∇2hVij≥0 for all points (xi,yj)∈Ωh, then $max(xi,yj)∈ΩhVij≤max(xi,yj)∈∂ΩhVij,If\nabla^2_hV_{ij}\leq 0forallpoints(x_i,y_j) \in \Omega_h,thenmin(xi,yj)∈ΩhVij≥min(xi,yj)∈∂ΩhVij.$
Propositions¶
The zero grid function for which Uij=0 for all (xi,yj)∈Ωh⋃∂Ωh is the only solution to the finite difference problem $∇2hUij=0 for (xi,yj)∈Ωh,Uij=0 for (xi,yj)∈∂Ωh.$
For prescribed grid functions fij and gij, there exists a unique solution to the problem $∇2hUij=fij for (xi,yj)∈Ωh,Uij=gij for (xi,yj)∈∂Ωh.$
Definition¶
For any grid function V:Ωh⋃∂Ωh→R, $||V||Ω=max(xi,yj)∈Ωh|Vij|,||V||∂Ω=max(xi,yj)∈∂Ωh|Vij|.$
Lemma¶
If the grid function V:Ωh⋃∂Ωh→R satisfies the boundary condition Vij=0 for (xi,yj)∈∂Ωh, then $||V||Ω≤18||∇2hV||Ω.$
Given these Lemmas and Propositions, we can now prove that the solution to the five point scheme ∇2h is convergent to the exact solution of the Poisson Equation ∇2.
Convergence Theorem¶
Let U be a solution to the Poisson equation and let w be the grid function that satisfies the discrete analog $−∇2hwij=fij for (xi,yj)∈Ωh,wij=gij for (xi,yj)∈∂Ωh.ThenthereexistsapositiveconstantKsuchthat||U−w||Ω≤KMh2,whereM={||∂4U∂x4||∞,||∂4U∂y4||∞}$
Proof
The statement of the theorem assumes that U∈C4(ˉΩ). This assumption holds if f and g are smooth enough. \begin{proof} Following from the proof of the Proposition we have $(∇2h−∇2)Uij=h212[∂4U∂x4(ζi,yj)+∂4U∂y4(xi,ηj)],forsome\zeta \in (x_{i-1},x_{i+1})and\eta_j\in(y_{j-1},y_{j+1}).Therefore,−∇2hUij=fij−h212[∂4U∂x4(ζi,yj)+∂4U∂y4(xi,ηj)].Ifwesubtractfromthistheidentityequation-\nabla_h^2w_{ij}=f_{ij}andnotethatU-wvanisheson\partial\Omega_h,wefindthat∇2h(Uij−wij)=h212[∂4U∂x4(ζi,yj)+∂4U∂y4(xi,ηj)].$ It follows that