Project 10: Numerical Solutions of the 2D Elliptic Equation
Objective
To numerically solve the 2D Piosson equation using finite difference methods and analyze the results.
Problem Description
The 2D Laplacian equation, often appearing in contexts like heat conduction, electrostatics, and fluid dynamics, is given by:
where
-
Domain: Let’s consider a rectangular domain
defined by the coordinates where and . -
Boundary condition: we use Dirichlet boundary condition for this problem
-
Left Edge (x = 0): Let’s set
to be a linear function of . -
Right Edge (x = 1): For
, let’s use a constant function. -
Bottom Edge (y = 0): Let
be a sinusoidal function. -
Top Edge (y = 1): Finally, for
, we can choose a quadratic function.
-
Step 1: Discretization and initialization
Discretize the domain
import numpy as np
N = 10
x = np.linspace(0,1,N+1) # N+1 points in x direction
y = np.linspace(0,1,N+1)
dx = x[1]-x[0]
dy = y[1]-y[0]
Step 3: Boundary Condition Function
Define the boundary condition functions
def f1(y):
return
def f2(y):
return
def g1(x):
return
def g2(x):
return
Step 4: Numerical Solution
Use the finite difference method to discretize the 2D Laplacian equation and obtain the numerical solution
The Poisson Equation is discretized using
The understanding of this five-point method is important to construct the matrix
This can be written as a system of
where
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
After we assemble the matrix
# Construct the right-hand side vector b
b = np.zeros(N2)
Step 5: Plot the Numerical Solution
To solve the system for
C=np.dot(np.linalg.inv(A),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();
Step 6: Analyze the Numerical Solution
We only study a simple boundary case, where
error = np.linalg.norm(u)
print('Error = ', error)
Deliverables
Have a jupyter-notebook to show your code and results. Based on the results, prepare a 4-minute presentation to show your results and answer the question.
Reference: Finite Difference Methods for the Poisson Equation