Finite Difference Methods for the Laplacian 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 which is the Laplacian Equation.
The Differential Equation#
The general two dimensional Poisson Equation is of the form:
with boundary conditions
Homogenous Poisson Equation#
This notebook will implement a finite difference scheme to approximate the homogenous form of the Poisson Equation
with the Boundary Conditions:
# 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
resulting in
and
The Figure below shows the discrete grid points for
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
The Figure below plots the boundary values of
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
and
The gives the Poisson Difference Equation,
where
for
Matrix form#
This can be written as a system of
where
where
The plot below shows the matrix
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
The vector
for
upper boundary condition for
for
and
for
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
such that
Lastly, as
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
denote the finite difference approximation associated with the grid
defined on
a simply connected, open set
The approximation
for all
In other words a method is consistent with the differential equation it is approximating.
Proof of Consistency#
The five-point difference analog
Proof
Pick
where
By the intermediate value theorem
for some
Similar reasoning shows that
for some
Convergence#
Definition#
Let
Theorem (DISCRETE MAXIMUM PRINCIPLE).#
If
If
Propositions#
The zero grid function for which
for all is the only solution to the finite difference problem
For prescribed grid functions
and , there exists a unique solution to the problem
Definition#
For any grid function
Lemma#
If the grid function
Given these Lemmas and Propositions, we can now prove that the solution to the five point scheme
Convergence Theorem#
Let
Then there exists a positive constant
where
Proof
The statement of the theorem assumes that
for some
If we subtract from this the identity equation
It follows that