{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution of 2-D Poisson equation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We consider the Poisson equation\n",
"$$\n",
" -(u_{xx} + u_{yy}) = f, \\quad\\textrm{in}\\quad \\Omega = [0,1] \\times [0,1]\n",
"$$\n",
"with boundary condition\n",
"$$\n",
"u = 0, \\quad\\textrm{on}\\quad \\partial\\Omega\n",
"$$\n",
"We will take\n",
"$$\n",
"f = 1\n",
"$$\n",
"Make a uniform grid with $n$ points in each direction, with spacing\n",
"$$\n",
"h = \\frac{1}{n-1}\n",
"$$\n",
"The grid points are\n",
"$$\n",
"x_i = ih, \\quad 0 \\le i \\le n-1\n",
"$$\n",
"$$\n",
"y_j = jh, \\quad 0 \\le j \\le n-1\n",
"$$\n",
"The finite difference approximation at $(i,j)$ is\n",
"$$\n",
"-\\frac{u_{i-1,j} - 2u_{i,j} + u_{i+1,j}}{\\Delta x^2} - \\frac{u_{i,j-1} - 2u_{i,j} + u_{i,j+1}}{\\Delta y^2} = f_{i,j}, \\qquad 1 \\le i \\le n-2, \\quad 1 \\le j \\le n-2\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"%config InlineBackend.figure_format = 'svg'\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The next function computers the $L^2$ norm of the residual which is used to test for convergence."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"def residual(h,u,f):\n",
" n = u.shape[0]\n",
" res = 0.0\n",
" for i in range(1,n-1):\n",
" for j in range(1,n-1):\n",
" uxx = (u[i-1,j]-2*u[i,j]+u[i+1,j])/h**2\n",
" uyy = (u[i,j-1]-2*u[i,j]+u[i,j+1])/h**2\n",
" res += (uxx + uyy + f[i,j])**2\n",
" return np.sqrt(res)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, define some problem parameters."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"